Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

11.2 KB
Raw Blame
import { useState, useEffect } from 'react';
import Drawer from '../Drawer';
import FormField from '../ui/FormField';
import { useAsyncAction } from '../../hooks/useAsyncAction';
import {
  createPrivacyOrg, updatePrivacyOrg, getOrgHoldings, setOrgHoldings, getSocialAccounts,
} from '../../services/api';
import ListLoadError from '../ui/ListLoadError';
import useListResource from '../../hooks/useListResource';
import toast from '../ui/Toast';
import { ORG_CATEGORIES, ORG_TRUST_LEVELS, ORG_STATUSES, VAULT_TYPES, INPUT_CLS, labelFor } from './constants';

const EMPTY = {
  name: '', category: 'other', website: '', trust: 'trusted', status: 'active',
  contactEmail: '', contactPhone: '', notes: '', socialAccountId: '',
};

// Digital Twin accounts for the cross-link picker (#2147); the component owns the error UI.
const fetchSocialAccounts = () => getSocialAccounts({}, { silent: true }).then((res) => res?.accounts || []);

const HOLDINGS_READY = Object.freeze({ state: 'ready', error: null });
const HOLDINGS_LOADING = Object.freeze({ state: 'loading', error: null });

// Human label for a Digital Twin social account in the picker.
const accountLabel = (a) => {
  const handle = a.username ? `@${a.username}` : '';
  const name = a.displayName && a.displayName !== a.username ? ` (${a.displayName})` : '';
  return `${a.platform || 'other'} · ${handle}${name}`.trim();
};

// Create/edit an organization plus which vault records it holds. The holdings
// picker is a checkbox list over the user's vault records (masked values only).
// Holdings are saved via the replace-set endpoint after the org exists (on
// create we POST the org first, then set holdings against the returned id).
export default function OrgDrawer({ open, org, subjectId, vaultRecords, vaultError = null, onRetryVault, onClose, onSaved }) {
  const editing = !!org;
  const [form, setForm] = useState(EMPTY);
  const [selected, setSelected] = useState(() => new Set()); // vaultRecordId set
  // The replace-set write is only safe once the org's stored holdings are in
  // `selected`: saving while that read is pending or failed would send [] and
  // erase every holding the org has.
  const [holdingsLoad, setHoldingsLoad] = useState(HOLDINGS_READY);
  const [holdingsAttempt, setHoldingsAttempt] = useState(0);
  const { items: socialAccounts, state: accountsState, error: accountsError, retry: retryAccounts } = useListResource(fetchSocialAccounts, { enabled: open });

  useEffect(() => {
    if (!open) return undefined;
    setForm(org
      ? {
        name: org.name ?? '',
        category: org.category ?? 'other',
        website: org.website ?? '',
        trust: org.trust ?? 'trusted',
        status: org.status ?? 'active',
        contactEmail: org.contact?.email ?? '',
        contactPhone: org.contact?.phone ?? '',
        notes: org.notes ?? '',
        socialAccountId: org.socialAccountId ?? '',
      }
      : { ...EMPTY });
    setSelected(new Set());
    return undefined;
  }, [open, org]);

  useEffect(() => {
    if (!open || !org?.id) {
      setHoldingsLoad(HOLDINGS_READY);
      return undefined;
    }
    let active = true;
    setHoldingsLoad(HOLDINGS_LOADING);
    getOrgHoldings(org.id, { silent: true })
      .then((h) => {
        if (!active) return;
        setSelected(new Set((h || []).map((x) => x.vaultRecordId)));
        setHoldingsLoad(HOLDINGS_READY);
      })
      .catch((error) => { if (active) setHoldingsLoad({ state: 'error', error }); });
    return () => { active = false; };
  }, [open, org, holdingsAttempt]);

  const set = (k, v) => setForm((prev) => ({ ...prev, [k]: v }));
  const toggleHolding = (id) => setSelected((prev) => {
    const next = new Set(prev);
    if (next.has(id)) next.delete(id); else next.add(id);
    return next;
  });

  const [save, saving] = useAsyncAction(async () => {
    if (!form.name.trim()) { toast.error('Name is required'); return null; }
    const contact = {};
    if (form.contactEmail.trim()) contact.email = form.contactEmail.trim();
    if (form.contactPhone.trim()) contact.phone = form.contactPhone.trim();
    const payload = {
      name: form.name.trim(),
      category: form.category,
      website: form.website,
      trust: form.trust,
      status: form.status,
      contact,
      notes: form.notes,
      // Cross-link to a Digital Twin social account (#2147). Empty → null so a
      // cleared link persists as an explicit clear rather than an empty string.
      socialAccountId: form.socialAccountId || null,
    };

    // The org belongs to whichever household subject was in scope when it was
    // created; the server rejects holdings that cross subjects, so it is not
    // re-assignable on edit.
    const savedOrg = editing
      ? await updatePrivacyOrg(org.id, payload, { silent: true })
      : await createPrivacyOrg({ ...payload, subjectId }, { silent: true });
    if (!savedOrg) return null;

    // Persist holdings (replace-set). Skip the call on create when nothing is
    // selected, and on edit until the stored holdings have loaded.
    const holdings = [...selected].map((vaultRecordId) => ({ vaultRecordId }));
    if (editing ? holdingsLoad.state === 'ready' : holdings.length > 0) {
      await setOrgHoldings(savedOrg.id, holdings, { silent: true }).catch(() => {
        toast.error('Organization saved, but holdings could not be updated');
      });
    }

    toast.success(editing ? 'Organization updated' : 'Organization created');
    onSaved(savedOrg, editing, selected.size);
    return savedOrg;
  }, { errorMessage: editing ? 'Failed to update organization' : 'Failed to create organization' });

  return (
    <Drawer
      open={open}
      onClose={onClose}
      title={editing ? 'Edit organization' : 'New organization'}
      subtitle={editing ? org?.name : undefined}
      size="md"
      closeOnEsc={false}
      closeOnBackdrop={false}
    >
      <form className="space-y-4" onSubmit={(e) => { e.preventDefault(); save(); }}>
        <FormField label="Name">
          <input type="text" value={form.name} onChange={(e) => set('name', e.target.value)} placeholder="e.g. Acme Bank" className={INPUT_CLS} maxLength={200} />
        </FormField>

        <div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
          <FormField label="Category">
            <select value={form.category} onChange={(e) => set('category', e.target.value)} className={INPUT_CLS}>
              {ORG_CATEGORIES.map((c) => <option key={c.id} value={c.id}>{c.label}</option>)}
            </select>
          </FormField>
          <FormField label="Trust">
            <select value={form.trust} onChange={(e) => set('trust', e.target.value)} className={INPUT_CLS}>
              {ORG_TRUST_LEVELS.map((t) => <option key={t.id} value={t.id}>{t.label}</option>)}
            </select>
          </FormField>
          <FormField label="Status">
            <select value={form.status} onChange={(e) => set('status', e.target.value)} className={INPUT_CLS}>
              {ORG_STATUSES.map((s) => <option key={s.id} value={s.id}>{s.label}</option>)}
            </select>
          </FormField>
        </div>

        <FormField label="Website">
          <input type="text" value={form.website} onChange={(e) => set('website', e.target.value)} placeholder="https://…" className={INPUT_CLS} maxLength={2000} />
        </FormField>

        <FormField label="Linked social account (Digital Twin)">
          <select
            value={form.socialAccountId}
            onChange={(e) => set('socialAccountId', e.target.value)}
            className={INPUT_CLS}
          >
            <option value="">— None —</option>
            {/* Preserve the current link so the select isn't blank; it is only "unavailable" once the list actually loaded without it. */}
            {form.socialAccountId && !socialAccounts.some((a) => a.id === form.socialAccountId) && (
              <option value={form.socialAccountId}>{accountsState === 'ready' ? 'Linked account (unavailable)' : 'Linked account'}</option>
            )}
            {socialAccounts.map((a) => (
              <option key={a.id} value={a.id}>{accountLabel(a)}</option>
            ))}
          </select>
          {accountsState === 'error' && (
            <ListLoadError what="Digital Twin social accounts" error={accountsError} onRetry={retryAccounts} className="mt-1" />
          )}
          <p className="text-[11px] text-gray-500 mt-1">
            Cross-links this org to one of your Digital Twin social accounts — the account shows an &ldquo;in org registry&rdquo; badge in return.
          </p>
        </FormField>

        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          <FormField label="Contact email">
            <input type="email" value={form.contactEmail} onChange={(e) => set('contactEmail', e.target.value)} className={INPUT_CLS} maxLength={320} />
          </FormField>
          <FormField label="Contact phone">
            <input type="tel" value={form.contactPhone} onChange={(e) => set('contactPhone', e.target.value)} className={INPUT_CLS} maxLength={64} />
          </FormField>
        </div>

        <div>
          <span className="block text-sm text-gray-400 mb-1">Holdings — which vault records does this org have?</span>
          {holdingsLoad.state === 'error' ? (
            <ListLoadError what="this organization's holdings" error={holdingsLoad.error} onRetry={() => setHoldingsAttempt((n) => n + 1)} />
          ) : holdingsLoad.state === 'loading' ? (
            <p className="text-xs text-gray-500">Loading holdings…</p>
          ) : vaultError ? (
            <ListLoadError what="vault records" error={vaultError} onRetry={onRetryVault} />
          ) : (!vaultRecords || vaultRecords.length === 0) ? (
            <p className="text-xs text-gray-500">No vault records yet. Add records in the Vault tab first.</p>
          ) : (
            <div className="max-h-52 overflow-y-auto border border-port-border rounded divide-y divide-port-border">
              {vaultRecords.map((rec) => (
                <label key={rec.id} className="flex items-center gap-2 px-3 py-2 text-sm cursor-pointer hover:bg-port-bg/50">
                  <input type="checkbox" checked={selected.has(rec.id)} onChange={() => toggleHolding(rec.id)} className="accent-port-accent" />
                  <span className="text-gray-300 truncate flex-1">{rec.label}</span>
                  <span className="text-[11px] text-gray-500">{labelFor(VAULT_TYPES, rec.type)}</span>
                  <span className="font-mono text-[11px] text-gray-500 truncate max-w-[40%]">{rec.maskedValue}</span>
                </label>
              ))}
            </div>
          )}
        </div>

        <FormField label="Notes">
          <textarea value={form.notes} onChange={(e) => set('notes', e.target.value)} rows={3} className={INPUT_CLS} maxLength={5000} />
        </FormField>

        <div className="flex items-center justify-end gap-2 pt-2">
          <button type="button" onClick={onClose} className="px-3 py-2 text-sm text-gray-400 hover:text-white">Cancel</button>
          <button type="submit" disabled={saving} className="px-4 py-2 text-sm rounded bg-port-accent text-white hover:bg-port-accent/80 disabled:opacity-50">
            {saving ? 'Saving…' : (editing ? 'Save changes' : 'Create organization')}
          </button>
        </div>
      </form>
    </Drawer>
  );
}