router and enable back button

This commit is contained in:
polv committed 2026-03-15 11:49:20 +07:00
1 parent 41e207afad
commit b1ef987f9d
5 files changed
+105 -40

No files matched your search

+21
View File
@@ -90,6 +90,9 @@ importers:
react-icons: react-icons:
specifier: ^5.5.0 specifier: ^5.5.0
version: 5.5.0(react@19.2.4) version: 5.5.0(react@19.2.4)
react-router:
specifier: ^7.13.1
version: 7.13.1(react-dom@19.2.4(react@19.2.4))(react@19.2.4)
wanakana: wanakana:
specifier: ^5.3.1 specifier: ^5.3.1
version: 5.3.1 version: 5.3.1
@@ -1442,6 +1445,16 @@ packages:
resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==} resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
react-router@7.13.1:
resolution: {integrity: sha512-td+xP4X2/6BJvZoX6xw++A2DdEi++YypA69bJUV5oVvqf6/9/9nNlD70YO1e9d3MyamJEBQFEzk6mbfDYbqrSA==}
engines: {node: '>=20.0.0'}
peerDependencies:
react: '>=18'
react-dom: '>=18'
peerDependenciesMeta:
react-dom:
optional: true
react@19.2.4: react@19.2.4:
resolution: {integrity: sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==} resolution: {integrity: sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@@ -3321,6 +3334,14 @@ snapshots:
react-refresh@0.17.0: {} react-refresh@0.17.0: {}
react-router@7.13.1(react-dom@19.2.4(react@19.2.4))(react@19.2.4):
dependencies:
cookie: 1.1.1
react: 19.2.4
set-cookie-parser: 2.7.2
optionalDependencies:
react-dom: 19.2.4(react@19.2.4)
react@19.2.4: {} react@19.2.4: {}
real-require@0.2.0: {} real-require@0.2.0: {}
+16 -16
View File
@@ -15,7 +15,6 @@ pub struct YomitanRow {
rules: String, rules: String,
score: i64, score: i64,
glossary_json: String, glossary_json: String,
glossary_b: Vec<u8>,
sequence: Option<i64>, sequence: Option<i64>,
term_tags: String, term_tags: String,
dict_title: String, dict_title: String,
@@ -118,26 +117,27 @@ impl YomitanDatabase {
" "
))?; ))?;
let rows = stmt.query_map(params![q_term_norm, q_reading, limit, offset], |r| { let rows = stmt.query_map(params![q_term_norm, q_reading, limit, offset], |r| {
Ok(YomitanRow { Ok((
term: r.get(0)?, YomitanRow {
reading: r.get(1)?, term: r.get(0)?,
def_tags: r.get(2)?, reading: r.get(1)?,
rules: r.get(3)?, def_tags: r.get(2)?,
score: r.get(4)?, rules: r.get(3)?,
glossary_json: String::new(), score: r.get(4)?,
glossary_b: r.get(5)?, glossary_json: String::from("[]"), // a valid JSON array
sequence: r.get(6)?, sequence: r.get(6)?,
term_tags: r.get(7)?, term_tags: r.get(7)?,
dict_title: r.get(8)?, dict_title: r.get(8)?,
}) },
r.get::<_, Vec<u8>>(5)?,
))
})?; })?;
let mut out = Vec::new(); let mut out = Vec::new();
for row in rows { for row in rows {
let mut r = row?; let (mut r, glossary_b) = row?;
r.glossary_json = r.glossary_json =
String::from_utf8(decompressor.decompress(&r.glossary_b, MAX_DECOMPRESSED_SIZE)?)?; String::from_utf8(decompressor.decompress(&glossary_b, MAX_DECOMPRESSED_SIZE)?)?;
r.glossary_b = vec![];
out.push(r); out.push(r);
} }
+1
View File
@@ -21,6 +21,7 @@
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0", "react-dom": "^19.1.0",
"react-icons": "^5.5.0", "react-icons": "^5.5.0",
"react-router": "^7.13.1",
"wanakana": "^5.3.1", "wanakana": "^5.3.1",
"zustand": "^5.0.11" "zustand": "^5.0.11"
}, },
+15 -2
View File
@@ -1,14 +1,27 @@
import "./App.css"; import "./App.css";
import { createBrowserRouter } from "react-router";
import { RouterProvider } from "react-router/dom";
import LoadingDialog from "./components/LoadingDialog"; import LoadingDialog from "./components/LoadingDialog";
import { Provider } from "./components/ui/provider"; import { Provider } from "./components/ui/provider";
import SearchPage from "./pages/SearchPage";
const router = createBrowserRouter([
{
path: "/",
lazy: async () => {
return {
Component: (await import("./pages/SearchPage")).default,
};
},
},
]);
function App() { function App() {
return ( return (
<Provider> <Provider>
<> <>
<SearchPage /> <RouterProvider router={router} />
<LoadingDialog /> <LoadingDialog />
</> </>
</Provider> </Provider>
+52 -22
View File
@@ -5,6 +5,7 @@ import {
useRef, useRef,
useState, useState,
} from "react"; } from "react";
import { useSearchParams } from "react-router";
import { toKana } from "wanakana"; import { toKana } from "wanakana";
import { import {
@@ -35,16 +36,27 @@ interface Entry {
} }
function SearchPage() { function SearchPage() {
const [q, setQ] = useState(""); const [searchParams, set_searchParams] = useSearchParams();
const [entries, setEntries] = useState<Entry[]>([]);
const [searchTimeout, setSearchTimeout] = useState(0); useEffect(() => {
const [furigana, setFurigana] = useState(""); let q = searchParams.get("q") || "";
if (q) set_q(q);
}, [searchParams]);
function searchParams_setQ(q: string) {
if (searchParams.get("q") === q) return;
set_searchParams({ q });
}
const [q, set_q] = useState("");
const [entries, set_entries] = useState<Entry[]>([]);
const [searchTimeout, set_searchTimeout] = useState(0);
const [furigana, set_furigana] = useState("");
const [isAutoKana, set_isAutoKana] = useState(true); const [isAutoKana, set_isAutoKana] = useState(true);
const nSearch = useRef(0); const nSearch = useRef(0);
const searchboxRef = useRef<HTMLInputElement | null>(null); const searchboxRef = useRef<HTMLInputElement | null>(null);
const resultScrollRef = useRef<HTMLElement | null>(null); const resultScrollRef = useRef<HTMLElement | null>(null);
const prevNormQ = useRef("");
const lang = "ja-JP"; const lang = "ja-JP";
@@ -59,7 +71,7 @@ function SearchPage() {
set_clipboardText((clipboardText) => { set_clipboardText((clipboardText) => {
if (clipboardText !== newText) { if (clipboardText !== newText) {
setQ(newText + " "); searchParams_setQ(newText + " ");
return newText; return newText;
} }
return clipboardText; return clipboardText;
@@ -75,7 +87,7 @@ function SearchPage() {
function trySearch() { function trySearch() {
if (!q.trim()) { if (!q.trim()) {
setEntries([]); set_entries([]);
return; return;
} }
@@ -88,7 +100,7 @@ function SearchPage() {
nSearch.current += 1; nSearch.current += 1;
setSearchTimeout( set_searchTimeout(
setTimeout(() => { setTimeout(() => {
if (nSearch.current > 1) runSearch(); if (nSearch.current > 1) runSearch();
}, 250), }, 250),
@@ -103,8 +115,6 @@ function SearchPage() {
norm_q = norm_q.replace(/n$/i, "ん").replace(/[a-z]$/i, ""); norm_q = norm_q.replace(/n$/i, "ん").replace(/[a-z]$/i, "");
} }
if (!norm_q) return; if (!norm_q) return;
if (isNew && prevNormQ.current === norm_q) return;
prevNormQ.current = norm_q;
const ender = /\p{Z}$/u.test(q) ? "" : "*"; const ender = /\p{Z}$/u.test(q) ? "" : "*";
@@ -149,7 +159,7 @@ function SearchPage() {
segs.map((s) => s.surface).join("") + segs.map((s) => s.surface).join("") +
" "; " ";
setQ(newQ); searchParams_setQ(newQ);
return; return;
} }
} }
@@ -158,9 +168,9 @@ function SearchPage() {
resultScrollRef.current.scrollTop = 0; resultScrollRef.current.scrollTop = 0;
} }
setEntries(result); set_entries(result);
} else { } else {
setEntries([...entries, ...result]); set_entries([...entries, ...result]);
} }
return result; return result;
@@ -195,7 +205,7 @@ function SearchPage() {
}); });
} }
setQ(q); set_q(q);
} }
const FURIGANA_REGEX = /^[\p{scx=Hiragana}\p{scx=Katakana}]+$/u; const FURIGANA_REGEX = /^[\p{scx=Hiragana}\p{scx=Katakana}]+$/u;
@@ -205,7 +215,7 @@ function SearchPage() {
data: compositionData, data: compositionData,
}) => { }) => {
if (FURIGANA_REGEX.test(compositionData)) { if (FURIGANA_REGEX.test(compositionData)) {
setFurigana(compositionData); set_furigana(compositionData);
} }
}; };
@@ -237,7 +247,7 @@ function SearchPage() {
.map((p, idx) => (idx & 1 ? `<${p}>[${rt[idx]}]` : p)) .map((p, idx) => (idx & 1 ? `<${p}>[${rt[idx]}]` : p))
.join(""); .join("");
setQ((q) => { set_q((q) => {
if (!(target instanceof HTMLInputElement)) return markup; if (!(target instanceof HTMLInputElement)) return markup;
let { selectionStart } = target; let { selectionStart } = target;
@@ -264,12 +274,24 @@ function SearchPage() {
size={"xs"} size={"xs"}
me={-2} me={-2}
onClick={() => { onClick={() => {
setQ(""); searchParams_setQ(q);
searchParams_setQ("");
set_q("");
searchboxRef.current ? searchboxRef.current.focus() : null; searchboxRef.current ? searchboxRef.current.focus() : null;
}} }}
/> />
) : null; ) : null;
function onTermClicked(t: string) {
searchParams_setQ(q);
searchParams_setQ(t + " ");
}
function JSONdumpClean(o: Record<string, any>) {
const p = Object.entries(o).filter(([, v]) => v || v === 0);
return p.length ? JSON.stringify(Object.fromEntries(p), null, 1) : null;
}
return ( return (
<Stack <Stack
style={{ style={{
@@ -335,10 +357,18 @@ function SearchPage() {
<li key={i} lang={lang} style={{ marginLeft: "1.5em" }}> <li key={i} lang={lang} style={{ marginLeft: "1.5em" }}>
<div> <div>
<Card.Root> <Card.Root>
<Card.Header> <Card.Header flexDirection={"row"} gap={0}>
{term} <a onClick={() => onTermClicked(term + " ")}>{term}</a>
{reading ? ` (${reading})` : ""} 【{dict_title}】 {reading && reading !== term ? (
{JSON.stringify(it, (_, v) => v || undefined)} <>
{"("}
<a onClick={() => onTermClicked(reading + " ")}>
{reading}
</a>
{")"}
</>
) : null}
【{dict_title}】{JSONdumpClean(it)}
</Card.Header> </Card.Header>
<Card.Body> <Card.Body>
<div <div
@@ -352,7 +382,7 @@ function SearchPage() {
<Glossary <Glossary
key={i} key={i}
glossary={g} glossary={g}
onTermClicked={(t) => setQ(t + " ")} onTermClicked={onTermClicked}
/> />
))} ))}
</div> </div>