|
| 1 | +import React, { useState, useEffect } from "react" |
| 2 | +import Form from "react-bootstrap/esm/Form" |
| 3 | +import { convertImplicitOrderedXDToExplicitHeaders, shouldConvertToExplicitHeaders } from "xd-crossword-tools" |
| 4 | + |
| 5 | +const CDN_BASE = "https://puzmo.blob.core.windows.net/xdg-mirror" |
| 6 | + |
| 7 | +type DecodedIndex = Record<string, { name: string; years: Record<string, string[]> }> |
| 8 | + |
| 9 | +let cachedIndex: DecodedIndex | null = null |
| 10 | +let cachedPub = "" |
| 11 | +let cachedYear = "" |
| 12 | + |
| 13 | +function decodeIndex(index: Record<string, [string, string, Record<string, string[]>]>): DecodedIndex { |
| 14 | + const tree: DecodedIndex = {} |
| 15 | + for (const [pub, [prefix, name, yearMap]] of Object.entries(index)) { |
| 16 | + const years: Record<string, string[]> = {} |
| 17 | + for (const [year, entries] of Object.entries(yearMap)) { |
| 18 | + years[year] = |
| 19 | + year === "_" |
| 20 | + ? entries // flat pub, filenames are complete |
| 21 | + : entries.map((e) => `${prefix}${year}-${e}`) // restore stripped prefix+year |
| 22 | + } |
| 23 | + tree[pub] = { name, years } |
| 24 | + } |
| 25 | + return tree |
| 26 | +} |
| 27 | + |
| 28 | +interface CDNBrowserProps { |
| 29 | + onSelect: (xd: string) => void |
| 30 | +} |
| 31 | + |
| 32 | +export function CDNBrowser({ onSelect }: CDNBrowserProps) { |
| 33 | + const [index, setIndex] = useState<DecodedIndex | null>(null) |
| 34 | + const [loading, setLoading] = useState(false) |
| 35 | + const [error, setError] = useState<string | null>(null) |
| 36 | + const [selectedPub, setSelectedPub] = useState(cachedPub) |
| 37 | + const [selectedYear, setSelectedYear] = useState(cachedYear) |
| 38 | + const [loadingFile, setLoadingFile] = useState<string | null>(null) |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (cachedIndex) { |
| 42 | + setIndex(cachedIndex) |
| 43 | + return |
| 44 | + } |
| 45 | + setLoading(true) |
| 46 | + fetch(`${CDN_BASE}/index.json`) |
| 47 | + .then((r) => r.json()) |
| 48 | + .then((raw) => { |
| 49 | + const decoded = decodeIndex(raw) |
| 50 | + cachedIndex = decoded |
| 51 | + setIndex(decoded) |
| 52 | + }) |
| 53 | + .catch((e) => setError(e.message)) |
| 54 | + .finally(() => setLoading(false)) |
| 55 | + }, []) |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if (!index || selectedPub) return |
| 59 | + const firstPub = Object.keys(index)[0] |
| 60 | + if (firstPub) { |
| 61 | + cachedPub = firstPub |
| 62 | + setSelectedPub(firstPub) |
| 63 | + const firstYear = Object.keys(index[firstPub].years)[0] |
| 64 | + if (firstYear) { cachedYear = firstYear; setSelectedYear(firstYear) } |
| 65 | + } |
| 66 | + }, [index]) |
| 67 | + |
| 68 | + const handlePubChange = (pub: string) => { |
| 69 | + cachedPub = pub |
| 70 | + setSelectedPub(pub) |
| 71 | + if (index) { |
| 72 | + const firstYear = Object.keys(index[pub].years)[0] |
| 73 | + cachedYear = firstYear || "" |
| 74 | + setSelectedYear(cachedYear) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const handleFileClick = async (pubKey: string, year: string, filename: string) => { |
| 79 | + setLoadingFile(filename) |
| 80 | + try { |
| 81 | + const path = year === "_" ? `${pubKey}/${filename}.xd` : `${pubKey}/${year}/${filename}.xd` |
| 82 | + const response = await fetch(`${CDN_BASE}/${path}`) |
| 83 | + if (!response.ok) throw new Error(`Failed to fetch: ${response.status}`) |
| 84 | + let text = await response.text() |
| 85 | + if (shouldConvertToExplicitHeaders(text)) text = convertImplicitOrderedXDToExplicitHeaders(text) |
| 86 | + onSelect(text) |
| 87 | + } catch (e) { |
| 88 | + setError(e instanceof Error ? e.message : "Failed to load puzzle") |
| 89 | + } finally { |
| 90 | + setLoadingFile(null) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (loading) return <div className="text-muted">Loading index...</div> |
| 95 | + if (error) return <div className="text-danger">{error}</div> |
| 96 | + if (!index) return null |
| 97 | + |
| 98 | + const pubEntries = Object.entries(index) |
| 99 | + const years = selectedPub ? Object.keys(index[selectedPub].years).sort().reverse() : [] |
| 100 | + const files = selectedPub && selectedYear ? index[selectedPub].years[selectedYear] : [] |
| 101 | + const totalFiles = files.length |
| 102 | + |
| 103 | + return ( |
| 104 | + <div> |
| 105 | + <p className="text-muted small mb-2"> |
| 106 | + Over 6,000 pre-1965 crosswords from the{" "} |
| 107 | + <a href="https://xd.saul.pw/data" target="_blank" rel="noreferrer"> |
| 108 | + gxd |
| 109 | + </a>{" "} |
| 110 | + dataset, mirrored by Puzzmo in March 2026. |
| 111 | + </p> |
| 112 | + <div className="d-flex gap-2 mb-2 flex-wrap align-items-center"> |
| 113 | + <Form.Select |
| 114 | + size="sm" |
| 115 | + value={selectedPub} |
| 116 | + onChange={(e) => handlePubChange(e.target.value)} |
| 117 | + style={{ maxWidth: "220px" }} |
| 118 | + > |
| 119 | + {pubEntries.map(([key, { name }]) => ( |
| 120 | + <option key={key} value={key}> |
| 121 | + {name} |
| 122 | + </option> |
| 123 | + ))} |
| 124 | + </Form.Select> |
| 125 | + <Form.Select |
| 126 | + size="sm" |
| 127 | + value={selectedYear} |
| 128 | + onChange={(e) => { cachedYear = e.target.value; setSelectedYear(e.target.value) }} |
| 129 | + style={{ maxWidth: "120px" }} |
| 130 | + disabled={!selectedPub} |
| 131 | + > |
| 132 | + {years.map((year) => ( |
| 133 | + <option key={year} value={year}> |
| 134 | + {year === "_" ? "All" : year} |
| 135 | + </option> |
| 136 | + ))} |
| 137 | + </Form.Select> |
| 138 | + <span className="text-muted small">{totalFiles} puzzle{totalFiles !== 1 ? "s" : ""}</span> |
| 139 | + </div> |
| 140 | + <div style={{ maxHeight: "300px", overflowY: "auto", display: "grid", gridTemplateColumns: "1fr 1fr", gap: "4px" }}> |
| 141 | + {files.length === 0 ? ( |
| 142 | + <div className="text-muted">No puzzles found</div> |
| 143 | + ) : ( |
| 144 | + files.map((filename) => { |
| 145 | + const dateMatch = filename.match(/(\d{4})-(\d{2})-(\d{2})/) |
| 146 | + const label = dateMatch |
| 147 | + ? new Date(`${dateMatch[1]}-${dateMatch[2]}-${dateMatch[3]}`).toLocaleDateString(undefined, { |
| 148 | + year: "numeric", |
| 149 | + month: "long", |
| 150 | + day: "numeric", |
| 151 | + timeZone: "UTC", |
| 152 | + }) |
| 153 | + : filename |
| 154 | + return ( |
| 155 | + <button |
| 156 | + key={filename} |
| 157 | + className="example-button" |
| 158 | + style={{ textAlign: "left" }} |
| 159 | + onClick={() => handleFileClick(selectedPub, selectedYear, filename)} |
| 160 | + disabled={loadingFile !== null} |
| 161 | + > |
| 162 | + <div className="example-title">{loadingFile === filename ? "Loading..." : label}</div> |
| 163 | + </button> |
| 164 | + ) |
| 165 | + }) |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + ) |
| 170 | +} |
0 commit comments