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

35.1 KB
Raw Blame
/**
 * Sharing page — cross-network share buckets via cloud-synced folders.
 *
 * Lists registered buckets, lets the user add/remove buckets and toggle each
 * one's import mode (auto-merge vs inbox), and surfaces the per-bucket inbox
 * + activity log. Top strip lets the user configure the display name + bio
 * that get stamped as the source on every outgoing share.
 */

import { useCallback, useEffect, useRef, useState } from 'react';
import { useParams, useNavigate } from 'react-router';
import {
  Share2, Plus, Trash2, Folder, Inbox, History, Save, Loader2, Check, X, Users, AlertCircle, RefreshCw, Copy, GitMerge,
} from 'lucide-react';
import PageHeader from '../components/PageHeader';
import PageSkeleton from '../components/ui/PageSkeleton';
import ListLoadError from '../components/ui/ListLoadError';
import useListResource from '../hooks/useListResource';
import toast from '../components/ui/Toast';
import InlineConfirmRow from '../components/ui/InlineConfirmRow';
import { FormField } from '../components/ui/FormField';
import { formatDateTime } from '../utils/formatters';
import { useConfirmDelete } from '../hooks/useConfirmDelete';
import useDrawerTab from '../hooks/useDrawerTab';
import FolderPicker from '../components/FolderPicker';
import DuplicatesTab from '../components/sharing/DuplicatesTab';
import ConflictsTab from '../components/sharing/ConflictsTab';
import socket from '../services/socket';
import {
  listShareBuckets, createShareBucket, updateShareBucket, deleteShareBucket,
  listShareInbox, promoteShareInboxItem, dismissShareInboxItem,
  listShareActivity, getSettings, updateSettings,
} from '../services/api';
import { getPageNavTabs } from '../../../server/lib/navManifest.js';
import { buildPageNavTabs } from '../lib/pageNavTabs.js';
import RouteTabsHeader from '../components/ui/RouteTabsHeader';
import TabPills from '../components/ui/TabPills';

const emptyForm = () => ({ name: '', path: '', mode: 'inbox', displayNameOverride: '', bioOverride: '' });

// A subscription row counts as "live" when its latest manifest was queued
// within this window. Re-exports fire on every record edit (debounced ~3s
// server-side) and propagate via chokidar in <1s — so a row received this
// recently means the sender is actively editing the record right now.
const SUBSCRIPTION_LIVE_WINDOW_MS = 5 * 60 * 1000;

export function isLiveSubscription(item, now = Date.now()) {
  if (!item || !item.subscription) return false;
  const receivedMs = Date.parse(item.receivedAt);
  if (!Number.isFinite(receivedMs)) return false;
  return (now - receivedMs) < SUBSCRIPTION_LIVE_WINDOW_MS;
}

// Icon per top-level section id. The manifest (`tabGroup: 'sharing'`) owns
// id/label/order and the absolute path (`to`) — this page owns only how each
// section looks; the page-local "Buckets" label (vs the manifest's page-level
// "Sharing") comes from the manifest's `tabLabel`. Buckets is the
// bucket-management view; Duplicates + Conflicts are the sync-hygiene review
// surfaces. Throws at import time on drift.
const SECTION_PRESENTATION = {
  buckets: { icon: Folder },
  duplicates: { icon: Copy },
  conflicts: { icon: GitMerge },
};

export const SECTIONS = buildPageNavTabs(getPageNavTabs('sharing'), SECTION_PRESENTATION, 'Sharing');

// Per-bucket detail sub-tabs. Held in the `?tab=` URL search param (not local
// state) so the open view is deep-linkable, reload-safe, and stays in sync with
// browser back/forward — a stale/hand-edited value degrades to Inbox.
export const BUCKET_TAB_IDS = ['inbox', 'activity', 'settings'];

const BUCKET_TABS = [
  { id: 'inbox', label: 'Inbox', icon: Inbox },
  { id: 'activity', label: 'Activity', icon: History },
  { id: 'settings', label: 'Settings', icon: Save },
];

function SharingHeader({ active, children }) {
  return (
    <div className="flex h-full min-h-0 flex-col">
      <PageHeader icon={Share2} title="Sharing" />
      <div className="shrink-0">
        <RouteTabsHeader tabs={SECTIONS} activeTab={active} ariaLabel="Sharing sections" />
      </div>
      <div className="flex-1 min-h-0 overflow-auto p-4">
        {children}
      </div>
    </div>
  );
}

export default function Sharing() {
  const { section = 'buckets', bucketId } = useParams();
  if (section === 'duplicates' || section === 'conflicts') {
    // RouteTabsHeader passes no `controlsIdPrefix`, so the panel names itself
    // with its own heading rather than an `aria-labelledby` borrowed from a
    // tab id that doesn't exist (#7420).
    const activeTabLabel = SECTIONS.find((item) => item.id === section)?.label;
    return (
      <SharingHeader active={section}>
        <div role="tabpanel" aria-labelledby="sharing-panel-heading">
          <h2 id="sharing-panel-heading" className="sr-only">{activeTabLabel}</h2>
          {section === 'duplicates' ? <DuplicatesTab /> : <ConflictsTab />}
        </div>
      </SharingHeader>
    );
  }
  return <SharingBuckets selectedId={bucketId || null} />;
}

// The selected bucket lives in the URL (`/sharing/buckets/:bucketId`) so it's
// deep-linkable and reload-safe. `selectedId` is the route param.
function SharingBuckets({ selectedId }) {
  const navigate = useNavigate();
  const fetchBuckets = useCallback(() => listShareBuckets({ silent: true }).then((resp) => resp?.buckets || []), []);
  const { items: buckets, state: bucketsState, error: bucketsError, retry: retryBuckets, setItems: setBuckets } = useListResource(fetchBuckets);
  const loading = bucketsState === 'loading';
  // 'loading' | 'ready' | 'error'. The display-name editor is seeded from these
  // settings and its Save replaces them, so it stays locked until the read succeeds.
  const [settingsState, setSettingsState] = useState('loading');
  const [settingsError, setSettingsError] = useState(null);
  const [showAdd, setShowAdd] = useState(false);
  const [form, setForm] = useState(emptyForm);
  const [creating, setCreating] = useState(false);
  // Active detail sub-tab lives in the URL (`?tab=inbox|activity|settings`) so it's
  // deep-linkable and history-safe; a stale/invalid value degrades to Inbox.
  const [activeTab, setActiveTab] = useDrawerTab('tab', 'inbox', BUCKET_TAB_IDS);

  // Display name + bio settings — used as the source attribution on outgoing shares.
  const [sharingDisplayName, setSharingDisplayName] = useState('');
  const [sharingBio, setSharingBio] = useState('');
  const [savedDisplayName, setSavedDisplayName] = useState('');
  const [savedBio, setSavedBio] = useState('');
  const [savingDisplayName, setSavingDisplayName] = useState(false);

  // Inbox + activity for the selected bucket. A failed read stays distinct from
  // an empty bucket (error row + Retry, never "No pending imports.").
  const fetchInbox = useCallback(
    () => listShareInbox(selectedId, { silent: true }).then((r) => r?.items || []),
    [selectedId],
  );
  const fetchActivity = useCallback(
    () => listShareActivity(selectedId, { silent: true }).then((r) => r?.manifests || []),
    [selectedId],
  );
  const inbox = useListResource(fetchInbox, { enabled: !!selectedId });
  const activity = useListResource(fetchActivity, { enabled: !!selectedId });
  // Socket handlers subscribe once; they read the latest hook state through this ref.
  const liveRef = useRef(null);
  useEffect(() => { liveRef.current = { selectedId, inbox, activity }; });

  const { isConfirming: isRemoveArmed, requestDelete: armRemove, cancelDelete: cancelRemove, confirmDelete: confirmRemove } = useConfirmDelete();

  // Auto-select the first bucket when the URL doesn't already name one, so a
  // bare /sharing lands on a populated detail panel (replace: no history spam).
  useEffect(() => {
    if (bucketsState === 'ready' && !selectedId && buckets.length > 0) {
      navigate(`/sharing/buckets/${encodeURIComponent(buckets[0].id)}`, { replace: true });
    }
  }, [bucketsState]);

  const loadSettings = useCallback(() => {
    setSettingsState('loading');
    return getSettings({ silent: true })
      .then((settings) => {
        const display = settings?.sharingDisplayName || '';
        const bio = settings?.sharingBio || '';
        setSharingDisplayName(display);
        setSharingBio(bio);
        setSavedDisplayName(display);
        setSavedBio(bio);
        setSettingsState('ready');
      })
      .catch((err) => {
        setSettingsError(err instanceof Error ? err : new Error('Request failed'));
        setSettingsState('error');
      });
  }, []);
  useEffect(() => { loadSettings(); }, [loadSettings]);

  // Live socket updates: inbox changes + new manifests. A refresh that fails keeps
  // the rows already shown; a resource already in error retries through the hook.
  useEffect(() => {
    const pickInbox = (r) => r?.items || [];
    const pickActivity = (r) => r?.manifests || [];
    const refresh = (resource, read, pick, bucketId) => {
      if (bucketId !== liveRef.current.selectedId) return;
      if (resource.state === 'error') { resource.retry(); return; }
      read(bucketId, { silent: true })
        .then((r) => { if (bucketId === liveRef.current.selectedId) resource.setItems(pick(r)); })
        .catch((err) => console.warn(`⚠️ Sharing refresh failed: ${err.message}`));
    };
    const onInboxUpdated = ({ bucketId }) => {
      if (bucketId) refresh(liveRef.current.inbox, listShareInbox, pickInbox, bucketId);
    };
    const onManifestProcessed = ({ bucketId }) => {
      if (!bucketId) return;
      refresh(liveRef.current.inbox, listShareInbox, pickInbox, bucketId);
      refresh(liveRef.current.activity, listShareActivity, pickActivity, bucketId);
    };
    socket.on('sharing:inbox-updated', onInboxUpdated);
    socket.on('sharing:manifest-processed', onManifestProcessed);
    return () => {
      socket.off('sharing:inbox-updated', onInboxUpdated);
      socket.off('sharing:manifest-processed', onManifestProcessed);
    };
  }, []);

  const handleCreate = async (e) => {
    e?.preventDefault();
    const name = form.name.trim();
    const path = form.path.trim();
    if (!name) {
      toast.error('Bucket name is required');
      return;
    }
    if (!path) {
      toast.error('Bucket path is required');
      return;
    }
    setCreating(true);
    const result = await createShareBucket({
      name,
      path,
      mode: form.mode,
      displayNameOverride: form.displayNameOverride.trim() || null,
      bioOverride: form.bioOverride.trim() || null,
    }, { silent: true }).catch((err) => {
      toast.error(err.message || 'Failed to register bucket');
      return null;
    });
    setCreating(false);
    if (!result?.bucket) return;
    setBuckets((prev) => [...prev, result.bucket].sort((a, b) => a.name.localeCompare(b.name)));
    navigate(`/sharing/buckets/${encodeURIComponent(result.bucket.id)}`);
    setForm(emptyForm());
    setShowAdd(false);
    toast.success(`Registered bucket "${result.bucket.name}"`);
  };

  const handleDelete = (bucket) => confirmRemove(() => {
    const prior = buckets;
    setBuckets((prev) => prev.filter((b) => b.id !== bucket.id));
    // If the open bucket was removed, fall back to the next remaining one (or the
    // index when none are left).
    if (selectedId === bucket.id) {
      const next = prior.find((b) => b.id !== bucket.id);
      navigate(next ? `/sharing/buckets/${encodeURIComponent(next.id)}` : '/sharing');
    }
    return deleteShareBucket(bucket.id, { silent: true }).catch((err) => {
      toast.error(err.message || 'Failed to remove bucket');
      setBuckets(prior);
    });
  });

  const handleModeToggle = async (bucket) => {
    const nextMode = bucket.mode === 'auto-merge' ? 'inbox' : 'auto-merge';
    const result = await updateShareBucket(bucket.id, { mode: nextMode }, { silent: true }).catch((err) => {
      toast.error(err.message || 'Failed to update mode');
      return null;
    });
    if (!result?.bucket) return;
    setBuckets((prev) => prev.map((b) => (b.id === bucket.id ? result.bucket : b)));
    toast.success(`Bucket "${result.bucket.name}" now ${nextMode}`);
  };

  const handleSaveDisplayName = async () => {
    setSavingDisplayName(true);
    const patch = {
      sharingDisplayName: sharingDisplayName.trim(),
      sharingBio: sharingBio.trim(),
    };
    const merged = await updateSettings(patch, { silent: true }).catch((err) => {
      toast.error(err.message || 'Failed to save');
      return null;
    });
    setSavingDisplayName(false);
    if (!merged) return;
    setSavedDisplayName(patch.sharingDisplayName);
    setSavedBio(patch.sharingBio);
    toast.success('Saved');
  };

  const handlePromote = async (bucketId, manifestId) => {
    const result = await promoteShareInboxItem(bucketId, manifestId, { silent: true }).catch((err) => {
      toast.error(err.message || 'Promote failed');
      return null;
    });
    if (!result) return;
    if (bucketId === liveRef.current.selectedId) inbox.setItems((items) => items.filter((it) => it.manifestId !== manifestId));
    const applied = result.outcome?.applied ?? 0;
    toast.success(`Imported — ${applied} record${applied === 1 ? '' : 's'}`);
  };

  const handleDismiss = async (bucketId, manifestId) => {
    const result = await dismissShareInboxItem(bucketId, manifestId, { silent: true }).catch((err) => {
      toast.error(err.message || 'Dismiss failed');
      return null;
    });
    if (!result) return;
    if (bucketId === liveRef.current.selectedId) inbox.setItems((items) => items.filter((it) => it.manifestId !== manifestId));
  };

  const selected = buckets.find((b) => b.id === selectedId) || null;
  // A bucketId in the URL that isn't in the loaded list (removed / bad deep link).
  const bucketNotFound = !!selectedId && bucketsState === 'ready' && !selected;
  const displayNameDirty = sharingDisplayName !== savedDisplayName || sharingBio !== savedBio;

  return (
    <SharingHeader active="buckets">
      {/* RouteTabsHeader passes no `controlsIdPrefix`, so the panel names
          itself with its own heading rather than an `aria-labelledby`
          borrowed from a tab id that doesn't exist (#7420). */}
      <div role="tabpanel" aria-labelledby="sharing-panel-heading">
      <h2 id="sharing-panel-heading" className="sr-only">Buckets</h2>
      <div className="flex justify-end mb-4">
        <button
          type="button"
          onClick={() => setShowAdd((v) => !v)}
          className="inline-flex items-center gap-2 px-3 py-2 rounded-lg bg-port-accent hover:bg-port-accent/90 text-white text-sm font-medium"
        >
          <Plus size={16} />
          Add bucket
        </button>
      </div>

      <p className="text-sm text-gray-400 mb-6">
        Share pipeline series, universes, and individual media with peers on different networks by exporting into a
        cloud-synced folder (Google Drive, Dropbox, iCloud, Syncthing, USB stick). PortOS reads/writes the folder; the
        cloud-sync app handles the cross-network transport.
        <span className="block mt-1 text-[11px] text-gray-500">
          ⚠️ The cloud provider sees the bucket contents as plaintext. Treat sharing trust like you would a Google Drive folder share.
        </span>
      </p>

      {/* Display name + bio strip */}
      <div className="mb-6 p-4 bg-port-card border border-port-border rounded-lg">
        <div className="flex items-center gap-2 mb-2">
          <Users size={14} className="text-gray-500" />
          <h2 className="text-sm font-medium text-white">Your display name</h2>
        </div>
        <p className="text-xs text-gray-500 mb-3">
          This name is stamped as the <em>source</em> on every share you send. Recipients see it as attribution. Each bucket can override this with its own name.
        </p>
        {settingsState === 'error' && (
          <ListLoadError what="your display name" error={settingsError} onRetry={loadSettings} className="mb-3" />
        )}
        <div className="grid grid-cols-1 md:grid-cols-[1fr_2fr_auto] gap-3 items-start">
          <input
            type="text"
            aria-label="Your display name"
            value={sharingDisplayName}
            onChange={(e) => setSharingDisplayName(e.target.value)}
            disabled={settingsState !== 'ready'}
            placeholder="Display name (e.g. atomantic)"
            maxLength={120}
            className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
          />
          <input
            type="text"
            aria-label="Your bio / contact note"
            value={sharingBio}
            onChange={(e) => setSharingBio(e.target.value)}
            disabled={settingsState !== 'ready'}
            placeholder="Optional bio / contact note (visible to recipients)"
            maxLength={2000}
            className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
          />
          <button
            type="button"
            onClick={handleSaveDisplayName}
            disabled={!displayNameDirty || savingDisplayName || settingsState !== 'ready'}
            className="inline-flex items-center gap-2 px-3 py-2 rounded bg-port-accent text-white text-sm disabled:opacity-40 disabled:cursor-not-allowed"
          >
            {savingDisplayName ? <Loader2 size={14} className="animate-spin" /> : <Save size={14} />}
            Save
          </button>
        </div>
      </div>

      {showAdd && (
        <form onSubmit={handleCreate} className="mb-6 p-4 bg-port-card border border-port-border rounded-lg space-y-3">
          <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
            <FormField label="Bucket name" labelClassName="block text-xs uppercase tracking-wider text-gray-500 mb-1">
              <input
                type="text"
                value={form.name}
                onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))}
                placeholder="Creative circle"
                maxLength={120}
                className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
                autoFocus
              />
            </FormField>
            <FormField label="Import mode" labelClassName="block text-xs uppercase tracking-wider text-gray-500 mb-1">
              <select
                value={form.mode}
                onChange={(e) => setForm((f) => ({ ...f, mode: e.target.value }))}
                className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
              >
                <option value="inbox">Inbox — review before importing</option>
                <option value="auto-merge">Auto-merge — apply immediately (trusted)</option>
              </select>
            </FormField>
          </div>
          <div>
            <label htmlFor="share-folder-path" className="block text-xs uppercase tracking-wider text-gray-500 mb-1">
              Folder path
            </label>
            <div className="flex gap-2 items-stretch">
              <input
                id="share-folder-path"
                type="text"
                value={form.path}
                onChange={(e) => setForm((f) => ({ ...f, path: e.target.value }))}
                placeholder="/Users/you/Library/CloudStorage/GoogleDrive-…/PortOS Shares/creative-circle"
                className="flex-1 min-w-0 px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm font-mono"
              />
              <FolderPicker
                value={form.path}
                onChange={(path) => setForm((f) => ({ ...f, path }))}
              />
            </div>
            <p className="text-[11px] text-gray-500 mt-1">
              Pick a folder that's synced by your cloud-storage app. PortOS will create <code>manifests/</code>, <code>records/</code>, and <code>assets/</code> inside it.
            </p>
          </div>
          <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
            <FormField label="Display name override (optional)" labelClassName="block text-xs uppercase tracking-wider text-gray-500 mb-1">
              <input
                type="text"
                value={form.displayNameOverride}
                onChange={(e) => setForm((f) => ({ ...f, displayNameOverride: e.target.value }))}
                placeholder="Use a different name in this bucket"
                maxLength={120}
                className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
              />
            </FormField>
            <FormField label="Bio override (optional)" labelClassName="block text-xs uppercase tracking-wider text-gray-500 mb-1">
              <input
                type="text"
                value={form.bioOverride}
                onChange={(e) => setForm((f) => ({ ...f, bioOverride: e.target.value }))}
                placeholder="Different bio for this bucket"
                maxLength={2000}
                className="w-full px-3 py-2 bg-port-bg border border-port-border rounded text-white text-sm"
              />
            </FormField>
          </div>
          <div className="flex gap-2">
            <button
              type="submit"
              disabled={creating}
              className="inline-flex items-center gap-2 px-3 py-2 rounded bg-port-accent text-white text-sm font-medium disabled:opacity-50"
            >
              {creating ? <Loader2 size={14} className="animate-spin" /> : null}
              Register bucket
            </button>
            <button
              type="button"
              onClick={() => setShowAdd(false)}
              className="px-3 py-2 rounded text-gray-400 hover:text-white text-sm"
            >
              Cancel
            </button>
          </div>
        </form>
      )}

      <div className="grid grid-cols-1 md:grid-cols-[280px_1fr] gap-4">
        {/* Bucket list */}
        <aside>
          {loading ? (
            <PageSkeleton header="none" label="Loading buckets" cards={3} sidebar={false} />
          ) : bucketsState === 'error' ? (
            <ListLoadError what="buckets" error={bucketsError} onRetry={retryBuckets} />
          ) : buckets.length === 0 ? (
            <div className="p-4 bg-port-card border border-port-border rounded-lg text-sm text-gray-500">
              No buckets yet. Click <span className="text-port-accent">Add bucket</span> to register a synced folder.
            </div>
          ) : (
            <ul className="space-y-2">
              {buckets.map((b) => {
                const inboxCount = selectedId === b.id && inbox.state === 'ready' ? inbox.items.length : 0;
                const isSelected = selectedId === b.id;
                const incompatible = b.schemaCompatible === false;
                return (
                  <li key={b.id}>
                    <button
                      type="button"
                      onClick={() => navigate(`/sharing/buckets/${encodeURIComponent(b.id)}`)}
                      className={`w-full text-left p-3 rounded-lg border ${isSelected ? 'bg-port-card border-port-accent/40' : 'bg-port-card border-port-border'} hover:border-port-accent/30 transition-colors`}
                    >
                      <div className="flex items-center justify-between gap-2">
                        <span className="text-white text-sm font-medium truncate flex items-center gap-1">
                          {incompatible ? <AlertCircle size={12} className="text-port-error shrink-0" /> : null}
                          <span className="truncate">{b.name}</span>
                        </span>
                        {inboxCount > 0 && (
                          <span className="inline-flex items-center justify-center min-w-[18px] px-1.5 py-0.5 rounded-full bg-port-accent text-[10px] text-white">
                            {inboxCount}
                          </span>
                        )}
                      </div>
                      <div className="text-[11px] text-gray-500 truncate mt-1 flex items-center gap-1">
                        <Folder size={10} />
                        {b.path}
                      </div>
                      <div className="text-[10px] text-gray-600 mt-1 flex items-center gap-2 flex-wrap">
                        <span>{b.mode === 'auto-merge' ? 'auto-merge' : 'inbox'}</span>
                        {b.bucketSchemaVersion != null && (
                          <span className={incompatible ? 'text-port-error' : ''} title={incompatible ? `Bucket protocol v${b.bucketSchemaVersion} > your PortOS (v${b.localSchemaVersion}). Upgrade required to read incoming shares.` : `Protocol v${b.bucketSchemaVersion}`}>
                            schema v{b.bucketSchemaVersion}
                          </span>
                        )}
                      </div>
                    </button>
                  </li>
                );
              })}
            </ul>
          )}
        </aside>

        {/* Detail panel */}
        <section>
          {bucketNotFound ? (
            <div className="p-6 bg-port-card border border-port-border rounded-lg text-sm text-gray-500">
              That bucket could not be found — it may have been removed.{' '}
              <button type="button" onClick={() => navigate('/sharing')} className="text-port-accent hover:underline">
                Back to buckets
              </button>
            </div>
          ) : !selected ? (
            <div className="p-6 bg-port-card border border-port-border rounded-lg text-sm text-gray-500">
              Pick a bucket on the left to see its inbox + activity.
            </div>
          ) : (
            <div>
              <TabPills
                tabs={BUCKET_TABS.map((tab) => (
                  tab.id === 'inbox'
                    ? { ...tab, count: inbox.items.length }
                    : tab
                ))}
                activeTab={activeTab}
                onChange={setActiveTab}
                size="sm"
                mobileCompact
                ariaLabel="Bucket views"
                className="mb-3"
              />

              {activeTab === 'inbox' && (
                <Inboxlist
                  bucket={selected}
                  items={inbox.items}
                  state={inbox.state}
                  error={inbox.error}
                  onRetry={inbox.retry}
                  onPromote={(id) => handlePromote(selected.id, id)}
                  onDismiss={(id) => handleDismiss(selected.id, id)}
                />
              )}
              {activeTab === 'activity' && (
                <ActivityList manifests={activity.items} state={activity.state} error={activity.error} onRetry={activity.retry} />
              )}
              {activeTab === 'settings' && (
                <SettingsPanel
                  bucket={selected}
                  onToggleMode={() => handleModeToggle(selected)}
                  onRequestDelete={() => armRemove(selected.id)}
                  onConfirmDelete={() => handleDelete(selected)}
                  onCancelDelete={cancelRemove}
                  armed={isRemoveArmed(selected.id)}
                />
              )}
            </div>
          )}
        </section>
      </div>
      </div>
    </SharingHeader>
  );
}

function Inboxlist({ bucket, items, state, error, onRetry, onPromote, onDismiss }) {
  if (bucket.mode === 'auto-merge') {
    return (
      <div className="p-4 bg-port-card border border-port-border rounded-lg text-sm text-gray-500 flex items-start gap-2">
        <AlertCircle size={14} className="mt-0.5 text-port-accent" />
        <div>
          This bucket is in <strong className="text-white">auto-merge</strong> mode — incoming shares are applied immediately. Switch to <strong className="text-white">inbox</strong> in the Settings tab to require manual review.
        </div>
      </div>
    );
  }
  if (state === 'error') return <ListLoadError what="the inbox" error={error} onRetry={onRetry} />;
  if (state === 'loading') return <div className="p-4 text-sm text-gray-500">Loading inbox…</div>;
  if (items.length === 0) {
    return <div className="p-4 bg-port-card border border-port-border rounded-lg text-sm text-gray-500">No pending imports.</div>;
  }
  return (
    <ul className="space-y-2">
      {items.map((item) => (
        <li key={item.manifestId} className="p-3 bg-port-card border border-port-border rounded-lg">
          <div className="flex items-start justify-between gap-3">
            <div className="flex-1 min-w-0">
              <div className="text-sm text-white flex items-center gap-2 flex-wrap">
                <span className="text-port-accent">{item.source}</span>
                {item.producedByVersion && item.producedByVersion !== 'unknown' && (
                  <span className="text-gray-500 text-[11px]">(PortOS {item.producedByVersion})</span>
                )}
                <span className="text-gray-600">· {item.kind}</span>
                <span className="text-gray-600">· {formatDateTime(item.receivedAt || item.createdAt)}</span>
                {isLiveSubscription(item) && (
                  <span
                    className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded bg-port-success/10 border border-port-success/30 text-[10px] text-port-success"
                    title={`Subscription refreshed within the last ${SUBSCRIPTION_LIVE_WINDOW_MS / 60000} minutes — sender is actively editing`}
                  >
                    <RefreshCw size={9} className="animate-spin [animation-duration:3s]" />
                    live
                  </span>
                )}
              </div>
              {item.sourceBio ? (
                <div className="text-[11px] text-gray-500 mt-0.5 italic">{item.sourceBio}</div>
              ) : null}
              {Array.isArray(item.summary) && item.summary.length > 0 ? (
                <ul className="mt-1 text-[12px] text-gray-400 list-disc pl-4">
                  {item.summary.map((s, i) => (
                    <li key={i}><span className="text-gray-600">{s.kind}:</span> {s.label}</li>
                  ))}
                </ul>
              ) : null}
              <div className="text-[11px] text-gray-500 mt-1 flex flex-wrap gap-3">
                {item.assetCount ? <span>{item.assetCount} asset{item.assetCount === 1 ? '' : 's'}</span> : null}
                {item.collectionItemCount ? (
                  <span>+ {item.collectionItemCount} collection item{item.collectionItemCount === 1 ? '' : 's'}{item.collectionName ? ` (${item.collectionName})` : ''}</span>
                ) : null}
              </div>
            </div>
            <div className="flex flex-col gap-1">
              <button
                type="button"
                onClick={() => onPromote(item.manifestId)}
                className="inline-flex items-center gap-1 px-2 py-1 rounded text-xs bg-port-success/20 text-port-success hover:bg-port-success/30 border border-port-success/30"
                title="Apply to local state"
              >
                <Check size={12} />
                Import
              </button>
              <button
                type="button"
                onClick={() => onDismiss(item.manifestId)}
                className="inline-flex items-center gap-1 px-2 py-1 rounded text-xs text-gray-400 hover:text-port-error border border-port-border"
                title="Discard this share"
              >
                <X size={12} />
                Dismiss
              </button>
            </div>
          </div>
        </li>
      ))}
    </ul>
  );
}

function ActivityList({ manifests, state, error, onRetry }) {
  if (state === 'error') return <ListLoadError what="share activity" error={error} onRetry={onRetry} />;
  if (state === 'loading') return <div className="p-4 text-sm text-gray-500">Loading activity…</div>;
  if (manifests.length === 0) {
    return <div className="p-4 bg-port-card border border-port-border rounded-lg text-sm text-gray-500">No share activity yet.</div>;
  }
  return (
    <ul className="space-y-1.5">
      {manifests.map((m) => (
        <li key={m.id} className="p-2 bg-port-card border border-port-border rounded text-xs flex items-center gap-2 flex-wrap">
          <span className="text-gray-500">{formatDateTime(m.createdAt)}</span>
          <span className="text-port-accent">{m.source}</span>
          {m.producedByVersion && m.producedByVersion !== 'unknown' && (
            <span className="text-gray-500 text-[10px]">v{m.producedByVersion}</span>
          )}
          <span className="text-gray-600">·</span>
          <span className="text-gray-300">{m.kind}</span>
          <span className="text-gray-600">·</span>
          <span className="text-gray-500">{(m.recordIds || []).length} record{(m.recordIds || []).length === 1 ? '' : 's'}, {(m.assetRefs || []).length} asset{(m.assetRefs || []).length === 1 ? '' : 's'}</span>
        </li>
      ))}
    </ul>
  );
}

function SettingsPanel({ bucket, onToggleMode, onRequestDelete, onConfirmDelete, onCancelDelete, armed }) {
  return (
    <div className="space-y-4">
      <div className="p-4 bg-port-card border border-port-border rounded-lg">
        <h3 className="text-sm font-medium text-white mb-2">Import mode</h3>
        <div className="flex items-center gap-3">
          <span className={`text-sm ${bucket.mode === 'inbox' ? 'text-white' : 'text-gray-500'}`}>Inbox (review)</span>
          <button
            type="button"
            role="switch"
            aria-checked={bucket.mode === 'auto-merge'}
            onClick={onToggleMode}
            className={`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${bucket.mode === 'auto-merge' ? 'bg-port-accent' : 'bg-port-border'}`}
            aria-label="Auto-merge incoming records (off queues them in the inbox for review)"
          >
            <span className={`inline-block h-3 w-3 transform rounded-full bg-white transition-transform ${bucket.mode === 'auto-merge' ? 'translate-x-5' : 'translate-x-1'}`} />
          </button>
          <span className={`text-sm ${bucket.mode === 'auto-merge' ? 'text-white' : 'text-gray-500'}`}>Auto-merge</span>
        </div>
        <p className="text-[11px] text-gray-500 mt-2">
          Auto-merge applies incoming records directly (LWW by updatedAt). Inbox queues them for explicit review.
        </p>
      </div>
      <div className="p-4 bg-port-card border border-port-border rounded-lg">
        <h3 className="text-sm font-medium text-white mb-1">Folder</h3>
        <div className="text-xs font-mono text-gray-400 break-all">{bucket.path}</div>
        <p className="text-[11px] text-gray-500 mt-2">
          To move a bucket to a different folder, remove this bucket and register a new one at the target path.
        </p>
      </div>
      <div className="p-4 bg-port-card border border-port-error/30 rounded-lg">
        <h3 className="text-sm font-medium text-port-error mb-2">Remove bucket</h3>
        <p className="text-[11px] text-gray-500 mb-3">
          Stops watching the folder for new shares and drops the bucket from PortOS. Files inside the folder are not deleted — your cloud-sync app keeps the data.
        </p>
        {armed ? (
          <InlineConfirmRow
            question="Remove this bucket?"
            confirmText="Remove"
            onConfirm={onConfirmDelete}
            onCancel={onCancelDelete}
          />
        ) : (
          <button
            type="button"
            onClick={onRequestDelete}
            className="inline-flex items-center gap-2 px-3 py-2 rounded text-xs bg-port-bg text-port-error border border-port-error/40 hover:bg-port-error/10"
          >
            <Trash2 size={12} />
            Remove bucket
          </button>
        )}
      </div>
    </div>
  );
}