import { useEffect, useState } from "react"; import { FormDialog } from "../../../ui/form-dialog"; import { userErrorText } from "../../../ui/api-error"; import { WorkspaceReachFields, type ReachWorkspace, type WorkspaceReach } from "../workspace-reach"; /** * Where the owner's own Only me file works: the workspace that stores it and * their Personal workspace always, plus the ones they pick, or all of their * workspaces in the organization. It stays Only me in each: only they, and * agent turns they start, can use it there. */ export function FileReachDialog({ open, onOpenChange, title, homeWorkspaceId, reachWorkspaceIds, workspaces, organizationName, onSave, }: { open: boolean; onOpenChange: (open: boolean) => void; /** The file's name. */ title: string; /** The workspace that stores the file, where this page is. */ homeWorkspaceId: string; /** Its other workspaces now; null for all of them. */ reachWorkspaceIds: readonly string[] | null; /** The owner's workspaces in this organization, their Personal workspace marked. */ workspaces: readonly ReachWorkspace[]; organizationName: string; /** Saves the other workspaces it works in, or null for all; throws when refused. */ onSave: (reachWorkspaceIds: string[] | null) => Promise; }) { const savedKey = reachWorkspaceIds === null ? null : [...reachWorkspaceIds].sort().join(","); const [value, setValue] = useState({ workspaceIds: reachWorkspaceIds, personalWorkspaces: true, }); const [error, setError] = useState(null); useEffect(() => { if (open) { setValue({ workspaceIds: savedKey === null ? null : savedKey.split(",").filter(Boolean), personalWorkspaces: true, }); setError(null); } }, [open, savedKey]); const chosen = value.workspaceIds === null ? null : value.workspaceIds.filter((id) => id !== homeWorkspaceId).sort(); const unchanged = chosen === null ? savedKey === null : savedKey === chosen.join(","); return ( { try { await onSave(chosen); } catch (cause) { setError(userErrorText(cause, "Couldn't change where this file works.")); return false; } }} onSubmitted={() => onOpenChange(false)} > ); }