import React, { useState, useEffect } from 'react'; import { Modal } from '../ui/Modal'; import { useUIStore } from '../../stores/uiStore'; import { useServerStore } from '../../stores/serverStore'; import { api } from '../../api/client'; import { PermissionBits, permissionsToString, stringToPermissions } from '../../utils/permissions'; interface ChannelOverride { channelId: string; targetType: string; targetId: string; allow: string; deny: string; } export function ChannelSettingsModal() { const activeModal = useUIStore((s) => s.activeModal); const modalData = useUIStore((s) => s.modalData); const closeModal = useUIStore((s) => s.closeModal); const currentServerId = useServerStore((s) => s.currentServerId); const channels = useServerStore((s) => s.channels); const [isPrivate, setIsPrivate] = useState(false); const [isLoading, setIsLoading] = useState(false); const [isFetching, setIsFetching] = useState(true); const [error, setError] = useState(''); const isOpen = activeModal === 'channelSettings'; const channelId = modalData?.channelId as string | undefined; const channel = channels.find(c => c.id === channelId); // Fetch overrides when modal opens useEffect(() => { if (!isOpen || !channelId || !currentServerId) { setIsFetching(false); return; } setIsFetching(true); setError(''); api.channels.getOverrides(channelId) .then((overrides: ChannelOverride[]) => { // Check if @everyone role (id === serverId) has VIEW_CHANNEL denied const everyoneOverride = overrides.find( o => o.targetType === 'role' && o.targetId === currentServerId ); if (everyoneOverride) { const denyBits = stringToPermissions(everyoneOverride.deny); setIsPrivate((denyBits & PermissionBits.VIEW_CHANNEL) !== 0n); } else { setIsPrivate(false); } }) .catch((err: Error) => { setError(err.message || 'Failed to load channel overrides'); }) .finally(() => { setIsFetching(false); }); }, [isOpen, channelId, currentServerId]); if (!isOpen || !channel || !channelId || !currentServerId) return null; const handleToggle = async () => { setError(''); setIsLoading(true); try { if (!isPrivate) { // Make private: deny VIEW_CHANNEL for @everyone role await api.channels.putOverride(channelId, { targetType: 'role', targetId: currentServerId, allow: '0', deny: permissionsToString(PermissionBits.VIEW_CHANNEL), }); setIsPrivate(true); } else { // Make public: remove the @everyone VIEW_CHANNEL deny override await api.channels.deleteOverride(channelId, 'role', currentServerId); setIsPrivate(false); } } catch (err) { setError(err instanceof Error ? err.message : 'Failed to update channel privacy'); } finally { setIsLoading(false); } }; return (
{channel.name}
{error && (
{error}
)}
Private Channel
Only selected members and roles will be able to view this channel.
{isPrivate && !isFetching && (
This channel is hidden from members without explicit access. Users with the Administrator permission or server owners can always see all channels.
)}
); }