import { LockClosedIcon, LockOpenIcon, ClockIcon } from "@heroicons/react/16/solid"; import clsx from "clsx"; import { useSessionStore } from "@/stores/sessionStore"; import { sessionApi } from "@/api/sessionApi"; import { Button } from "@/components/Button"; import { usePermissions } from "@/hooks/usePermissions"; import { Permission } from "@/types/permissions"; type RpcSendFunction = (method: string, params: Record, callback: (response: { result?: unknown; error?: { message: string } }) => void) => void; interface SessionControlPanelProps { sendFn: RpcSendFunction; className?: string; } export default function SessionControlPanel({ sendFn, className }: SessionControlPanelProps) { const { currentSessionId, currentMode, sessions, isRequestingPrimary, setRequestingPrimary, setSessionError, canRequestPrimary } = useSessionStore(); const { hasPermission } = usePermissions(); const handleRequestPrimary = async () => { if (!currentSessionId || isRequestingPrimary) return; setRequestingPrimary(true); setSessionError(null); try { const result = await sessionApi.requestPrimary(sendFn, currentSessionId); if (result.status === "success") { if (result.mode === "primary") { // Immediately became primary setRequestingPrimary(false); } else if (result.mode === "queued") { // Request sent, waiting for approval // Keep isRequestingPrimary true to show waiting state } } else if (result.status === "error") { setSessionError(result.message || "Failed to request primary control"); setRequestingPrimary(false); } } catch (error) { setSessionError(error instanceof Error ? error.message : "Unknown error"); console.error("Failed to request primary control:", error); setRequestingPrimary(false); } }; const handleReleasePrimary = async () => { if (!currentSessionId || currentMode !== "primary") return; try { await sessionApi.releasePrimary(sendFn, currentSessionId); } catch (error) { setSessionError(error instanceof Error ? error.message : "Unknown error"); console.error("Failed to release primary control:", error); } }; const canReleasePrimary = () => { const otherEligibleSessions = sessions.filter( s => s.id !== currentSessionId && (s.mode === "observer" || s.mode === "queued") ); return otherEligibleSessions.length > 0; }; return (
{/* Current session controls */}

Session Control

{hasPermission(Permission.SESSION_RELEASE_PRIMARY) && (
)} {hasPermission(Permission.SESSION_REQUEST_PRIMARY) && ( <> {isRequestingPrimary ? (
Waiting for approval from primary session...
) : (
); }