import { useEffect, useRef, useState } from "react"; import { useAnchoredPopover } from "../../components/living/use-anchored-popover"; import { Tooltip } from "../../components/living/Tooltip"; import { Glyph } from "../../ui/icons"; import { useCopy } from "../../ui/copy"; import type { ChannelRoom, RoomSettings } from "../protocol"; import type { ChannelController } from "../types"; import { ChannelAction } from "./ChannelMedia"; import { ChannelIdentityFields } from "./ChannelIdentity"; import "./channel-room-controls.css"; function RoomForm({ room, appHosted, disabled, busy, onSave, onCancel }: { room?: ChannelRoom; appHosted: boolean; disabled: boolean; busy: boolean; onSave: (settings: RoomSettings) => Promise; onCancel(): void; }) { const { lang } = useCopy(); const zh = lang === "zh"; const [name, setName] = useState(room?.name ?? ""); const [icon, setIcon] = useState(room?.icon ?? "couch"); const [imageBusy, setImageBusy] = useState(false); const [access, setAccess] = useState(room?.access ?? "public"); const [hidden, setHidden] = useState(room?.hidden ?? false); const [password, setPassword] = useState(""); const [removePassword, setRemovePassword] = useState(false); return
{ event.preventDefault(); if (!name.trim() || disabled || imageBusy) return; void onSave({ name: name.trim(), icon, access, hidden: access === "private" && hidden, ...(access === "private" && password ? { password } : removePassword ? { password: null } : {}) }).catch(() => {}); }} onKeyDown={event => { if (event.key === "Enter" && event.nativeEvent.isComposing) event.preventDefault(); }}> {!room &&

{appHosted ? (zh ? "由 App 托管,关闭网页后房间继续运行。" : "Hosted by App. The room keeps running when this page closes.") : (zh ? "由此网页托管,关闭网页后房间暂停。" : "Hosted by this page. The room pauses when this page closes.")}

}
{zh ? "谁可以进入" : "Who can enter"} {(["public", "private"] as const).map(value => )}

{access === "public" ? (zh ? "域内的用户都能进入。" : "Anyone in this domain can enter.") : (zh ? "凭房间邀请或密码进入。" : "Enter with a room invitation or password.")}

{access === "private" &&
{room?.hasPassword && }
}
; } /** Room policy has one editor for creation and management; navigation grants no access. */ export function ChannelRoomControls({ channel, room, onCreated }: { channel: ChannelController; room?: ChannelRoom; onCreated?: (id: string) => void; }) { const { lang, vis } = useCopy(); const zh = lang === "zh"; const trigger = useRef(null); const panel = useRef(null); const [open, setOpen] = useState(false); const [invite, setInvite] = useState(""); const [copied, setCopied] = useState(false); const id = room ? "c3-room-settings" : "c3-create-room"; const title = room ? (zh ? "房间设置" : "Room settings") : (zh ? "创建房间" : "Create room"); useAnchoredPopover(trigger, panel, { side: "below", align: "end" }); useEffect(() => { setInvite(""); setCopied(false); panel.current?.hidePopover(); }, [room?.id, room?.access]); const available = channel.connection === "connected" && (!room || room.available); const busy = channel.pending.has(room ? `room:${room.id}` : "create-room"); const members = room?.members?.filter(userId => userId !== room.creatorId && userId !== channel.state?.channel.ownerId) ?? []; const run = (work: Promise) => { void work.catch(() => {}); }; return <> ; } export function ChannelRoomEntry({ channel, room }: { channel: ChannelController; room: ChannelRoom }) { const { lang } = useCopy(); const zh = lang === "zh"; const [password, setPassword] = useState(""); const available = channel.connection === "connected" && room.available; return

{zh ? "这是一间私密房间" : "This room is private"}

{room.hasPassword ? (zh ? "输入房间密码,或使用房间邀请链接。" : "Enter its password or open a room invitation.") : (zh ? "请向房主索取邀请链接。" : "Ask the room owner for an invitation.")}

{room.hasPassword &&
{ event.preventDefault(); if (!available) return; void channel.enterRoom(room.id, password).then(() => setPassword("")).catch(() => {}); }}> setPassword(event.target.value)} /> }
; }