|
| 1 | +<script lang="ts"> |
| 2 | + // org-workbench — the first surface of the "Augment from DB" flow. Start |
| 3 | + // from a canonical SurrealDB organization: smart-search to it, then work |
| 4 | + // its card (identity/social links, pulse streams, corpus items) with an |
| 5 | + // additive ➕ on every list. Credential-free (spec D1) — everything rides |
| 6 | + // workspace.invoke. Client-derivation + workspace-changed handling copied |
| 7 | + // from person-db-resolver. See context-v/specs/Augment-From-DB-Flow.md. |
| 8 | +
|
| 9 | + import { onMount } from 'svelte'; |
| 10 | + import { workspace } from '@augment-it/workspace'; |
| 11 | + import OrgSearch from './OrgSearch.svelte'; |
| 12 | + import OrgCard from './OrgCard.svelte'; |
| 13 | + import { fetchOrgDetail } from './lib/org-client'; |
| 14 | + import type { OrgDetail, OrgSuggestion } from './lib/types'; |
| 15 | +
|
| 16 | + const TOKEN_KEY = 'augment-it:session-token'; |
| 17 | + const WS_URL = 'ws://localhost:3001/ws'; |
| 18 | + // Restore the last-worked org on remount (HMR, flow switch, tab reopen). |
| 19 | + const ACTIVE_ORG_KEY = 'augment-it:org-workbench:active-org'; |
| 20 | +
|
| 21 | + let status = $state<'connecting' | 'open' | 'closed' | 'error'>('connecting'); |
| 22 | + let client = $state<string>('reach-edu'); |
| 23 | +
|
| 24 | + let org = $state<OrgDetail | null>(null); |
| 25 | + let loading = $state(false); |
| 26 | + let error = $state<string | null>(null); |
| 27 | +
|
| 28 | + async function loadOrg(org_slug: string) { |
| 29 | + loading = true; |
| 30 | + error = null; |
| 31 | + try { |
| 32 | + org = await fetchOrgDetail(org_slug, client); |
| 33 | + if (typeof localStorage !== 'undefined') localStorage.setItem(ACTIVE_ORG_KEY, org_slug); |
| 34 | + } catch (err) { |
| 35 | + error = err instanceof Error ? err.message : String(err); |
| 36 | + org = null; |
| 37 | + } finally { |
| 38 | + loading = false; |
| 39 | + } |
| 40 | + } |
| 41 | +
|
| 42 | + function onPick(s: OrgSuggestion) { |
| 43 | + void loadOrg(s.slug); |
| 44 | + } |
| 45 | +
|
| 46 | + function refetch() { |
| 47 | + if (org) void loadOrg(org.slug); |
| 48 | + } |
| 49 | +
|
| 50 | + function onEntityUpdated(e: Event) { |
| 51 | + const detail = (e as CustomEvent).detail as { org_slug?: string } | undefined; |
| 52 | + if (detail?.org_slug && org && detail.org_slug === org.slug) refetch(); |
| 53 | + } |
| 54 | +
|
| 55 | + function onWorkspaceChanged(e: Event) { |
| 56 | + const detail = (e as CustomEvent).detail as { client_id?: string } | undefined; |
| 57 | + if (detail?.client_id) client = detail.client_id; |
| 58 | + else void loadActiveClient(); |
| 59 | + // A different client sees a different slice of the canonical layer — |
| 60 | + // drop the card rather than show rows the new client may not access. |
| 61 | + org = null; |
| 62 | + } |
| 63 | +
|
| 64 | + async function loadActiveClient() { |
| 65 | + try { |
| 66 | + const r = (await workspace.invoke('workspace.active', {})) as { active_client_id?: string }; |
| 67 | + if (r?.active_client_id) client = r.active_client_id; |
| 68 | + } catch { |
| 69 | + /* keep default */ |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + onMount(() => { |
| 74 | + workspace.connect({ |
| 75 | + url: WS_URL, |
| 76 | + getToken: () => localStorage.getItem(TOKEN_KEY), |
| 77 | + saveToken: (t) => localStorage.setItem(TOKEN_KEY, t), |
| 78 | + onStatus: (s) => (status = s), |
| 79 | + }); |
| 80 | + void (async () => { |
| 81 | + await loadActiveClient(); |
| 82 | + const stored = typeof localStorage !== 'undefined' ? localStorage.getItem(ACTIVE_ORG_KEY) : null; |
| 83 | + if (stored) void loadOrg(stored); |
| 84 | + })(); |
| 85 | + window.addEventListener('augment-it:workspace-changed', onWorkspaceChanged); |
| 86 | + window.addEventListener('augment-it:entity-updated', onEntityUpdated); |
| 87 | + return () => { |
| 88 | + window.removeEventListener('augment-it:workspace-changed', onWorkspaceChanged); |
| 89 | + window.removeEventListener('augment-it:entity-updated', onEntityUpdated); |
| 90 | + }; |
| 91 | + }); |
| 92 | +</script> |
| 93 | + |
| 94 | +<div class="ow-app"> |
| 95 | + <header class="ow-header"> |
| 96 | + <div class="ow-title-row"> |
| 97 | + <h1 class="ow-title">Org Workbench</h1> |
| 98 | + <span class="ow-source">SurrealDB · Organizations</span> |
| 99 | + <span class="ow-client">client: <strong>{client}</strong></span> |
| 100 | + <span class="ow-ws status-{status}">{status}</span> |
| 101 | + </div> |
| 102 | + <OrgSearch {client} onpick={onPick} /> |
| 103 | + </header> |
| 104 | + |
| 105 | + <main class="ow-body"> |
| 106 | + {#if loading} |
| 107 | + <p class="ow-loading">loading…</p> |
| 108 | + {:else if error} |
| 109 | + <div class="ow-error">{error}</div> |
| 110 | + {:else if org} |
| 111 | + <OrgCard {org} {client} onchanged={refetch} /> |
| 112 | + {:else} |
| 113 | + <p class="ow-empty-state"> |
| 114 | + Search for an organization above to open its card — links, pulse streams, corpus items, |
| 115 | + and (soon) its people. |
| 116 | + </p> |
| 117 | + {/if} |
| 118 | + </main> |
| 119 | +</div> |
0 commit comments