import { useMemo, useState, type ReactNode } from "react"; import { Button } from "../../ui/button"; import { ChoiceCard, ChoiceCards } from "../../ui/choice-cards"; import { CheckboxField } from "../../ui/field"; import { Skeleton } from "../../ui/skeleton"; import { Toolbar, ToolbarSearch } from "../../ui/toolbar"; /* ---------------------------------------------------------------------------- "Available in": where a thing can be used, in the same words for every kind (model accounts, vaults, bots, and whatever else an organization or a workspace shares). One owner per thing decides who manages it; where it works never changes that owner, so the control says both: Available in Belongs to Acme. Organization owners and admins manage it. ( ) All workspaces in Acme every shared workspace, new ones too ( ) Selected workspaces only the ones ticked below [x] Personal workspaces organization things only A workspace's own thing always works in that workspace (its row stays ticked); your own thing always works in your Personal workspace. Your own logins, plans and machines use per-workspace switches that save at once instead ("Where it works"), because each one changes your own turns only. -------------------------------------------------------------------------- */ export type WorkspaceReach = { /** The workspaces it works in; null for every workspace, including new ones. */ workspaceIds: readonly string[] | null; /** Organization things only: whether members can use it in their own Personal workspace. */ personalWorkspaces: boolean; }; export type ReachWorkspace = { id: string; name: string; /** The viewer's own Personal workspace, listed like the others. */ personal?: boolean | undefined; }; /** Who owns a thing, and so who manages it. */ export type ReachOwner = | { kind: "organization"; name: string } | { kind: "workspace"; id: string; name: string } | { kind: "person" }; /** * How Personal workspaces are handled: one checkbox for everyone's * (organization things), always included (your own things), or not offered. */ export type ReachPersonalWorkspaces = "choice" | "always" | "none"; /** * "Belongs to Acme. Organization owners manage it." Who manages a kind differs * (vaults and bots need organization owners, model accounts owners and admins), * so the caller says it; your own things are managed by you alone. */ export function reachOwnerLine(owner: ReachOwner, managedBy?: string): string { const belongs = owner.kind === "person" ? "Belongs to you." : `Belongs to ${owner.name}.`; const manager = managedBy ?? (owner.kind === "person" ? "Only you manage it." : undefined); return manager ? `${belongs} ${manager}` : belongs; } function workspaceCount(count: number): string { return count === 1 ? "1 workspace" : `${count} workspaces`; } /** * The short value for an "Available in" row: "All workspaces + Personal", * "2 workspaces", "Personal workspaces only", "All my workspaces". Workspaces * that always have it (`alwaysIncluded`) count. */ export function workspaceReachSummary( reach: WorkspaceReach, input: { owner: ReachOwner["kind"]; personalWorkspaces: ReachPersonalWorkspaces; alwaysIncluded?: readonly string[] | undefined; }, ): string { if (input.owner === "person") { if (reach.workspaceIds === null) return "All my workspaces"; const count = reach.workspaceIds.length; return count === 0 ? "Only Personal" : `Personal + ${workspaceCount(count)}`; } const personal = input.personalWorkspaces === "choice" && reach.personalWorkspaces; const count = reach.workspaceIds === null ? null : new Set([...reach.workspaceIds, ...(input.alwaysIncluded ?? [])]).size; if (count === 0) return personal ? "Personal workspaces only" : "No workspaces"; const shared = count === null ? "All workspaces" : workspaceCount(count); return personal ? `${shared} + Personal` : shared; } /** Nothing can use it as drafted: no workspace ticked and no Personal workspaces. */ export function workspaceReachIsEmpty( reach: WorkspaceReach, input: { owner: ReachOwner["kind"]; personalWorkspaces: ReachPersonalWorkspaces; alwaysIncluded?: readonly string[] | undefined; }, ): boolean { if (input.owner !== "organization") return false; return ( reach.workspaceIds !== null && reach.workspaceIds.length === 0 && (input.alwaysIncluded ?? []).length === 0 && !(input.personalWorkspaces === "choice" && reach.personalWorkspaces) ); } function toggle(values: readonly string[], value: string, checked: boolean): string[] { return checked ? [...new Set([...values, value])] : values.filter((item) => item !== value); } /** * Workspaces to tick, with their loading, failed, empty and long-list states. * Rows in `alwaysIncluded` stay ticked with their reason; the workspace the * page was opened from says so. */ export function WorkspaceChecklist({ legend = "Workspaces", workspaces, workspacesError = false, onRetryWorkspaces, selected, onSelectedChange, hereId, alwaysIncluded, emptyText = "This organization has no shared workspaces yet.", disabled = false, error, }: { legend?: string; /** null while loading. */ workspaces: readonly ReachWorkspace[] | null; workspacesError?: boolean; onRetryWorkspaces?: (() => void) | undefined; selected: readonly string[]; onSelectedChange: (next: string[]) => void; /** The workspace the page was opened from. */ hereId?: string | null | undefined; /** Workspace id to the reason it is always included. */ alwaysIncluded?: Readonly> | undefined; emptyText?: string; disabled?: boolean; error?: ReactNode; }) { const [query, setQuery] = useState(""); const words = query.trim().toLowerCase(); const long = (workspaces?.length ?? 0) > 8; const shown = useMemo( () => (workspaces ?? []).filter( (workspace) => !words || workspace.name.toLowerCase().includes(words), ), [workspaces, words], ); const ticked = workspaces ? workspaces.filter( (workspace) => alwaysIncluded?.[workspace.id] !== undefined || selected.includes(workspace.id), ).length : 0; return (
{legend} {workspaces === null && workspacesError ? (

Couldn't load the organization's workspaces.

{onRetryWorkspaces ? ( ) : null}
) : workspaces === null ? (
) : workspaces.length === 0 ? (

{emptyText}

) : ( <> {long ? ( ) : null}
{shown.map((workspace) => { const name = workspace.personal ? "Your Personal workspace" : workspace.name; const label = workspace.id === hereId ? `${name} (this workspace)` : name; const reason = alwaysIncluded?.[workspace.id]; return reason !== undefined ? ( ) : ( onSelectedChange(toggle(selected, workspace.id, checked)) } /> ); })} {shown.length === 0 ? (

No workspaces match.

) : null}
{long ? (

{ticked} of {workspaces.length} selected

) : null} )} {error ? (

{error}

) : null}
); } /** * Another way to limit it, beside the workspace choices, such as "Only * selected people" for a model account. While chosen it replaces the * workspace list and the Personal workspaces choice. */ export type ReachExtraChoice = { value: string; title: string; description: string; chosen: boolean; onChoose: () => void; /** Shown under the choices while it is chosen. */ content?: ReactNode; }; /** * The "Available in" control: the owner line, all or selected workspaces, the * workspace list, and for organization things the Personal workspaces choice. */ export function WorkspaceReachFields({ owner, organizationName, managedBy, showOwner = true, label = "Available in", labelHidden = false, value, onChange, workspaces, workspacesError = false, onRetryWorkspaces, hereId, alwaysIncluded, personalWorkspaces, seedSelected, extraChoice, emptyText, error, disabled = false, }: { owner: ReachOwner; organizationName: string; /** Who manages it, as a sentence: "Organization owners manage it." */ managedBy?: string | undefined; /** Off where the owner was chosen right above, so it isn't said twice. */ showOwner?: boolean; label?: string; /** On a page whose title already says it; the group keeps it as its name. */ labelHidden?: boolean; value: WorkspaceReach; onChange: (next: WorkspaceReach) => void; /** The workspaces it could reach; null while loading. */ workspaces: readonly ReachWorkspace[] | null; workspacesError?: boolean; onRetryWorkspaces?: (() => void) | undefined; hereId?: string | null | undefined; /** Workspace id to the reason it is always included (its owner, or one that keeps it). */ alwaysIncluded?: Readonly> | undefined; personalWorkspaces: ReachPersonalWorkspaces; /** The workspaces ticked when switching to Selected workspaces. None by default. */ seedSelected?: (() => string[]) | undefined; extraChoice?: ReachExtraChoice | undefined; emptyText?: string | undefined; error?: ReactNode; disabled?: boolean; }) { const person = owner.kind === "person"; const home = owner.kind === "workspace" ? owner : null; const included = useMemo( () => home ? { [home.id]: "It belongs to this workspace, so it's always included.", ...alwaysIncluded } : alwaysIncluded, [home, alwaysIncluded], ); const listed = useMemo( () => // Your own thing always works in your Personal workspace: no row for it. person ? (workspaces?.filter((workspace) => !workspace.personal) ?? null) : workspaces, [person, workspaces], ); const mode = extraChoice?.chosen ? extraChoice.value : value.workspaceIds === null ? "all" : "selected"; const ownerLine = showOwner ? reachOwnerLine(owner, managedBy) : undefined; return (
{ if (extraChoice && next === extraChoice.value) { extraChoice.onChoose(); return; } onChange({ ...value, workspaceIds: next === "all" ? null : (seedSelected?.() ?? []), }); }} > {extraChoice ? ( ) : null} {extraChoice?.chosen ? ( extraChoice.content ) : value.workspaceIds !== null ? ( onChange({ ...value, workspaceIds })} hereId={hereId} alwaysIncluded={included} {...(emptyText ? { emptyText } : person ? { emptyText: "You have no other workspaces in this organization." } : {})} disabled={disabled} error={error} /> ) : error ? (

{error}

) : null} {personalWorkspaces === "choice" && !extraChoice?.chosen ? (
onChange({ ...value, personalWorkspaces: checked })} />
) : null}
); }