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

19.0 KB
Raw Blame
import { useEffect, useMemo, useRef, useState } from 'react';
import { Link, useNavigate } from 'react-router';
import { Plus, FolderOpen, Inbox, Trash2, Image as ImageIcon, Film, Search, AlertTriangle } from 'lucide-react';
import PageSkeleton from '../components/ui/PageSkeleton';
import Banner from '../components/ui/Banner';
import EmptyState from '../components/EmptyState';
import toast from '../components/ui/Toast';
import {
  listMediaCollections, createMediaCollection, deleteMediaCollection,
  listGalleryCollectionSummaries,
} from '../services/api';
import { buildUnsortedCollection } from '../lib/unsorted';
import {
  applyCollectionView, splitCollectionName, collectionItemCount,
  COLLECTION_SORTS, DEFAULT_COLLECTION_SORT, normalizeCollectionSort,
} from '../lib/mediaCollectionList';
import { middleTruncate } from '../utils/formatters';
import MediaImage from '../components/MediaImage';
import SyncBadge from '../components/sync/SyncBadge';
import { useSyncIntegrity, syncBadgeStatus } from '../hooks/useSyncIntegrity';
import useUrlParams from '../hooks/useUrlParams';
import useCancelableDebounce from '../hooks/useCancelableDebounce';

// Cap for the card title AFTER the auto-creator prefix moves to its badge.
// Middle-truncating at this length keeps the trailing date/qualifier — the only
// thing distinguishing sibling auto-generated names — visible, which a plain
// end-clip (`line-clamp`) always eats.
const TITLE_MAX_CHARS = 34;

export default function MediaCollections() {
  // Focus target for the empty state's call to action — the create form sits
  // above the list, so the button has to point back up at it.
  const nameInputRef = useRef(null);
  const navigate = useNavigate();
  const [searchParams, updateParams] = useUrlParams();
  // `null` = collections were never successfully fetched (initial state, or a
  // first-load failure); `[]` = the server really has none. Collapsing the two
  // is what let a failed fetch render the "No collections yet" onboarding copy
  // and hand every image/video in the library to the synthetic "Unsorted"
  // bucket, reading as "all my collections were deleted" (#6019).
  const [collections, setCollections] = useState(null);
  const [collectionsError, setCollectionsError] = useState(null);
  const [loading, setLoading] = useState(true);
  const [creating, setCreating] = useState(false);
  const [name, setName] = useState('');
  const [summaries, setSummaries] = useState([]);

  // Sync integrity — no peers prop (the page doesn't fetch peers itself),
  // so the hook fetches instances internally.
  const sync = useSyncIntegrity('mediaCollection');

  const refresh = async () => {
    setLoading(true);
    // The collections leg resolves to an array on success and to an
    // `{ error }` envelope on failure, so the failure survives `Promise.all`
    // as data instead of being flattened into an indistinguishable `[]`.
    // `silent: true` because this page owns the failure UI below — the shared
    // `request()` toast would fade and leave the banner as the only signal.
    const [cols, mediaSummaries] = await Promise.all([
      listMediaCollections({ silent: true }).then(
        (list) => (Array.isArray(list) ? list : []),
        (err) => ({ error: err?.message || 'The collections list could not be loaded.' }),
      ),
      listGalleryCollectionSummaries({ silent: true }).catch(() => null),
    ]);
    if (Array.isArray(cols)) {
      setCollections(cols);
      setCollectionsError(null);
    } else {
      // Deliberately leave `collections` alone: the sentinel on a first-load
      // failure, or the last good list on a refresh failure. Either way the
      // grid never claims the library is unfiled.
      setCollectionsError(cols.error);
    }
    if (mediaSummaries) setSummaries(mediaSummaries);
    else setCollectionsError('Could not load collection covers and counts.');
    setLoading(false);
  };
  useEffect(() => { refresh(); }, []);

  const handleCreate = async (e) => {
    e.preventDefault();
    const trimmed = name.trim();
    if (!trimmed) return;
    setCreating(true);
    const created = await createMediaCollection({ name: trimmed }, { silent: true }).catch((err) => {
      toast.error(err.message || 'Failed to create');
      return null;
    });
    setCreating(false);
    if (created) {
      // Only extend a list we actually have — fabricating one from a single
      // record while the fetch is still failed would clear the sentinel and
      // resurrect the "everything is unsorted" lie.
      setCollections((prev) => (prev ? [...prev, created] : prev));
      setName('');
      toast.success(`Created "${created.name}"`);
      // Open the new collection instead of dropping the user back on the grid.
      // A new collection is always empty, so any filter state that hides
      // empties — or an active search it doesn't match — would swallow it and
      // make the create read as broken. Navigating is the one feedback that
      // holds regardless of the filter row, and an empty bucket exists to be
      // filled anyway.
      // Drop any pending query→URL mirror write first — it would otherwise be
      // free to fire after this navigation and clobber the detail route back
      // to the list route (#8187).
      cancelQueryMirror();
      navigate(`/media/collections/${encodeURIComponent(created.id)}`);
    }
  };

  const handleDelete = async (collection) => {
    setCollections((prev) => (prev ? prev.filter((c) => c.id !== collection.id) : prev));
    const deleted = await deleteMediaCollection(collection.id, { silent: true }).then(() => true).catch((err) => {
      toast.error(err.message || 'Delete failed');
      refresh();
      return false;
    });
    if (!deleted) return;
    // Removing the last membership makes its media Unsorted. Counts and covers
    // are server-owned now, so update that projection after the delete commits.
    const nextSummaries = await listGalleryCollectionSummaries({ silent: true }).catch(() => null);
    if (nextSummaries) setSummaries(nextSummaries);
    else setCollectionsError('Could not load collection covers and counts.');
  };

  const unsorted = useMemo(() => ({ ...buildUnsortedCollection([], [], []),
    ...(summaries.find(s => s.id === 'unsorted') || {}),
  }), [summaries]);

  const enriched = useMemo(() => {
    if (!collections) return [];
    // Pinned synthetic "Unsorted" entry first, then real collections.
    const all = [unsorted, ...collections];
    return all.map((c) => {
      const summary = summaries.find(s => s.id === c.id);
      const counts = summary?.counts || (c.items || []).reduce((counts, item) => ({ ...counts, [item.kind]: counts[item.kind] + 1 }), { image: 0, video: 0 });
      // Lift the shared auto-creator prefix ("Creative Director: ") out of the
      // title into its own badge, then middle-truncate what's left — otherwise
      // every auto-generated card renders the same clipped prefix and the
      // trailing project name/date that tells them apart is what gets cut.
      const { badge, title } = splitCollectionName(c.name);
      return {
        ...c,
        cover: summary?.cover,
        itemCount: summary?.total ?? (c.items || []).length,
        counts,
        badge,
        displayTitle: middleTruncate(title, TITLE_MAX_CHARS),
      };
    });
  }, [collections, unsorted, summaries]);

  // Filter/sort state lives in the URL so a filtered grid is shareable and
  // survives a back-navigation from a collection. `empty=1` OPTS IN to showing
  // empty collections — the default (param absent) hides them, because an empty
  // auto-created collection has nothing to open.
  const sort = normalizeCollectionSort(searchParams.get('sort'));
  const hideEmpty = searchParams.get('empty') !== '1';

  // Two-stage search (same shape as Catalog): `query` is what the user is
  // typing and drives the client-side filter instantly; `mirroredQuery` is the
  // debounced copy written to the URL, so a shareable `?q=` doesn't cost a
  // history write + full route re-render per keystroke.
  const [query, setQuery] = useState(() => searchParams.get('q') || '');
  const [mirroredQuery, setMirroredQuery] = useState(() => searchParams.get('q') || '');
  // Cancelable so a route change (opening the just-created collection) can
  // drop a still-pending mirror write before it fires — otherwise the stale
  // write can land after the navigation and clobber it back to the list
  // route (#8187). React's own unmount cleanup isn't enough: it runs
  // asynchronously relative to the `navigate()` call in `handleCreate`, and
  // under load the gap can outlast the 300ms debounce.
  const [scheduleQueryMirror, cancelQueryMirror] = useCancelableDebounce();
  useEffect(() => {
    scheduleQueryMirror(() => {
      const trimmed = query.trim();
      setMirroredQuery(trimmed);
      updateParams({ q: trimmed }, { replace: true });
    }, 300);
  }, [query, updateParams, scheduleQueryMirror]);

  // Adopt an externally-changed `?q=` (Back/Forward, or a ⌘K/voice link while
  // this page stays mounted). Our own debounced write lands `urlQuery ===
  // mirroredQuery`, so this is a no-op for typing and only fires on a real
  // external change.
  const urlQuery = searchParams.get('q') || '';
  useEffect(() => {
    if (urlQuery !== mirroredQuery) {
      setQuery(urlQuery);
      setMirroredQuery(urlQuery);
    }
  }, [urlQuery, mirroredQuery]);

  // Search + sort first, then apply "hide empty" as a slice of that result, so
  // the "N empty hidden" count only ever names rows the toggle removed — not
  // rows the search already dropped.
  const matching = useMemo(
    () => applyCollectionView(enriched, { query, sort }),
    [enriched, query, sort],
  );
  const visible = useMemo(
    () => (hideEmpty ? matching.filter((c) => collectionItemCount(c) > 0) : matching),
    [matching, hideEmpty],
  );
  const hiddenEmptyCount = matching.length - visible.length;

  return (
    <div className="space-y-4">
      <form onSubmit={handleCreate} className="flex gap-2 items-center">
        <input
          ref={nameInputRef}
          type="text"
          value={name}
          onChange={(e) => setName(e.target.value)}
          maxLength={80}
          aria-label="New collection name"
          placeholder="New collection name"
          className="flex-1 bg-port-card border border-port-border rounded-lg px-3 py-2 text-sm text-white focus:outline-none focus:border-port-accent"
        />
        <button
          type="submit"
          disabled={creating || !name.trim()}
          className="px-3 py-2 bg-port-accent/20 hover:bg-port-accent/40 text-port-accent rounded-lg text-sm flex items-center gap-1.5 disabled:opacity-40"
        >
          <Plus className="w-4 h-4" /> Create
        </button>
      </form>

      {/* Filter row. Stacks under `sm` so the search box keeps a usable width
          on a phone instead of sharing one line with the sort + toggle. */}
      <div className="flex flex-col sm:flex-row gap-2 sm:items-center">
        <div className="relative flex-1">
          <Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" aria-hidden="true" />
          <label htmlFor="collection-search" className="sr-only">Search collections</label>
          <input
            id="collection-search"
            type="search"
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            placeholder="Search collections…"
            className="w-full pl-9 pr-3 py-2 bg-port-card border border-port-border rounded-lg text-white text-sm focus:outline-none focus:border-port-accent"
          />
        </div>
        <label htmlFor="collection-sort" className="sr-only">Sort collections</label>
        <select
          id="collection-sort"
          value={sort}
          onChange={(e) => updateParams({ sort: e.target.value === DEFAULT_COLLECTION_SORT ? '' : e.target.value })}
          className="bg-port-card border border-port-border rounded-lg px-3 py-2 text-sm text-white focus:outline-none focus:border-port-accent"
        >
          {COLLECTION_SORTS.map((s) => (
            <option key={s.id} value={s.id}>{s.label}</option>
          ))}
        </select>
        <label htmlFor="collection-hide-empty" className="flex items-center gap-2 text-sm text-gray-400 px-1 py-2 cursor-pointer whitespace-nowrap">
          <input
            id="collection-hide-empty"
            type="checkbox"
            checked={hideEmpty}
            onChange={(e) => updateParams({ empty: e.target.checked ? '' : '1' })}
            className="accent-port-accent"
          />
          Hide empty
        </label>
      </div>

      {collectionsError && (
        <Banner
          tone="error"
          size="md"
          icon={AlertTriangle}
          title="Couldn't load collections"
          actions={(
            <button
              type="button"
              onClick={refresh}
              disabled={loading}
              className="px-2.5 py-1 bg-port-error/20 hover:bg-port-error/40 text-port-error rounded text-xs disabled:opacity-40"
            >
              Retry
            </button>
          )}
        >
          <div className="break-words">{collectionsError}</div>
          {/* A read failed — say so, or the missing grid reads as data loss. */}
          <div className="opacity-80">Nothing was deleted; the list just couldn't be read.</div>
        </Banner>
      )}

      {loading ? (
        <PageSkeleton header="none" label="Loading collections" cards={4} sidebar={false} />
      ) : collections ? (
        <div className="space-y-2">
          {/* Precedence chain, not independent gates. First run wins only when
              no search is active — gated on the REAL collections, since the
              synthetic "Unsorted" entry is always prepended and an
              `enriched.length === 0` test would never fire. Otherwise the
              hidden-empties notice and the empty-result panel render TOGETHER,
              so the panel's "use Show above" can never point at a link that
              isn't on screen. The grid is independent of both — a fresh install
              with loose media shows the onboarding copy AND its Unsorted card. */}
          {collections.length === 0 && !query ? (
            <EmptyState
              icon={FolderOpen}
              title="No collections yet"
              message="Group images and videos into a collection — name one above, or use the folder icon on any image/video card."
              actionLabel="Name your first collection"
              onAction={() => nameInputRef.current?.focus()}
            />
          ) : (
            <>
              {hiddenEmptyCount > 0 && (
                <div className="text-xs text-gray-500">
                  {hiddenEmptyCount} empty {hiddenEmptyCount === 1 ? 'collection' : 'collections'} hidden.{' '}
                  <button type="button" onClick={() => updateParams({ empty: '1' })} className="text-port-accent hover:underline">
                    Show
                  </button>
                </div>
              )}
              {visible.length === 0 && (
                <div className="text-gray-500 text-sm bg-port-card border border-port-border rounded-lg p-6 text-center">
                  {/* "No match" is only true when the search itself came up
                      empty. When it matched rows that "Hide empty" then
                      removed, saying that would contradict the "N empty hidden
                      — Show" line above and send the user hunting for a
                      collection that does exist. */}
                  {matching.length === 0
                    ? 'No collections match that search.'
                    : 'Every collection here is empty — use Show above to see them.'}
                </div>
              )}
            </>
          )}
          {visible.length > 0 && (
            <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-3">
              {visible.map((c) => (
                <div
                  key={c.id}
                  className={`bg-port-card border rounded-xl overflow-hidden flex flex-col ${c.synthetic ? 'border-port-accent/40' : 'border-port-border'}`}
                >
                  <Link
                    to={`/media/collections/${encodeURIComponent(c.id)}`}
                    className="block aspect-square bg-port-bg relative"
                  >
                    {c.cover ? (
                      // A cover can point at a pruned video thumbnail or an unpulled
                      // peer asset; MediaImage degrades to the syncing tile instead
                      // of the browser's broken-image icon.
                      <MediaImage src={c.cover} alt={c.name} className="w-full h-full object-cover" loading="lazy" />
                    ) : (
                      <div className="w-full h-full flex items-center justify-center text-gray-600">
                        {c.synthetic ? <Inbox className="w-12 h-12" /> : <FolderOpen className="w-12 h-12" />}
                      </div>
                    )}
                  </Link>
                  <div className="p-2 space-y-1.5 flex-1 flex flex-col">
                    {c.badge && (
                      <span className="inline-block self-start text-[9px] uppercase tracking-wide text-gray-400 bg-port-bg border border-port-border rounded px-1.5 py-0.5">
                        {c.badge}
                      </span>
                    )}
                    <Link to={`/media/collections/${encodeURIComponent(c.id)}`} className="text-sm text-white hover:text-port-accent break-words" title={c.name}>
                      {c.displayTitle}
                    </Link>
                    <div className="flex items-center gap-2 text-[10px] text-gray-500">
                      {c.counts.image > 0 && <span className="flex items-center gap-1"><ImageIcon className="w-3 h-3" />{c.counts.image}</span>}
                      {c.counts.video > 0 && <span className="flex items-center gap-1"><Film className="w-3 h-3" />{c.counts.video}</span>}
                      {c.counts.image === 0 && c.counts.video === 0 && <span>Empty</span>}
                    </div>
                    <div className="flex-1" />
                    <div className="flex items-center justify-between gap-1">
                      {!c.synthetic && (
                        <SyncBadge
                          status={syncBadgeStatus(sync, c.id)}
                          onClick={() => navigate(`/media/collections/${encodeURIComponent(c.id)}/sync`)}
                        />
                      )}
                      {!c.synthetic && (
                        <button
                          type="button"
                          onClick={() => handleDelete(c)}
                          className="px-1.5 py-1 bg-port-error/20 hover:bg-port-error/40 text-port-error text-[10px] rounded flex items-center gap-1"
                          title="Delete collection" aria-label="Delete collection"
                        >
                          <Trash2 className="w-3 h-3" />
                        </button>
                      )}
                    </div>
                  </div>
                </div>
              ))}
            </div>
          )}
        </div>
      ) : null}
    </div>
  );
}