Files

292 lines
9.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import Image from "next/image";
import Link from "next/link";
import { useEffect, useState } from "react";
import {
ArrowLeft,
Check,
CircleOff,
Eraser,
Hexagon,
RotateCcw,
Sparkles,
} from "lucide-react";
import HoneycombSection, {
HONEYCOMB_DEFAULT_LAYERS,
HONEYCOMB_LOGOS,
PHYSICS_PRESETS,
type HoneycombLayers,
type HoneycombLogoPlacements,
type HoneycombPlacementTool,
} from "@/components/landing/HoneycombSection";
import styles from "./grid-lab.module.css";
const STORAGE_KEY = "hivegrid:playground:honeycomb-grid-lab:original:v2";
const LEGACY_STORAGE_KEY = "hivegrid:playground:honeycomb-grid-lab:v1";
type GridState = {
version: 2;
placements: HoneycombLogoPlacements;
activeCellIds: number[];
layers: HoneycombLayers;
};
const LAYER_OPTIONS: ReadonlyArray<{ key: keyof HoneycombLayers; label: string }> = [
{ key: "innerRing", label: "Innerer Ring" },
{ key: "outerRing", label: "Äußerer Ring" },
{ key: "field", label: "Wabenfeld" },
{ key: "strands", label: "Verbindungen" },
{ key: "packets", label: "Datenpunkte" },
{ key: "glow", label: "Glow" },
];
function createEmptyGridState(): GridState {
return {
version: 2,
placements: {},
activeCellIds: [],
layers: { ...HONEYCOMB_DEFAULT_LAYERS, hub: true },
};
}
function normalizeStoredState(value: unknown): GridState {
const fallback = createEmptyGridState();
if (!value || typeof value !== "object") return fallback;
const candidate = value as Partial<GridState>;
const placements = candidate.placements && typeof candidate.placements === "object"
? Object.fromEntries(
Object.entries(candidate.placements).filter(
([cellId, label]) =>
Number.isInteger(Number(cellId)) &&
Number(cellId) > 1 &&
typeof label === "string" &&
Boolean(HONEYCOMB_LOGOS[label])
)
)
: {};
const activeCellIds = Array.isArray(candidate.activeCellIds)
? candidate.activeCellIds.filter(
(cellId): cellId is number => Number.isInteger(cellId) && cellId > 1
)
: [];
const storedLayers: Partial<HoneycombLayers> =
candidate.layers && typeof candidate.layers === "object" ? candidate.layers : {};
return {
version: 2,
placements,
activeCellIds: [...new Set(activeCellIds)].sort((a, b) => a - b),
layers: {
...HONEYCOMB_DEFAULT_LAYERS,
...Object.fromEntries(
LAYER_OPTIONS.map(({ key }) => [
key,
typeof storedLayers[key] === "boolean"
? storedLayers[key]
: HONEYCOMB_DEFAULT_LAYERS[key],
])
),
hub: true,
} as HoneycombLayers,
};
}
export default function GridLabClient() {
const [gridState, setGridState] = useState<GridState>(createEmptyGridState);
const [placementTool, setPlacementTool] = useState<HoneycombPlacementTool>(null);
const [hydrated, setHydrated] = useState(false);
const [storageAvailable, setStorageAvailable] = useState(true);
useEffect(() => {
try {
const stored = window.localStorage.getItem(STORAGE_KEY);
if (stored) setGridState(normalizeStoredState(JSON.parse(stored)));
window.localStorage.removeItem(LEGACY_STORAGE_KEY);
} catch {
setStorageAvailable(false);
} finally {
setHydrated(true);
}
}, []);
useEffect(() => {
if (!hydrated || !storageAvailable) return;
try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(gridState));
} catch {
setStorageAvailable(false);
}
}, [gridState, hydrated, storageAvailable]);
const updateGrid = (update: (current: GridState) => GridState) => {
setGridState(update);
};
const resetGrid = () => {
if (!window.confirm("Das Grid leeren und auf das mittlere G zurücksetzen?")) return;
setGridState(createEmptyGridState());
setPlacementTool(null);
};
return (
<div className={styles.page}>
<header className={styles.hero}>
<Link href="/playground" className={styles.backLink}>
<ArrowLeft aria-hidden="true" size={16} />
Playground
</Link>
<div className={styles.eyebrow}>
<Hexagon aria-hidden="true" size={15} />
Playground / Grid Lab
</div>
<div className={styles.heroCopy}>
<div>
<h1>Honeycomb Grid-Lab</h1>
<p>
Eine stabile Originalversion zum freien Bestücken. Wähle oben ein Logo aus und klicke
danach auf die gewünschte Wabe. Das Grid startet leer – nur das HiveGrid-G bleibt stehen.
</p>
</div>
<div className={`${styles.saveState} ${!storageAvailable ? styles.saveError : ""}`}>
{storageAvailable ? <Check aria-hidden="true" size={15} /> : <CircleOff aria-hidden="true" size={15} />}
{storageAvailable ? "Automatisch lokal gespeichert" : "Lokaler Speicher nicht verfügbar"}
</div>
</div>
</header>
<aside className={styles.toolDock} aria-label="Grid-Werkzeuge">
<div className={styles.dockSection}>
<span className={styles.dockLabel}>Alle Logos – auswählen und danach im Grid platzieren</span>
<div className={styles.logoRail}>
{Object.entries(HONEYCOMB_LOGOS).map(([label, logo]) => {
const selected = placementTool === label;
return (
<button
key={label}
type="button"
className={`${styles.logoButton} ${selected ? styles.logoButtonActive : ""}`}
aria-label={`${label} platzieren`}
aria-pressed={selected}
title={`${label} platzieren`}
onClick={() => setPlacementTool(selected ? null : label)}
>
<Image
src={logo.src}
alt=""
width={76}
height={30}
sizes="76px"
style={{ borderRadius: logo.cornerRadius }}
/>
<span>{label}</span>
</button>
);
})}
</div>
</div>
<div className={styles.dockFooter}>
<div className={styles.controlStack}>
<span className={styles.dockLabel}>Bearbeiten</span>
<div className={styles.modeButtons}>
<button
type="button"
className={placementTool === null ? styles.toolActive : ""}
onClick={() => setPlacementTool(null)}
aria-pressed={placementTool === null}
>
<Sparkles aria-hidden="true" size={15} />
Wabe an / aus
</button>
<button
type="button"
className={placementTool === "__erase__" ? styles.toolActive : ""}
onClick={() => setPlacementTool("__erase__")}
aria-pressed={placementTool === "__erase__"}
>
<Eraser aria-hidden="true" size={15} />
Logo entfernen
</button>
</div>
</div>
<div className={styles.controlStack}>
<span className={styles.dockLabel}>Sichtbare Ebenen</span>
<div className={styles.layerControls} aria-label="Grid-Ebenen">
{LAYER_OPTIONS.map(({ key, label }) => {
const enabled = gridState.layers[key];
return (
<button
key={key}
type="button"
aria-pressed={enabled}
className={enabled ? styles.layerEnabled : ""}
onClick={() => updateGrid((current) => ({
...current,
layers: { ...current.layers, [key]: !current.layers[key], hub: true },
}))}
>
<span aria-hidden="true" />
{label}
</button>
);
})}
</div>
</div>
<button type="button" className={styles.resetButton} onClick={resetGrid}>
<RotateCcw aria-hidden="true" size={15} />
Grid leeren
</button>
</div>
</aside>
<section className={`${styles.previewShell} ${styles.solid}`} aria-labelledby="active-grid-title">
<div className={styles.previewAtmosphere} aria-hidden="true">
<span />
<span />
<span />
</div>
<div className={styles.previewMeta}>
<div>
<span>Originalversion</span>
<h2 id="active-grid-title">Original Solid</h2>
</div>
<p>
{placementTool === null
? "Waben anklicken, um sie ein- oder auszuschalten. Bereits gesetzte Logos können verschoben werden."
: placementTool === "__erase__"
? "Auf eine belegte Wabe klicken, um das Logo zu entfernen."
: `${placementTool} ist ausgewählt – jetzt die gewünschte Wabe anklicken.`}
</p>
</div>
<div className={styles.gridStage}>
<HoneycombSection
inline
theme="light"
physics={PHYSICS_PRESETS.soft}
appearance="solid"
layers={gridState.layers}
showCellNumbers={false}
showInlineFade={false}
logoPlacements={gridState.placements}
onLogoPlacementsChange={(placements) => updateGrid((current) => ({ ...current, placements }))}
activeCellIds={gridState.activeCellIds}
onActiveCellIdsChange={(activeCellIds) => updateGrid((current) => ({ ...current, activeCellIds }))}
editorMode
placementTool={placementTool}
/>
</div>
</section>
<footer className={styles.labNote}>
<strong>Nur Playground.</strong> Der Zustand dieses Grids wird ausschließlich lokal in deinem Browser gespeichert.
</footer>
</div>
);
}