import { Link, useNavigate, useParams } from "@tanstack/react-router";
import { useEffect, useState } from "react";
import { Live } from "../Live";
import { Capture, Windows, isCaptured } from "../Native";
import { all, chapters, search } from "../data";
import { Find, typing } from "../Find";
import { NotFound } from "./NotFound";
export function Story() {
const { page, story } = useParams({ strict: false });
const navigate = useNavigate();
const [query, setQuery] = useState("");
const chapter = chapters.find((c) => c.slug === page);
const found = chapter?.stories.find((s) => s.slug === story);
const at = all.findIndex((e) => e.story === found);
const [prev, next] = at < 0 ? [] : [all[at - 1], all[at + 1]];
useEffect(() => {
if (chapter && found) document.title = `${found.title} · ${chapter.title} · Ely`;
}, [chapter, found]);
useEffect(() => {
document.querySelector('.side [aria-current="page"]')?.scrollIntoView({ block: "nearest" });
}, [page, story]);
useEffect(() => {
const step = (e: KeyboardEvent) => {
if (e.metaKey || e.ctrlKey || e.altKey || typing(e)) return;
const to = e.key === "ArrowLeft" ? prev : e.key === "ArrowRight" ? next : undefined;
if (to) navigate({ to: "/components/$page/$story/", params: { page: to.chapter.slug, story: to.story.slug } });
};
addEventListener("keydown", step);
return () => removeEventListener("keydown", step);
}, [prev, next, navigate]);
if (!chapter || !found) return