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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* CatalogAlbum — a collapsible group section in the Catalog "Albums" view (#1762).
*
* One album per live universe, plus the pinned "Unsorted / Raw" and "Orphaned"
* albums. Cards lazy-load via `loadPage(offset)` the FIRST time the album is
* expanded (album headers show the count from /facets up front, so an unexpanded
* album costs nothing), and paginate with "Load more" so an album with more than
* one page's worth of ingredients is never silently truncated. Reuses
* <CatalogCard> so album cards match the flat grid.
*/
import { useState, useEffect, useCallback } from 'react';
import { ChevronRight, ChevronDown, Loader2 } from 'lucide-react';
import BrailleSpinner from '../BrailleSpinner';
import CatalogCard from './CatalogCard';
export default function CatalogAlbum({
title,
subtitle,
count,
pageSize,
defaultExpanded = false,
loadPage,
getType,
selectedIds,
onToggleSelect,
armedId,
onArm,
onCancelArm,
onConfirmDelete,
}) {
const [expanded, setExpanded] = useState(defaultExpanded);
// `null` = not yet loaded (distinct from `[]` = loaded-and-empty), so an
// empty album doesn't re-fetch on every expand toggle.
const [items, setItems] = useState(null);
const [hasMore, setHasMore] = useState(false);
const [loading, setLoading] = useState(false);
const [loadingMore, setLoadingMore] = useState(false);
const [error, setError] = useState('');
// `attempted` gates the auto-load so a FAILED first load (which leaves items
// null + loading false) doesn't make the expand effect retry in a tight loop.
// Reset by Retry; a remount (filter/data change) starts fresh.
const [attempted, setAttempted] = useState(false);
const fetchPage = useCallback(async (offset) => {
const list = await loadPage(offset).catch((err) => {
setError(err?.message || 'Failed to load album');
return null;
});
if (!Array.isArray(list)) return null;
setHasMore(list.length === pageSize);
return list;
}, [loadPage, pageSize]);
const ensureLoaded = useCallback(async () => {
if (items !== null || loading || attempted) return;
setAttempted(true);
setLoading(true);
setError('');
const list = await fetchPage(0);
setLoading(false);
if (list) setItems(list);
}, [items, loading, attempted, fetchPage]);
// Retry after a failed load: clear the attempt gate + error so the expand
// effect re-runs ensureLoaded.
const retry = () => {
setError('');
setAttempted(false);
};
// Load the first time the album is expanded — covers both an explicit toggle
// and a `defaultExpanded` album that mounts already open (the pinned Raw album).
useEffect(() => {
if (expanded) ensureLoaded();
}, [expanded, ensureLoaded]);
const toggle = () => setExpanded((e) => !e);
const Chevron = expanded ? ChevronDown : ChevronRight;
const loadMore = async () => {
setLoadingMore(true);
const list = await fetchPage((items || []).length);
setLoadingMore(false);
if (list) setItems((prev) => [...(prev || []), ...list]);
};
// Album-local optimistic delete: remove the card immediately, restore it if
// the parent's delete handler reports failure (it owns the toast + rollback
// of shared state like selection + facet counts).
const handleDelete = async (it) => {
const idx = (items || []).findIndex((x) => x.id === it.id);
setItems((prev) => (prev || []).filter((x) => x.id !== it.id));
const ok = await onConfirmDelete(it);
if (!ok) {
setItems((prev) => {
if ((prev || []).some((x) => x.id === it.id)) return prev;
const next = [...(prev || [])];
next.splice(Math.max(0, idx), 0, it);
return next;
});
}
};
return (
<section className="border border-port-border rounded-lg overflow-hidden bg-port-card/40">
{/* On a phone the subtitle drops to its own line under the title rather
than competing with it for a ~200px slot; the chevron and count stay
pinned so the row reads the same at every width. */}
<button
type="button"
onClick={toggle}
aria-expanded={expanded}
className="w-full flex items-start sm:items-center gap-2 px-3 sm:px-4 py-3 text-left hover:bg-port-bg/40"
>
<Chevron size={16} className="shrink-0 mt-0.5 sm:mt-0 text-gray-400" />
<span className="min-w-0 flex-1 flex flex-col sm:flex-row sm:items-center sm:gap-2">
<span className="text-white font-medium truncate">{title}</span>
{subtitle ? <span className="text-xs text-gray-500 sm:truncate">{subtitle}</span> : null}
</span>
<span className="shrink-0 text-xs text-gray-400 px-2 py-0.5 rounded-full border border-port-border">
{count}
</span>
</button>
{expanded && (
<div className="px-3 sm:px-4 pb-3 sm:pb-4">
{loading ? (
<div className="text-sm py-3">
<BrailleSpinner text="Loading…" />
</div>
) : error ? (
<div className="flex items-center gap-3 py-3">
<span className="text-sm text-port-error">{error}</span>
<button
type="button"
onClick={retry}
className="text-xs px-2 py-1 rounded border border-port-border text-gray-300 hover:text-white"
>
Retry
</button>
</div>
) : (items && items.length > 0) ? (
<>
<ul className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
{items.map((it) => (
<CatalogCard
key={it.id}
ingredient={it}
getType={getType}
selected={selectedIds.has(it.id)}
onToggleSelect={onToggleSelect}
armed={armedId === it.id}
onArm={onArm}
onCancelArm={onCancelArm}
onConfirmDelete={handleDelete}
/>
))}
</ul>
{hasMore && (
<div className="flex justify-center mt-3">
<button
type="button"
onClick={loadMore}
disabled={loadingMore}
className="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-port-border bg-port-card hover:bg-port-bg text-white text-sm font-medium disabled:opacity-50"
>
{loadingMore ? <Loader2 size={14} className="animate-spin" /> : null}
Load more
</button>
</div>
)}
</>
) : (
<div className="text-sm text-gray-500 py-3">No ingredients in this album.</div>
)}
</div>
)}
</section>
);
}