import { useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherArrowLeft, FeatherHash, FeatherMessageCircle, FeatherPlus, FeatherUsers } from "@subframe/core"; import type { ChatChannelCreateMode, ChatChannelCreateSubmission, ChatChannelCreateVM, ChatGroupPrivacy } from "@hivegrid/view-models"; import { Alert } from "~/ui/components/Alert"; import { Avatar } from "~/ui/components/Avatar"; import { Button } from "~/ui/components/Button"; import { ButtonGroup } from "~/ui/components/ButtonGroup"; import { Card } from "~/ui/components/Card"; import { Checkbox } from "~/ui/components/Checkbox"; import { EmptyState } from "~/ui/components/EmptyState"; import { LoadingIndicatorSkeleton } from "~/ui/components/LoadingIndicatorSkeleton"; import { Select } from "~/ui/components/Select"; import { TextArea } from "~/ui/components/TextArea"; import { TextInput } from "~/ui/components/TextInput"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; import { ChatSwitch } from "./ChatSwitch"; export type ChatChannelCreateActions = { readonly onCancel: () => void; readonly onCreate: (submission: ChatChannelCreateSubmission) => void | Promise; readonly onRetry: () => void | Promise; }; export type ChatChannelCreatePageProps = { readonly vm: ChatChannelCreateVM; readonly actions: ChatChannelCreateActions; readonly busy?: boolean; readonly actionErrorKey?: string | null; }; function CreateShell({ children, actions }: { readonly children: React.ReactNode; readonly actions?: React.ReactNode }): React.JSX.Element { const { t } = useTranslation(); return ( } title={t("chat.channelCreate.title")} subtitle={t("chat.channelCreate.subtitle")} actions={actions} uiId="ui.chat.channelCreate.header" /> {children} ); } export function ChatChannelCreatePage({ vm, actions, busy = false, actionErrorKey = null }: ChatChannelCreatePageProps): React.JSX.Element { const { t } = useTranslation(); const readyData = vm.status === "ready" ? vm.data : null; const initialMode = readyData?.selectedMode ?? null; const initialGroupForm = readyData?.form?.mode === "group" ? readyData.form : null; const initialDmForm = readyData?.form?.mode === "dm" ? readyData.form : null; const [mode, setMode] = useState(initialMode); const [name, setName] = useState(initialGroupForm?.name ?? ""); const [description, setDescription] = useState(initialGroupForm?.description ?? ""); const [privacy, setPrivacy] = useState(initialGroupForm?.privacy ?? "public"); const [initialUserIds, setInitialUserIds] = useState(initialGroupForm?.initialUserIds ?? []); const [initialGroupIds, setInitialGroupIds] = useState(initialGroupForm?.initialGroupIds ?? []); const [syncGroupMembership, setSyncGroupMembership] = useState(initialGroupForm?.syncGroupMembership ?? false); const [peerUserId, setPeerUserId] = useState(initialDmForm?.peerUserId ?? ""); const [localErrorKey, setLocalErrorKey] = useState(null); const groupTabRef = useRef(null); const dmTabRef = useRef(null); useEffect(() => { if (!readyData) return; setMode(readyData.selectedMode); if (readyData.form?.mode === "group") { setName(readyData.form.name); setDescription(readyData.form.description); setPrivacy(readyData.form.privacy); setInitialUserIds(readyData.form.initialUserIds); setInitialGroupIds(readyData.form.initialGroupIds); setSyncGroupMembership(readyData.form.syncGroupMembership); } else if (readyData.form?.mode === "dm") { setPeerUserId(readyData.form.peerUserId ?? ""); } setLocalErrorKey(null); }, [readyData]); const selectedUserIds = useMemo(() => new Set(initialUserIds), [initialUserIds]); const selectedGroupIds = useMemo(() => new Set(initialGroupIds), [initialGroupIds]); const canSubmit = mode === "group" ? name.trim().length > 0 : mode === "dm" ? peerUserId.length > 0 : false; function selectMode(nextMode: ChatChannelCreateMode, focus = false): void { setMode(nextMode); setLocalErrorKey(null); if (focus) requestAnimationFrame(() => (nextMode === "group" ? groupTabRef.current : dmTabRef.current)?.focus()); } function handleModeKeyDown(event: React.KeyboardEvent, currentMode: ChatChannelCreateMode): void { if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) return; event.preventDefault(); const allowed = readyData?.allowedModes ?? []; if (allowed.length < 2) return; const currentIndex = allowed.indexOf(currentMode); const nextIndex = event.key === "Home" ? 0 : event.key === "End" ? allowed.length - 1 : event.key === "ArrowRight" ? (currentIndex + 1) % allowed.length : (currentIndex - 1 + allowed.length) % allowed.length; const nextMode = allowed[nextIndex]; if (nextMode) selectMode(nextMode, true); } const submit = () => { if (mode === "group") { if (!name.trim()) { setLocalErrorKey("chat.channelCreate.validation.groupNameRequired"); return; } setLocalErrorKey(null); void actions.onCreate({ mode: "group", name: name.trim(), description: description.trim() || null, privacy, initialUserIds: [...new Set(initialUserIds)], initialGroupIds: [...new Set(initialGroupIds)], syncGroupMembership: syncGroupMembership && initialGroupIds.length > 0, }); return; } if (mode === "dm") { if (!peerUserId) { setLocalErrorKey("chat.channelCreate.validation.dmPeerRequired"); return; } setLocalErrorKey(null); void actions.onCreate({ mode: "dm", peerUserId }); } }; const headerActions = ( {readyData?.allowedModes.length ? ( ) : null} ); if (vm.status === "idle" || vm.status === "loading") { return ( ); } if (vm.status === "error") { return ( void actions.onRetry()}> {t("shared.retry")} } data-ui-id="ui.chat.channelCreate.error" /> ); } if (vm.status === "empty" || !readyData || readyData.allowedModes.length === 0 || !mode) { return ( {t("chat.channelCreate.actions.backToChat")} } data-ui-id="ui.chat.channelCreate.permissionDenied" /> ); } return ( {actionErrorKey || localErrorKey ? ( ) : null} {readyData.allowedModes.length > 1 ? ( {readyData.allowedModes.includes("group") ? ( ) : null} {readyData.allowedModes.includes("dm") ? ( ) : null} ) : null} {mode === "group" ? ( { setName(event.target.value); if (event.target.value.trim()) setLocalErrorKey(null); }} placeholder={t("chat.channelCreate.placeholders.name")} maxLength={120} autoFocus data-ui-id="ui.chat.channelCreate.name" /> setPrivacy(checked ? "private" : "public")} aria-label={t("chat.channelCreate.fields.private")} /> {t(privacy === "private" ? "chat.channelCreate.privacy.privateHelp" : "chat.channelCreate.privacy.publicHelp")} {t("chat.channelCreate.fields.members")} {t("chat.channelCreate.fields.membersHelp")} {readyData.userOptions.length === 0 ? ( ) : ( readyData.userOptions.map((user) => ( {user.avatar.fallbackLabel} {user.displayName} setInitialUserIds((current) => (checked ? [...new Set([...current, user.userId])] : current.filter((id) => id !== user.userId))) } data-user-id={user.userId} /> )) )} {t("chat.channelCreate.fields.groups")} {t("chat.channelCreate.fields.groupsHelp")} {readyData.groupOptions.length === 0 ? ( ) : ( readyData.groupOptions.map((group) => ( {group.name} {group.description || t("chat.channelCreate.groups.noDescription")} ·{" "} {t("chat.channelCreate.groups.memberCount", { count: group.memberCount })} { setInitialGroupIds((current) => { const next = checked ? [...new Set([...current, group.groupId])] : current.filter((id) => id !== group.groupId); if (next.length === 0) setSyncGroupMembership(false); return next; }); }} data-group-id={group.groupId} /> )) )} {t("chat.channelCreate.fields.groupSync")} {t("chat.channelCreate.fields.groupSyncHelp")} ) : ( {readyData.userOptions.length === 0 ? ( ) : ( )} )} ); }