Commit 3c314f82 by PLN (Algolia)

404: ctrl+h lifts the curtain on the running sketch's code, click opens the full…

404: ctrl+h lifts the curtain on the running sketch's code, click opens the full work; second-sketch retry before the photo fallback
parent c0cdb4db
......@@ -32,6 +32,7 @@ async function loadSketches() {
const sketch = await resolveHydraSketch(data.source);
if (sketch) {
out.push({
id: f.replace(/\.md$/, ''),
title: sketch.credits?.title || data.title || f.replace(/\.md$/, ''),
source: withLocalAssets(sketch.source, manifest[f.replace(/\.md$/, '')] ?? {}),
});
......
'use client';
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
/** The 404 canvas: boots one real StarryNights sketch per visit (embedded
* at build by app/not-found.js). Cinema: the theme switcher sits out, and
* if hydra or WebGL can't run, the piece degrades to a random NASA star
* photo — the room is never black. */
* if hydra or WebGL can't run, a second sketch gets its chance before the
* piece degrades to a random NASA star photo — the room is never black.
* ctrl+h lifts the curtain on the running sketch's code; clicking the code
* opens the full work on /hydra/<id>. */
function sanitize(source) {
return source
......@@ -17,6 +19,9 @@ function sanitize(source) {
export default function NotFoundHydra({ sketches }) {
const canvasRef = useRef(null);
const wrapRef = useRef(null);
const bootedRef = useRef([]);
const [codeOpen, setCodeOpen] = useState(false);
const [current, setCurrent] = useState(null);
useEffect(() => {
if (!canvasRef.current || !sketches?.length) return;
......@@ -26,42 +31,85 @@ export default function NotFoundHydra({ sketches }) {
if (pill) pill.style.display = 'none';
const star = `/images/404/star-${String(1 + Math.floor(Math.random() * 31)).padStart(2, '0')}.jpg`;
let instance = null;
import('hydra-synth')
.then((HydraModule) => {
const canvas = canvasRef.current;
if (!canvas) return;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const boot = (piece) => {
const canvas = canvasRef.current;
if (!canvas) return false;
try {
instance = new HydraModule.default({
canvas,
detectAudio: false,
width: canvas.width,
height: canvas.height,
});
const piece = sketches[Math.floor(Math.random() * sketches.length)];
instance.eval(sanitize(piece.source));
if (piece.title) {
const credit = document.createElement('div');
credit.textContent = `hydra · ${piece.title}`;
credit.setAttribute('aria-hidden', 'true');
credit.style.cssText =
'position:fixed;right:1rem;bottom:0.9rem;z-index:1;font-size:0.75rem;opacity:0.55;color:var(--text-muted)';
wrapRef.current?.appendChild(credit);
bootedRef.current.push(piece);
setCurrent(piece);
return true;
} catch (ex) {
console.error(`404 sketch "${piece.title}" failed to boot:`, ex);
if (instance && typeof instance.close === 'function') instance.close();
instance = null;
return false;
}
};
let HydraModule = null;
import('hydra-synth')
.then((Hydra) => {
HydraModule = Hydra;
const canvas = canvasRef.current;
if (!canvas) return;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// two sketches get their chance before the raw photo falls
for (let attempt = 0; attempt < 2; attempt++) {
const remaining = sketches.filter((s) => !bootedRef.current.includes(s));
const pool = remaining.length > 0 ? remaining : sketches;
const piece = pool[Math.floor(Math.random() * pool.length)];
if (boot(piece)) {
const credit = document.createElement('div');
credit.textContent = `hydra · ${piece.title} · ctrl+h = code`;
credit.setAttribute('aria-hidden', 'true');
credit.style.cssText =
'position:fixed;right:1rem;bottom:0.9rem;z-index:1;font-size:0.75rem;opacity:0.55;color:var(--text-muted)';
wrapRef.current?.appendChild(credit);
break;
}
}
if (!instance && wrapRef.current) {
// no WebGL / no hydra at all? the raw photo still falls — cinema never black
wrapRef.current.style.background = `#0a0a0a url(${star}) center / cover no-repeat`;
}
})
.catch((ex) => {
// no WebGL / no hydra? the raw photo still falls — cinema never black
console.error('devname 404 sketch:', ex);
console.error('hydra-synth import failed:', ex);
if (wrapRef.current) {
wrapRef.current.style.background = `#0a0a0a url(${star}) center / cover no-repeat`;
}
});
const onKey = (e) => {
if (e.ctrlKey && !e.metaKey && !e.altKey && e.key.toLowerCase() === 'h') {
e.preventDefault();
setCodeOpen((open) => !open);
}
};
window.addEventListener('keydown', onKey);
return () => {
window.removeEventListener('keydown', onKey);
if (instance && typeof instance.close === 'function') instance.close();
if (pill && prev !== undefined) pill.style.display = prev;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sketches]);
const openWork = (e) => {
e.preventDefault();
e.stopPropagation(); // the whole 404 is a link home; the code overlay is not
if (current?.id) window.location.assign(`/hydra/${current.id}`);
};
return (
<div ref={wrapRef} style={{ position: 'fixed', inset: 0, zIndex: 0 }}>
<canvas
......@@ -69,6 +117,49 @@ export default function NotFoundHydra({ sketches }) {
aria-hidden="true"
style={{ position: 'fixed', inset: 0, width: '100vw', height: '100vh', zIndex: 0 }}
/>
{codeOpen && current && (
<div
onClick={openWork}
role="button"
tabIndex={0}
aria-label={`Ouvrir ${current.title} — ${current.id}`}
style={{
position: 'fixed',
inset: 0,
zIndex: 3,
overflow: 'auto',
background: 'rgba(8, 8, 12, 0.88)',
backdropFilter: 'blur(2px)',
color: 'var(--text-primary, #e8e8f0)',
cursor: 'pointer',
padding: 'clamp(1rem, 4vw, 3rem)',
}}
>
<p
style={{
fontSize: '0.8rem',
opacity: 0.7,
marginBottom: '0.75rem',
fontFamily: 'var(--font-mono, monospace)',
}}
>
{current.title} — cliquer pour la pièce complète (/hydra/{current.id}) · ctrl+h pour
replier
</p>
<pre
style={{
margin: 0,
fontSize: '0.8rem',
lineHeight: 1.55,
whiteSpace: 'pre-wrap',
wordBreak: 'break-word',
fontFamily: 'var(--font-mono, monospace)',
}}
>
{current.source}
</pre>
</div>
)}
</div>
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment