()
| 27 | ); |
| 28 | |
| 29 | const AlgorithmExplorer: React.FC = () => { |
| 30 | const { t, i18n } = useTranslation(); |
| 31 | const orderedDetails = getOrderedAlgorithmDetails(); |
| 32 | const [translatedDetails, setTranslatedDetails] = useState<AlgorithmDetail[]>([]); |
| 33 | |
| 34 | useEffect(() => { |
| 35 | setTranslatedDetails(getTranslatedAlgorithmDetails()); |
| 36 | }, [i18n.language]); |
| 37 | |
| 38 | const location = useLocation(); |
| 39 | const navigate = useNavigate(); |
| 40 | const initialHadAlgoParamRef = useRef<boolean>(new URLSearchParams(location.search).has("algo")); |
| 41 | const userInteractedRef = useRef<boolean>(false); |
| 42 | // Parse query param for deep link (?algo=ID) |
| 43 | const searchParams = new URLSearchParams(location.search); |
| 44 | const initialParam = parseInt(searchParams.get("algo") || "", 10); |
| 45 | const initial = orderedDetails.some((a) => a.id === initialParam) ? initialParam : (orderedDetails[0]?.id ?? 1); |
| 46 | const [activeId, setActiveId] = useState<number>(initial); |
| 47 | // Keep the URL in sync with the selected algorithm (replace to avoid adding a history entry per click) |
| 48 | useEffect(() => { |
| 49 | if (!initialHadAlgoParamRef.current && !userInteractedRef.current) return; |
| 50 | const sp = new URLSearchParams(location.search); |
| 51 | const current = sp.get("algo"); |
| 52 | if (String(activeId) !== current) { |
| 53 | sp.set("algo", String(activeId)); |
| 54 | navigate({ pathname: location.pathname, search: sp.toString() }, { replace: true }); |
| 55 | } |
| 56 | }, [activeId]); |
| 57 | |
| 58 | const shouldNoindex = new URLSearchParams(location.search).size > 0; |
| 59 | |
| 60 | const active = translatedDetails.find((a) => a.id === activeId) || translatedDetails[0] || orderedDetails[0]; |
| 61 | |
| 62 | const baseImgRef = useRef<HTMLImageElement | null>(null); |
| 63 | const baseWorkRef = useRef<{ width: number; height: number; srcData: Uint8ClampedArray } | null>(null); |
| 64 | const [baseLoaded, setBaseLoaded] = useState(false); |
| 65 | const [examples, setExamples] = useState<Record<number, ExampleSet>>({}); |
| 66 | const [basePreview, setBasePreview] = useState<string | null>(null); |
| 67 | |
| 68 | const synthesizeBase = () => { |
| 69 | const w = WORKING_WIDTH; |
| 70 | const h = Math.round((WORKING_WIDTH * 3) / 4); |
| 71 | const c = document.createElement("canvas"); |
| 72 | c.width = w; |
| 73 | c.height = h; |
| 74 | const ctx = c.getContext("2d"); |
| 75 | if (!ctx) return; |
| 76 | const grd = ctx.createLinearGradient(0, 0, w, h); |
| 77 | grd.addColorStop(0, "#000"); |
| 78 | grd.addColorStop(1, "#fff"); |
| 79 | ctx.fillStyle = grd; |
| 80 | ctx.fillRect(0, 0, w, h); |
| 81 | const rg = ctx.createRadialGradient(w * 0.7, h * 0.3, 10, w * 0.7, h * 0.3, w * 0.8); |
| 82 | rg.addColorStop(0, "rgba(255,255,255,0.6)"); |
| 83 | rg.addColorStop(1, "rgba(0,0,0,0)"); |
| 84 | ctx.fillStyle = rg; |
| 85 | ctx.fillRect(0, 0, w, h); |
| 86 | const id = ctx.getImageData(0, 0, w, h); |
nothing calls this directly
no test coverage detected