import React, { useState, useEffect } from 'react'; import ReactDOM from 'react-dom'; import { useConvex } from 'convex/react'; import { api } from '../../../../convex/_generated/api'; const MobileChannelSettingsScreen = ({ channel, categories, onClose, onDelete }) => { const [visible, setVisible] = useState(false); const [channelName, setChannelName] = useState(channel.name); const [channelTopic, setChannelTopic] = useState(channel.topic || ''); const [selectedCategoryId, setSelectedCategoryId] = useState(channel.categoryId || null); const [showCategoryPicker, setShowCategoryPicker] = useState(false); const [confirmDelete, setConfirmDelete] = useState(false); const [saving, setSaving] = useState(false); const convex = useConvex(); useEffect(() => { requestAnimationFrame(() => setVisible(true)); }, []); const handleClose = () => { setVisible(false); setTimeout(onClose, 250); }; const handleNameChange = (e) => { setChannelName(e.target.value.toLowerCase().replace(/\s+/g, '-')); }; const hasChanges = channelName.trim() !== channel.name || channelTopic.trim() !== (channel.topic || '') || selectedCategoryId !== (channel.categoryId || null); const handleSave = async () => { if (!hasChanges || saving) return; setSaving(true); try { const trimmedName = channelName.trim(); if (trimmedName && trimmedName !== channel.name) { await convex.mutation(api.channels.rename, { id: channel._id, name: trimmedName }); } const trimmedTopic = channelTopic.trim(); if (trimmedTopic !== (channel.topic || '')) { await convex.mutation(api.channels.updateTopic, { id: channel._id, topic: trimmedTopic }); } if (selectedCategoryId !== (channel.categoryId || null)) { await convex.mutation(api.channels.moveChannel, { id: channel._id, categoryId: selectedCategoryId || undefined, position: 0, }); } handleClose(); } catch (err) { console.error('Failed to save channel settings:', err); alert('Failed to save: ' + err.message); } finally { setSaving(false); } }; const handleDelete = async () => { try { await convex.mutation(api.channels.remove, { id: channel._id }); if (onDelete) onDelete(channel._id); handleClose(); } catch (err) { console.error('Failed to delete channel:', err); alert('Failed to delete: ' + err.message); } }; const currentCategoryName = selectedCategoryId ? (categories || []).find(c => c._id === selectedCategoryId)?.name || 'Unknown' : 'None'; return ReactDOM.createPortal(
{/* Header */}
Channel Settings
{/* Body */}
{/* Channel Name */}
{channel.type === 'voice' ? ( ) : '#'}
{/* Channel Topic */}