import type { WorkspaceMeta } from "@yaakapp-internal/models"; import { mergeModelsInStore, patchModel, workspaceMetasAtom, workspacesAtom, } from "@yaakapp-internal/models"; import { Banner, HStack, InlineCode, CountBadge } from "@yaakapp-internal/ui"; import { useAtomValue } from "jotai"; import { useEffect, useState } from "react"; import { useAuthTab } from "../hooks/useAuthTab"; import { useHeadersTab } from "../hooks/useHeadersTab"; import { useInheritedHeaders } from "../hooks/useInheritedHeaders"; import { deleteModelWithConfirm } from "../lib/deleteModelWithConfirm"; import { showDialog } from "../lib/dialog"; import { router } from "../lib/router"; import { rpc } from "../lib/rpc"; import { CopyIconButton } from "./CopyIconButton"; import { Button } from "./core/Button"; import { PlainInput } from "./core/PlainInput"; import { SettingsList, SettingsSection } from "./core/SettingRow"; import { TabContent, Tabs } from "./core/Tabs/Tabs"; import { DnsOverridesEditor } from "./DnsOverridesEditor"; import { HeadersEditor } from "./HeadersEditor"; import { HttpAuthenticationEditor } from "./HttpAuthenticationEditor"; import { MarkdownEditor } from "./MarkdownEditor"; import { ModelSettingsEditor } from "./ModelSettingsEditor"; import { SyncToFilesystemSetting } from "./SyncToFilesystemSetting"; import { WorkspaceEncryptionSetting } from "./WorkspaceEncryptionSetting"; interface Props { workspaceId: string; hide: () => void; tab?: WorkspaceSettingsTab; } const TAB_AUTH = "auth"; const TAB_DNS = "dns"; const TAB_HEADERS = "headers"; const TAB_GENERAL = "general"; const TAB_SETTINGS = "settings"; export type WorkspaceSettingsTab = | typeof TAB_AUTH | typeof TAB_DNS | typeof TAB_HEADERS | typeof TAB_GENERAL | typeof TAB_SETTINGS; const DEFAULT_TAB: WorkspaceSettingsTab = TAB_GENERAL; export function WorkspaceSettingsDialog({ workspaceId, hide, tab }: Props) { const workspace = useAtomValue(workspacesAtom).find((w) => w.id === workspaceId); const workspaceMeta = useAtomValue(workspaceMetasAtom).find((m) => m.workspaceId === workspaceId); const authTab = useAuthTab(TAB_AUTH, workspace ?? null); const headersTab = useHeadersTab(TAB_HEADERS, workspace ?? null); const inheritedHeaders = useInheritedHeaders(workspace ?? null); const workspaceMetaError = useEnsureWorkspaceMeta( workspaceId, workspace != null && workspaceMeta == null, ); if (workspace == null) { return ( Workspace not found ); } return ( 0 ? ( ) : null, }, ]} storageKey="workspace_settings_tabs" > patchModel(workspace, { headers })} stateKey={`headers.${workspace.id}`} /> {workspaceMeta != null ? ( <> patchModel(workspaceMeta, { settingSyncDir: filePath }) } />
) : workspaceMetaError != null ? ( Sync and encryption settings are unavailable because{" "} WorkspaceMeta could not be loaded: {workspaceMetaError} ) : null}
patchModel(workspace, { name })} /> patchModel(workspace, { description })} heightMode="auto" /> {workspaceId}
); } /** * The meta row normally reaches the store when the workspace loads, which also creates it if it * is missing. If that load failed (for example on a corrupt row elsewhere in the workspace), the * store never gets it, so ask for it directly. Returns the error if that fails too. */ function useEnsureWorkspaceMeta(workspaceId: string, missing: boolean): string | null { const [error, setError] = useState(null); useEffect(() => { if (!missing) return; let cancelled = false; setError(null); rpc("cmd_get_workspace_meta", { workspaceId }).then( (meta) => { if (!cancelled) mergeModelsInStore("workspace_meta", [meta]); }, (err) => { if (!cancelled) setError(String(err)); }, ); return () => { cancelled = true; }; }, [workspaceId, missing]); return error; } WorkspaceSettingsDialog.show = (workspaceId: string, tab?: WorkspaceSettingsTab) => { showDialog({ id: "workspace-settings", size: "lg", className: "h-[calc(100vh-5rem)] max-h-200!", noPadding: true, render: ({ hide }) => ( ), }); };