Zum Inhalt

src/stores/thumbnailStore.ts

// src/stores/thumbnailStore.ts
// Reactive, localStorage-backed store of per-preset preview images.
import { createSignal } from 'solid-js';

const KEY = 'physarum:thumbnails';

function load(): Record<string, string> {
  try {
    const raw = localStorage.getItem(KEY);
    if (!raw) return {};
    const parsed: unknown = JSON.parse(raw);
    return typeof parsed === 'object' && parsed !== null ? (parsed as Record<string, string>) : {};
  } catch {
    return {};
  }
}

const [thumbnails, setThumbnails] = createSignal<Record<string, string>>(load());

export { thumbnails };

/** Stores a preview image (data URL) for a preset and persists it. */
export function setThumbnail(presetId: string, dataUrl: string): void {
  const next = { ...thumbnails(), [presetId]: dataUrl };
  setThumbnails(next);
  try {
    localStorage.setItem(KEY, JSON.stringify(next));
  } catch {
    // localStorage quota exceeded — keep the in-memory copy, drop persistence
  }
}

export function getThumbnail(presetId: string): string | undefined {
  return thumbnails()[presetId];
}