import type { SessionAdminAccess } from "@opengeni/sdk"; import { ShieldCheckIcon } from "lucide-react"; import { useCallback, useEffect, useRef, useState } from "react"; import { toast } from "../../lib/toast"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "../../../ui/dropdown-menu"; import { Field, FieldStack } from "../../../ui/field"; import { FormDialog } from "../../../ui/form-dialog"; import { SegmentedControl } from "../../../ui/segmented-control"; import { useAppContext } from "../../app-context"; import { hasInbox } from "../../lib/inbox"; import { ADMIN_ACCESS_DURATIONS, ADMIN_ACCESS_GRANT_DESCRIPTION, adminAccessDurationHours as durationHours, adminAccessGrantedByText as grantedByText, endsText, type AdminAccessDuration, } from "../../../session-admin-access-model"; import { userErrorText } from "../../../ui/api-error"; export { ADMIN_ACCESS_DURATIONS, endsText, type AdminAccessDuration }; /* ---------------------------------------------------------------------------- Full access for one session (named "admin access" in the API). Any member gives a session they started their own access (when the organization allows it, in Organization settings > Security & data) for an hour, a few hours, a day or with no time limit; its agent can then do what they can in the app, as them, and never more. The header shows a small shield while it is on, with when it ends and a one-click way to turn it off. -------------------------------------------------------------------------- */ export type SessionAdminAccessControl = { state: SessionAdminAccess; busy: boolean; grant: (duration: AdminAccessDuration) => Promise; revoke: () => void; }; export function useSessionAdminAccess(session: { workspaceId: string; id: string; }): SessionAdminAccessControl | null { const context = useAppContext(); const client = context.client; const applies = hasInbox(context.accessContext); const [state, setState] = useState(null); const [busy, setBusy] = useState(false); const [reloads, setReloads] = useState(0); useEffect(() => { if (!applies) return; let current = true; client .getSessionAdminAccess(session.workspaceId, session.id) .then((value) => { if (current) setState(value); }) // Older servers, or no access to read it: nothing to show. .catch(() => undefined); return () => { current = false; }; }, [applies, client, reloads, session.id, session.workspaceId]); useEffect(() => { setState(null); }, [session.id, session.workspaceId]); // When the time limit is reached, read it again so the shield goes away. const expiresAt = state?.active ? state.expiresAt : null; useEffect(() => { if (!expiresAt) return; const delay = Date.parse(expiresAt) - Date.now(); if (!Number.isFinite(delay)) return; const timer = setTimeout( () => setReloads((count) => count + 1), Math.min(Math.max(delay + 1_000, 0), 2_147_000_000), ); return () => clearTimeout(timer); }, [expiresAt]); const grant = useCallback( async (duration: AdminAccessDuration) => { setBusy(true); const wasActive = state?.active === true; try { const value = await client.grantSessionAdminAccess(session.workspaceId, session.id, { durationHours: durationHours(duration), }); setState(value); toast.success(wasActive ? "Full access updated" : "This session has full access", { description: wasActive ? endsText(value.expiresAt) : `Its agent can use it from its next message. ${endsText(value.expiresAt)}.`, }); } finally { setBusy(false); } }, [client, session.id, session.workspaceId, state?.active], ); const revoke = useCallback(() => { if (busy) return; setBusy(true); client .revokeSessionAdminAccess(session.workspaceId, session.id) .then((value) => { setState(value); toast.success("Full access is off for this session"); }) .catch((error: unknown) => { toast.error("Couldn't turn off full access", { description: userErrorText(error), }); }) .finally(() => setBusy(false)); }, [busy, client, session.id, session.workspaceId]); if (!state) return null; if (!state.active && !state.canGrant) return null; return { state, busy, grant, revoke }; } /** The small shield beside the title while the session has full access. */ export function SessionAdminAccessIndicator({ control, onChangeTimeLimit, }: { control: SessionAdminAccessControl; /** Opens the time limit dialog; shown to the person who can give access. */ onChangeTimeLimit?: (() => void) | undefined; }) { if (!control.state.active) return null; return ; } function ActiveIndicator({ control, onChangeTimeLimit, }: { control: SessionAdminAccessControl; onChangeTimeLimit?: (() => void) | undefined; }) { // Opening the dialog waits for the menu to close, so the menu's focus // restoration can't pull focus out of the dialog. const afterClose = useRef<(() => void) | null>(null); return ( { const action = afterClose.current; if (!action) return; afterClose.current = null; event.preventDefault(); action(); }} > { afterClose.current = action; }} /> ); } /** * What full access is and how to change it, as menu items: the shield's own * menu, or the header's "…" menu when the shield has no room beside the title. * `runAfterClose` runs an action once the menu has closed. */ export function SessionAdminAccessMenuItems({ control, onChangeTimeLimit, runAfterClose, }: { control: SessionAdminAccessControl; onChangeTimeLimit?: (() => void) | undefined; runAfterClose: (action: () => void) => void; }) { if (!control.state.active) return null; const changeTimeLimit = control.state.canGrant ? onChangeTimeLimit : undefined; return ( <>

{grantedByText(control.state)}

{endsText(control.state.expiresAt)}.

{changeTimeLimit || control.state.canRevoke ? : null} {changeTimeLimit ? ( runAfterClose(changeTimeLimit)}> Change time limit… ) : null} {control.state.canRevoke ? ( control.revoke()}> Turn off full access ) : null} ); } /** Gives this session full access, or changes how long it lasts while it is on. */ export function GrantSessionAdminAccessDialog({ control, open, onOpenChange, }: { control: SessionAdminAccessControl; open: boolean; onOpenChange: (open: boolean) => void; }) { const changing = control.state.active; const [duration, setDuration] = useState("1"); useEffect(() => { if (open) setDuration("1"); }, [open]); return ( { await control.grant(duration); }} onSubmitted={() => onOpenChange(false)} > ); } function durationHint(duration: AdminAccessDuration): string { if (duration === "none") return "It stays on until you turn it off."; const hours = durationHours(duration)!; return `${endsText(new Date(Date.now() + hours * 3_600_000).toISOString())}.`; }