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

4.7 KB
Raw Blame
// Picker over sprites that already have a locked main reference (#sprite-i2i):
// the "select an existing reference sprite" source for seeding a new main via
// image+text→image. Fetches GET /sprites/reference-sources on open, shows a
// searchable grid of reference thumbnails, and calls onSelect({ id, name, path })
// then closes. `excludeId` drops the sprite being edited (seeding a main from
// its own — not-yet-locked — reference is nonsensical, and once locked it can't
// be regenerated anyway).

import { useEffect, useMemo, useState } from 'react';
import { Search, X, RefreshCw } from 'lucide-react';
import Modal from '../ui/Modal';
import ListLoadError from '../ui/ListLoadError';
import useListResource from '../../hooks/useListResource';
import SpritePreview from './SpritePreview.jsx';
import { listSpriteReferenceSources } from '../../services/apiSprites.js';

const fetchSources = () => listSpriteReferenceSources({ silent: true });

export default function SpriteReferencePicker({ open, onClose, onSelect, excludeId }) {
  const [query, setQuery] = useState('');
  // Refetch each open so a sprite whose main was just locked shows up.
  const { items, state, error, retry } = useListResource(fetchSources, { enabled: open });

  // Reset the search on close so a re-open starts clean.
  useEffect(() => { if (!open) setQuery(''); }, [open]);

  const filtered = useMemo(() => {
    const pool = items.filter((it) => it.id !== excludeId);
    const q = query.trim().toLowerCase();
    if (!q) return pool;
    return pool.filter((it) => `${it.name} ${it.id}`.toLowerCase().includes(q));
  }, [items, query, excludeId]);

  return (
    <Modal
      open={open}
      onClose={onClose}
      size="3xl"
      usePortal
      panelClassName="bg-port-card border border-port-border rounded-xl flex flex-col"
      ariaLabel="Pick a reference sprite"
    >
      <div className="flex items-center justify-between gap-3 p-3 border-b border-port-border">
        <h2 className="text-sm font-medium text-white shrink-0">Pick a reference sprite</h2>
        <div className="relative flex-1 max-w-md">
          <Search className="absolute left-2 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-gray-500" />
          <input
            type="text"
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            placeholder="Search by name or id…"
            aria-label="Search reference sprites"
            className="w-full pl-7 pr-7 py-1.5 text-xs bg-port-bg border border-port-border rounded-lg text-white placeholder-gray-500 focus:outline-none focus:border-port-accent"
            autoFocus
          />
          {query && (
            <button
              type="button"
              onClick={() => setQuery('')}
              className="absolute right-2 top-1/2 -translate-y-1/2 text-gray-500 hover:text-white"
              aria-label="Clear search"
            >
              <X className="w-3.5 h-3.5" />
            </button>
          )}
        </div>
        <button
          type="button"
          onClick={onClose}
          className="shrink-0 p-1.5 text-gray-400 hover:text-white rounded min-h-[44px] min-w-[44px] flex items-center justify-center"
          aria-label="Close"
        >
          <X className="w-4 h-4" />
        </button>
      </div>

      <div className="flex-1 overflow-y-auto p-3">
        {state === 'loading' ? (
          <div className="flex items-center justify-center gap-2 text-xs text-gray-400 py-10">
            <RefreshCw className="w-4 h-4 animate-spin" /> Loading reference sprites…
          </div>
        ) : state === 'error' ? (
          <ListLoadError what="reference sprites" error={error} onRetry={retry} />
        ) : filtered.length === 0 ? (
          <div className="text-xs text-gray-500 py-10 text-center">
            {items.length === 0
              ? 'No sprites with a locked main reference yet — lock one first.'
              : 'No reference sprites match your search.'}
          </div>
        ) : (
          <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-3">
            {filtered.map((it) => (
              <button
                key={it.id}
                type="button"
                onClick={() => { onSelect?.(it); onClose?.(); }}
                className="text-left bg-port-bg border border-port-border rounded-lg p-2 hover:border-port-accent"
              >
                <SpritePreview recordId={it.id} path={it.path} className="w-full h-28 object-contain mb-1.5" />
                <span className="block text-xs font-medium text-white truncate">{it.name}</span>
                <span className="block text-[10px] text-gray-500 truncate">{it.id}</span>
              </button>
            ))}
          </div>
        )}
      </div>
    </Modal>
  );
}