|
| 1 | +<script lang="ts"> |
| 2 | + // First-class org creation for the workbench — behind the gate. "Try to be |
| 3 | + // sure there is no match": name (+ optional website/domain) → scored |
| 4 | + // candidates from resolver.candidates (slug 100 · domain 90 · fuzzy name |
| 5 | + // 60 — every signal, not just the autocomplete's name-contains) → the gate |
| 6 | + // ALWAYS shows. Picking a candidate just OPENS that org (no write); create |
| 7 | + // is an explicit choice past the gate, never a silent submit. A created |
| 8 | + // org seeds its domain so it's domain-matchable from birth; a website URL |
| 9 | + // also lands as its first org_link. |
| 10 | + // Per context-v/issues/Org-Workbench-Needs-Create-Organization-Behind-A-No-Match-Gate.md. |
| 11 | +
|
| 12 | + import { fetchOrgCandidates, createOrg, addOrgLink } from './lib/org-client'; |
| 13 | + import type { OrgCandidate } from './lib/types'; |
| 14 | +
|
| 15 | + let { |
| 16 | + client, |
| 17 | + onopen, |
| 18 | + oncancel, |
| 19 | + }: { |
| 20 | + client: string; |
| 21 | + // Called with the slug to load — an existing candidate OR the new org. |
| 22 | + onopen: (org_slug: string) => void; |
| 23 | + oncancel: () => void; |
| 24 | + } = $props(); |
| 25 | +
|
| 26 | + let name = $state(''); |
| 27 | + let site = $state(''); // URL or bare domain, optional |
| 28 | + let phase = $state<'form' | 'gate' | 'writing'>('form'); |
| 29 | + let candidates = $state<OrgCandidate[]>([]); |
| 30 | + let error = $state<string | null>(null); |
| 31 | +
|
| 32 | + function siteParts(): { url?: string; domain?: string } { |
| 33 | + const raw = site.trim(); |
| 34 | + if (!raw) return {}; |
| 35 | + const withProto = /^[a-z]+:\/\//i.test(raw) ? raw : `https://${raw}`; |
| 36 | + try { |
| 37 | + const u = new URL(withProto); |
| 38 | + return { url: withProto, domain: u.hostname.toLowerCase().replace(/^www\./, '') }; |
| 39 | + } catch { |
| 40 | + return {}; |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + async function findMatches(e: SubmitEvent) { |
| 45 | + e.preventDefault(); |
| 46 | + if (!name.trim()) return; |
| 47 | + error = null; |
| 48 | + try { |
| 49 | + const { url } = siteParts(); |
| 50 | + candidates = await fetchOrgCandidates({ name: name.trim(), url }, client); |
| 51 | + phase = 'gate'; // always gate — zero candidates still gets an explicit create |
| 52 | + } catch (err) { |
| 53 | + error = err instanceof Error ? err.message : String(err); |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + async function create() { |
| 58 | + phase = 'writing'; |
| 59 | + error = null; |
| 60 | + try { |
| 61 | + const { url, domain } = siteParts(); |
| 62 | + const r = await createOrg({ org_name: name.trim(), org_domain: domain, client }); |
| 63 | + // A given website becomes the new org's first identity link — only on a |
| 64 | + // genuine create; a slug that matched an existing org keeps its lists. |
| 65 | + if (url && r.org_created) { |
| 66 | + await addOrgLink({ org_slug: r.org_slug, url, kind: 'website', client }); |
| 67 | + } |
| 68 | + onopen(r.org_slug); |
| 69 | + } catch (err) { |
| 70 | + error = err instanceof Error ? err.message : String(err); |
| 71 | + phase = 'gate'; |
| 72 | + } |
| 73 | + } |
| 74 | +</script> |
| 75 | + |
| 76 | +<div class="ow-addperson"> |
| 77 | + <form class="ow-addperson-form" onsubmit={findMatches}> |
| 78 | + <input |
| 79 | + class="ow-add-url" |
| 80 | + type="text" |
| 81 | + placeholder="Organization name (required)" |
| 82 | + bind:value={name} |
| 83 | + required |
| 84 | + disabled={phase !== 'form'} |
| 85 | + /> |
| 86 | + <input |
| 87 | + class="ow-add-url" |
| 88 | + type="text" |
| 89 | + placeholder="Website or domain (optional — strongest match signal)" |
| 90 | + bind:value={site} |
| 91 | + disabled={phase !== 'form'} |
| 92 | + /> |
| 93 | + {#if phase === 'form'} |
| 94 | + <span class="ow-addperson-actions"> |
| 95 | + <button type="submit" class="ow-add-go" disabled={!name.trim()}>Find matches</button> |
| 96 | + <button type="button" class="ow-add-go" onclick={oncancel}>Cancel</button> |
| 97 | + </span> |
| 98 | + {/if} |
| 99 | + </form> |
| 100 | + |
| 101 | + {#if phase === 'gate'} |
| 102 | + <div class="ow-gate"> |
| 103 | + {#if candidates.length > 0} |
| 104 | + <p class="ow-gate-note"> |
| 105 | + Existing organizations that might be “{name}” — open one instead of creating a duplicate: |
| 106 | + </p> |
| 107 | + <ul class="ow-gate-list"> |
| 108 | + {#each candidates as c (c.slug)} |
| 109 | + <li> |
| 110 | + <button type="button" class="ow-gate-pick" onclick={() => onopen(c.slug)}> |
| 111 | + <strong>{c.complete_name ?? c.conventional_name ?? c.slug}</strong> |
| 112 | + <span class="ow-gate-headline">{c.slug}</span> |
| 113 | + <span class="ow-gate-score"> |
| 114 | + {c.score} · {c.match_reason.join(', ')} · |
| 115 | + {c.existing.org_links} links · {c.existing.media_streams} streams · |
| 116 | + {c.existing.org_corpus} corpus |
| 117 | + </span> |
| 118 | + </button> |
| 119 | + </li> |
| 120 | + {/each} |
| 121 | + </ul> |
| 122 | + {:else} |
| 123 | + <p class="ow-gate-note">No existing organization matches “{name}”.</p> |
| 124 | + {/if} |
| 125 | + <span class="ow-addperson-actions"> |
| 126 | + <button type="button" class="ow-add-go" onclick={create}> |
| 127 | + No match — create “{name.trim()}” |
| 128 | + </button> |
| 129 | + <button type="button" class="ow-add-go" onclick={() => (phase = 'form')}>Back</button> |
| 130 | + </span> |
| 131 | + </div> |
| 132 | + {:else if phase === 'writing'} |
| 133 | + <p class="ow-gate-note">creating organization…</p> |
| 134 | + {/if} |
| 135 | + {#if error}<div class="ow-error">{error}</div>{/if} |
| 136 | +</div> |
0 commit comments