{"version":3,"file":"RepositoryWorkspace.mjs","names":[],"sources":["../../../../src/web/features/settings/RepositoryWorkspace.tsx"],"sourcesContent":["import { useT } from '@agimon-ai/po-core/web';\nimport {\n  Button,\n  LoadingState,\n  RefreshIcon,\n  Select,\n  SelectContent,\n  SelectGroup,\n  SelectItem,\n  SelectLabel,\n  SelectTrigger,\n  SelectValue,\n  SettingsDescription,\n  SettingsHeader,\n  SettingsPage,\n  SettingsTitle,\n} from '@agimon-ai/po-web-components';\nimport { fetchWithStepUp } from '@agimon-ai/po-web-security/browser';\nimport { useStore } from '@tanstack/react-store';\nimport { useEffect, useMemo, useState } from 'react';\n\nimport type { SettingsRepository } from '../../../types/settings';\nimport { ContributedSettings } from '../../components/ContributedSettings';\nimport { focusWorkspaceWebPlugins } from '../../lib/pluginCompositions';\nimport { sealedHttpSession } from '../../lib/sealedSession';\nimport { listSettingsRepositories } from '../../lib/settingsApi';\nimport type { SettingsSection } from '../../lib/settingsSections';\nimport { workspacesStore } from '../../stores/workspacesStore';\nimport { RepositorySettings } from './RepositorySettings';\nimport { SettingsMenu } from './SettingsMenu';\n\nconst LAST_REPOSITORY_KEY = 'po.settings.repository';\n\nconst requestThroughSealedSession = (input: string, init?: RequestInit): Promise<Response> =>\n  sealedHttpSession.fetch(input, init);\n\nfunction rememberedRepository(): string {\n  try {\n    return localStorage.getItem(LAST_REPOSITORY_KEY) ?? '';\n  } catch {\n    return '';\n  }\n}\n\nfunction rememberRepository(repositoryId: string): void {\n  try {\n    localStorage.setItem(LAST_REPOSITORY_KEY, repositoryId);\n  } catch {\n    // Browser storage is optional. The picker still works for this page load.\n  }\n}\n\nexport function RepositoryWorkspace({\n  current,\n  initialRepositoryId,\n  onRepositoryChange,\n  onReady,\n}: {\n  current: SettingsSection;\n  initialRepositoryId?: string;\n  onRepositoryChange?: (repositoryId: string) => void;\n  onReady?: (ready: boolean) => void;\n}) {\n  const t = useT();\n  const workspaceFingerprint = useStore(workspacesStore, (state) => state.order.join('\\n'));\n  const [repositories, setRepositories] = useState<readonly SettingsRepository[]>([]);\n  const [repositoryId, setRepositoryId] = useState(() => initialRepositoryId ?? rememberedRepository());\n  const [loading, setLoading] = useState(true);\n  const repository = useMemo(\n    () => repositories.find((candidate) => candidate.id === repositoryId) ?? null,\n    [repositories, repositoryId],\n  );\n\n  useEffect(() => {\n    let currentRequest = true;\n    // eslint-disable-next-line react/set-state-in-effect -- marks the in-flight request that lists the hub's repositories over HTTP.\n    setLoading(true);\n    void listSettingsRepositories().then((found) => {\n      if (!currentRequest) return;\n      setRepositories(found);\n      setRepositoryId((selected) => {\n        const next = found.some((candidate) => candidate.id === selected)\n          ? selected\n          : (found.find((candidate) => candidate.active)?.id ?? found[0]?.id ?? '');\n        if (next) rememberRepository(next);\n        return next;\n      });\n      setLoading(false);\n    });\n    return () => {\n      currentRequest = false;\n    };\n  }, [workspaceFingerprint]);\n\n  useEffect(() => {\n    if (loading) return;\n    let cancelled = false;\n    void focusWorkspaceWebPlugins(repositoryId || null)\n      .then(() => {\n        if (!cancelled) onReady?.(true);\n      })\n      .catch((error: unknown) => console.error(error));\n    return () => {\n      cancelled = true;\n    };\n  }, [repositoryId, loading, onReady]);\n\n  const selectRepository = (next: string): void => {\n    setRepositoryId(next);\n    rememberRepository(next);\n    onRepositoryChange?.(next);\n  };\n  const panel = current.repositoryPanel;\n  const Panel = panel?.component;\n  // Open and recent are where a repository is, not what it is called, so the\n  // menu groups by them instead of repeating a status after every name.\n  const groups = [\n    {\n      id: 'open',\n      label: t('webDashboard.settings.workspace.open', 'Open'),\n      items: repositories.filter((candidate) => candidate.active),\n    },\n    {\n      id: 'recent',\n      label: t('webDashboard.settings.workspace.recent', 'Recent'),\n      items: repositories.filter((candidate) => !candidate.active),\n    },\n  ].filter((group) => group.items.length > 0);\n\n  return (\n    <div className=\"flex min-h-0 flex-1 flex-col\">\n      <div className=\"flex min-w-0 shrink-0 items-center gap-3 border-b border-po-border-soft px-3 py-2.5 sm:px-5 lg:px-6\">\n        <Select value={repositoryId} disabled={loading || repositories.length === 0} onValueChange={selectRepository}>\n          <SelectTrigger\n            data-testid=\"repository-workspace-picker\"\n            aria-label={t('webDashboard.settings.workspace.repository', 'Repository')}\n            className=\"w-48 shrink-0 text-sm font-medium sm:w-60\"\n          >\n            <SelectValue\n              placeholder={\n                loading\n                  ? t('webDashboard.settings.workspace.reading', 'Reading repositories…')\n                  : t('webDashboard.settings.workspace.pick', 'Pick a repository')\n              }\n            />\n          </SelectTrigger>\n          <SelectContent>\n            {groups.map((group) => (\n              <SelectGroup key={group.id}>\n                <SelectLabel>{group.label}</SelectLabel>\n                {group.items.map((candidate) => (\n                  <SelectItem key={candidate.id} value={candidate.id}>\n                    {candidate.name}\n                  </SelectItem>\n                ))}\n              </SelectGroup>\n            ))}\n          </SelectContent>\n        </Select>\n        <span title={repository?.path} className=\"min-w-0 flex-1 truncate font-mono text-sm text-po-dim\">\n          {repository?.path}\n        </span>\n        <Button\n          variant=\"ghost\"\n          size=\"icon-md\"\n          aria-label={t('webDashboard.settings.workspace.refresh', 'Refresh repositories')}\n          title={t('webDashboard.settings.workspace.refresh', 'Refresh repositories')}\n          loading={loading}\n          disabled={loading}\n          onClick={() => {\n            setLoading(true);\n            void listSettingsRepositories().then((found) => {\n              setRepositories(found);\n              setLoading(false);\n            });\n          }}\n          className=\"shrink-0 text-po-dim\"\n        >\n          <RefreshIcon aria-hidden className=\"size-4\" />\n        </Button>\n      </div>\n      <div className=\"flex min-h-0 flex-1 flex-col lg:flex-row\">\n        <SettingsMenu active={current.id} workspace=\"repository\" repositoryId={repositoryId} />\n        <section\n          data-testid=\"settings-content\"\n          className=\"min-w-0 flex-1 overflow-y-auto px-3 py-3 sm:px-5 sm:py-5 lg:px-8\"\n        >\n          {/* Same column width the general workspace uses. */}\n          <div className=\"mx-auto flex w-full max-w-5xl flex-col gap-8\">\n            {current.id === 'repositories' ? <RepositorySettings repository={repository} /> : null}\n            {current.contribution === undefined ? null : (\n              <ContributedSettings\n                section={current.contribution}\n                scope=\"repository\"\n                repoRoot={repository?.path ?? ''}\n                workspaceId={repository?.id}\n              />\n            )}\n            {panel === undefined || Panel === undefined ? null : (\n              <SettingsPage data-testid={`repository-settings-panel-${panel.pluginId}`}>\n                <SettingsHeader>\n                  <SettingsTitle>{panel.label}</SettingsTitle>\n                  {panel.detail ? <SettingsDescription>{panel.detail}</SettingsDescription> : null}\n                </SettingsHeader>\n                {/* Until the list answers, null would read as no repository picked. */}\n                {loading && repository === null ? (\n                  <LoadingState\n                    size=\"compact\"\n                    label={t('webDashboard.settings.workspace.loading', 'Loading repositories')}\n                  />\n                ) : (\n                  <Panel\n                    repository={repository}\n                    request={requestThroughSealedSession}\n                    requestWithStepUp={fetchWithStepUp}\n                  />\n                )}\n              </SettingsPage>\n            )}\n          </div>\n        </section>\n      </div>\n    </div>\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;AA+BA,MAAM,sBAAsB;AAE5B,MAAM,+BAA+B,OAAe,SAClD,kBAAkB,MAAM,OAAO,IAAI;AAErC,SAAS,uBAA+B;CACtC,IAAI;EACF,OAAO,aAAa,QAAQ,mBAAmB,KAAK;CACtD,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,mBAAmB,cAA4B;CACtD,IAAI;EACF,aAAa,QAAQ,qBAAqB,YAAY;CACxD,QAAQ,CAER;AACF;AAEA,SAAgB,oBAAoB,EAClC,SACA,qBACA,oBACA,WAMC;CACD,MAAM,IAAI,KAAK;CACf,MAAM,uBAAuB,SAAS,kBAAkB,UAAU,MAAM,MAAM,KAAK,IAAI,CAAC;CACxF,MAAM,CAAC,cAAc,mBAAmB,SAAwC,CAAC,CAAC;CAClF,MAAM,CAAC,cAAc,mBAAmB,eAAe,uBAAuB,qBAAqB,CAAC;CACpG,MAAM,CAAC,SAAS,cAAc,SAAS,IAAI;CAC3C,MAAM,aAAa,cACX,aAAa,MAAM,cAAc,UAAU,OAAO,YAAY,KAAK,MACzE,CAAC,cAAc,YAAY,CAC7B;CAEA,g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