11"use client" ;
22
3+ import React from "react" ;
4+
35import { queryCharacters } from "@/lib/api" ;
4- import { buildTableRows , parse廣韻字音 , parse中原音韻字音 , parse東干甘肅話字音 } from "@/lib/dataProcessor" ;
5- import type { CharacterResult , ProcessedLanguage , TableRow } from "@/types" ;
6+ import { buildTableRows , parse特殊語言字音 } from "@/lib/dataProcessor" ;
7+ import { CharacterResultItem , ProcessedLanguage , TableRow , UserSettings } from "@/types" ;
68import { useState , useEffect } from "react" ;
79import { useApp } from "@/contexts/AppContext" ;
810import { getTranslation } from "@/lib/i18n" ;
@@ -31,6 +33,92 @@ const MagnifyingGlassIcon = () => (
3133 </ svg >
3234) ;
3335
36+ const SpecialCharReadingFragments = ( {
37+ parsedList,
38+ } : {
39+ parsedList : { field : string ; fieldValue : string ; lang : string } [ ] ;
40+ } ) => {
41+ return (
42+ < >
43+ { parsedList . map ( ( item , idx ) => (
44+ < React . Fragment key = { idx } >
45+ < span title = { item . field } lang = { item . lang } >
46+ { item . fieldValue }
47+ </ span >
48+ { idx !== parsedList . length - 1 && "/" }
49+ </ React . Fragment >
50+ ) ) }
51+ </ >
52+ ) ;
53+ } ;
54+
55+ const CharReadingBox = ( {
56+ 字音,
57+ langAbbr,
58+ idx,
59+ settings,
60+ } : {
61+ 字音 : CharacterResultItem ;
62+ langAbbr : string ;
63+ idx : number ;
64+ settings : UserSettings ;
65+ } ) => {
66+ const isSpecialLanguage = langAbbr === "廣韻" || langAbbr === "中原音韻" || langAbbr === "東干甘肅話" ;
67+ const is女書 = langAbbr === "江永上江墟" ;
68+
69+ if ( typeof 字音 === "string" ) {
70+ return (
71+ < td
72+ key = { idx }
73+ { ...( isSpecialLanguage ? { } : { lang : "zh-Latn-fonipa" } ) }
74+ className = "border border-border px-2 py-1 text-sm bg-card font-mono break-words overflow-hidden text-foreground"
75+ style = { { width : "192px" , maxWidth : "192px" , minWidth : "192px" } } >
76+ { isSpecialLanguage ? (
77+ < SpecialCharReadingFragments parsedList = { parse特殊語言字音 ( 字音 , settings , langAbbr ) } />
78+ ) : (
79+ 字音
80+ ) }
81+ </ td >
82+ ) ;
83+ }
84+
85+ // 多音字
86+ else if ( Array . isArray ( 字音 ) ) {
87+ return (
88+ < td
89+ key = { idx }
90+ className = "border border-border px-2 py-1 text-sm bg-card font-mono break-words overflow-hidden text-foreground"
91+ style = { { width : "192px" , maxWidth : "192px" , minWidth : "192px" } } >
92+ { 字音 . map ( ( item , idx ) => {
93+ const 音標 = item [ 0 ] ;
94+ const 注釋 = item [ 1 ] ?? null ;
95+
96+ return (
97+ < div key = { idx } className = "mb-1 last:mb-0" >
98+ { isSpecialLanguage ? (
99+ < SpecialCharReadingFragments parsedList = { parse特殊語言字音 ( 音標 , settings , langAbbr ) } />
100+ ) : (
101+ < span lang = "zh-Latn-fonipa" > { 音標 } </ span >
102+ ) }
103+ { 注釋 && (
104+ < span lang = { is女書 ? "zh-Nshu" : "zh-HK" } className = "ml-1 text-xs text-muted-foreground" >
105+ { 注釋 }
106+ </ span >
107+ ) }
108+ </ div >
109+ ) ;
110+ } ) }
111+ </ td >
112+ ) ;
113+ }
114+ } ;
115+
116+ const makeRow = ( row : TableRow , settings : UserSettings ) => {
117+ return row . 字音列表 . map ( ( 字音 : CharacterResultItem , charIdx : number ) => {
118+ return < CharReadingBox key = { charIdx } 字音 = { 字音 } langAbbr = { row . languageAbbr } idx = { charIdx } settings = { settings } /> ;
119+ } ) ;
120+ } ;
121+
34122const Query = ( ) => {
35123 const {
36124 processedLanguages,
@@ -91,9 +179,6 @@ const Query = () => {
91179 }
92180 } , [ contextQueryResults , processedLanguages , settings . selectedLanguages ] ) ;
93181
94- // Extract characters from query results
95- const characters = contextQueryResults ? contextQueryResults . map ( ( [ char ] : CharacterResult ) => char ) : [ ] ;
96-
97182 return (
98183 < div className = "bg-background" >
99184 { /* Query Input Section */ }
@@ -121,7 +206,7 @@ const Query = () => {
121206 </ div >
122207
123208 { /* Results Table Section */ }
124- { tableRows . length > 0 && (
209+ { contextQueryResults !== null && tableRows . length > 0 && (
125210 < div className = "p-4 flex justify-center" >
126211 < div className = "overflow-x-auto shadow-sm" >
127212 < table className = "border-collapse border border-border bg-card" >
@@ -130,7 +215,7 @@ const Query = () => {
130215 < th
131216 className = "border border-border px-2 py-1 text-left text-sm font-bold bg-[#EB0000]"
132217 style = { { width : "128px" , maxWidth : "128px" , minWidth : "128px" } } > </ th >
133- { characters . map ( ( char : string , idx : number ) => (
218+ { ( contextQueryResults [ 0 ] . slice ( 1 ) as string [ ] ) . map ( ( char : string , idx : number ) => (
134219 < th
135220 key = { idx }
136221 className = "border border-border px-2 py-1 text-center text-lg font-bold bg-[#EB0000]"
@@ -163,48 +248,7 @@ const Query = () => {
163248 { row . languageAbbr }
164249 </ span >
165250 </ td >
166- { characters . map ( ( char : string , charIdx : number ) => {
167- let 字音 = row . 字音列表 [ char ] || "—" ;
168- let isHTML = false ;
169- // Special handling for Guangyun (廣韻) data
170- if ( row . languageAbbr === "廣韻" && 字音 !== "—" ) {
171- 字音 = parse廣韻字音 ( 字音 , settings . 廣韻字段 ) ;
172- isHTML = true ; // Guangyun data contains HTML tags
173- }
174-
175- // Special handling for 中原音韻 data
176- if ( row . languageAbbr === "中原音韻" && 字音 !== "—" ) {
177- 字音 = parse中原音韻字音 ( 字音 , settings . 中原音韻字段 ) ;
178- isHTML = true ; // 中原音韻 data contains HTML tags
179- }
180-
181- // Special handling for 東干甘肅話 data
182- if ( row . languageAbbr === "東干甘肅話" && 字音 !== "—" ) {
183- 字音 = parse東干甘肅話字音 ( 字音 , settings . 東干甘肅話字段 ) ;
184- isHTML = true ; // 東干甘肅話 data contains HTML tags
185- }
186-
187- // Render with HTML or plain text
188- if ( isHTML ) {
189- return (
190- < td
191- key = { `char-${ charIdx } ` }
192- className = "border border-border px-2 py-1 text-sm bg-card font-mono break-words overflow-hidden text-foreground"
193- style = { { width : "192px" , maxWidth : "192px" , minWidth : "192px" } }
194- dangerouslySetInnerHTML = { { __html : 字音 } }
195- />
196- ) ;
197- }
198-
199- return (
200- < td
201- key = { `char-${ charIdx } ` }
202- className = "border border-border px-2 py-1 text-sm bg-card font-mono break-words overflow-hidden text-foreground"
203- style = { { width : "192px" , maxWidth : "192px" , minWidth : "192px" } } >
204- { 字音 }
205- </ td >
206- ) ;
207- } ) }
251+ { makeRow ( row , settings ) }
208252 </ tr >
209253 ) ;
210254 } ) }
0 commit comments