alpha01 filetti: web app, crossword service and tor batch

This commit is contained in:
2026-06-05 16:22:17 +02:00
parent 47d8957e15
commit 9cb8a5aa8f
29 changed files with 8590 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
"use client";
import { useEffect, useState } from "react";
import { getMockCrosswordResponse } from "@/lib/mock-crossword";
import type { Locale } from "@/lib/i18n";
import type { CrosswordResponse } from "@/lib/types";
type UseCrosswordDataResult = {
crossword: CrosswordResponse | null;
errorMessage: string;
isLoading: boolean;
};
export function useCrosswordData(id: string, locale: Locale): UseCrosswordDataResult {
const [crossword, setCrossword] = useState<CrosswordResponse | null>(
id === "demo-transport" ? getMockCrosswordResponse(locale) : null,
);
const [errorMessage, setErrorMessage] = useState("");
const [isLoading, setIsLoading] = useState(id !== "demo-transport");
useEffect(() => {
if (id === "demo-transport") {
setIsLoading(false);
return;
}
let cancelled = false;
setIsLoading(true);
setErrorMessage("");
fetch(`/api/crosswords/${id}`)
.then(async (response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json();
})
.then((payload: CrosswordResponse) => {
if (!cancelled) {
setCrossword(payload);
setIsLoading(false);
}
})
.catch((error) => {
if (!cancelled) {
setErrorMessage(error instanceof Error ? error.message : "Unexpected error");
setIsLoading(false);
}
});
return () => {
cancelled = true;
};
}, [id, locale]);
return {
crossword,
errorMessage,
isLoading,
};
}