| import { useState, useEffect, useRef, useCallback } from 'react'; |
| import { useTranslation } from 'react-i18next'; |
| import { MdxGroupIndex } from './types'; |
| import { useSearchStore, useSettingsStore } from './store'; |
| import { LibraryView } from './pages/LibraryView'; |
| import { SearchView, SearchViewRef } from './pages/SearchView'; |
| import { HistoryView } from './pages/HistoryView'; |
| import { FavoritesView } from './pages/FavoritesView'; |
| import { SettingsView } from './pages/SettingsView'; |
| import { |
| IonApp, |
| IonIcon, |
| IonLabel, |
| IonRouterOutlet, |
| IonTabs, |
| IonTabBar, |
| IonTabButton, |
| IonAlert, |
| setupIonicReact, |
| } from '@ionic/react'; |
| import { IonReactRouter } from '@ionic/react-router'; |
| import { Redirect, Route } from 'react-router-dom'; |
| import { |
| searchOutline, |
| libraryOutline, |
| timeOutline, |
| heartOutline, |
| settingsOutline, |
| } from 'ionicons/icons'; |
| import { |
| applyIonicTheme, |
| getEffectiveThemeMode, |
| getEffectiveThemePreset, |
| } from './ionicTheme'; |
| import { listen } from '@tauri-apps/api/event'; |
| import { readText as tauriReadText } from '@tauri-apps/plugin-clipboard-manager'; |
|
|
| |
| |
| const getInitialThemePreset = (): 'ios' | 'md' => { |
| const stored = localStorage.getItem('settings-storage'); |
| if (stored) { |
| try { |
| const parsed = JSON.parse(stored); |
| const themePreset = parsed.state?.themePreset || 'auto'; |
| return getEffectiveThemePreset(themePreset); |
| } catch (e) { |
| return getEffectiveThemePreset('auto'); |
| } |
| } |
| return getEffectiveThemePreset('auto'); |
| }; |
|
|
| setupIonicReact({ |
| rippleEffect: true, |
| mode: getInitialThemePreset(), |
| }); |
| |
| |
| |
| interface TabItem { |
| id: string; |
| titleKey: string; |
| icon: string; |
| path: string; |
| } |
|
|
| const tabItems: TabItem[] = [ |
| { id: 'search', titleKey: 'Search', icon: searchOutline, path: '/search' }, |
| { id: 'library', titleKey: 'Library', icon: libraryOutline, path: '/library' }, |
| { id: 'history', titleKey: 'History', icon: timeOutline, path: '/history' }, |
| { id: 'favorites', titleKey: 'Favorites', icon: heartOutline, path: '/favorites' }, |
| { id: 'settings', titleKey: 'Settings', icon: settingsOutline, path: '/settings' }, |
| ]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function App() { |
| const { t } = useTranslation(); |
| |
| |
| const useProfile = useSearchStore((state) => state.useProfile); |
| const performSearch = useSearchStore ((state) => state.performSearch); |
| const searchTerm = useSearchStore ((state) => state.searchTerm); |
|
|
| |
| const themePreset = useSettingsStore((state) => state.themePreset); |
| const themeMode = useSettingsStore((state) => state.themeMode); |
|
|
| |
| const effectiveThemePreset = getEffectiveThemePreset(themePreset); |
|
|
| const [errorAlert, setErrorAlert] = useState<{ open: boolean; message: string }>({ |
| open: false, |
| message: '' |
| }); |
|
|
| |
| const [effectiveThemeMode, setEffectiveThemeMode] = useState<'light' | 'dark'>(() => |
| getEffectiveThemeMode(themeMode) |
| ); |
| |
| |
| const searchViewRef = useRef<SearchViewRef>(null); |
|
|
| const setActiveTab = (tab: string) => { |
| const tabElement = document.querySelector(`[tab="${tab}"]`) as HTMLElement; |
| if (tab) { |
| tabElement.click(); |
| } |
| }; |
|
|
| |
| const handleSelectLibrary = useCallback(async (profileId?: number) => { |
| try { |
| if (profileId) { |
| const lastSearchTerm = searchTerm; |
| await useProfile(profileId); |
| if (lastSearchTerm) { |
| performSearch(lastSearchTerm); |
| } |
| |
| setActiveTab('search'); |
| } |
| |
| } catch (error) { |
| console.error('Failed to open library:', error); |
| setErrorAlert({ |
| open: true, |
| message: t('Failed to open library. Please check if the dictionary file exists or try again.') |
| }); |
| } |
| }, [useProfile, performSearch, searchTerm]); |
|
|
|
|
| |
| const handleEntrySelect = useCallback((groupIndexes: MdxGroupIndex[], addToHistory: boolean = true) => { |
| if (searchViewRef.current) { |
| searchViewRef.current.displayByEntry(groupIndexes, addToHistory); |
| } |
| setActiveTab('search'); |
| }, [searchViewRef]); |
|
|
|
|
| const handleCloseErrorAlert = useCallback(() => { |
| setErrorAlert({ open: false, message: '' }); |
| }, [setErrorAlert]); |
|
|
|
|
| |
| useEffect(() => { |
| const newEffectiveMode = getEffectiveThemeMode(themeMode); |
| setEffectiveThemeMode(newEffectiveMode); |
| }, [themeMode]); |
|
|
| |
| useEffect(() => { |
| applyIonicTheme({ preset: effectiveThemePreset, mode: effectiveThemeMode }); |
| }, [effectiveThemePreset, effectiveThemeMode]); |
|
|
| |
| useEffect(() => { |
| if (themeMode !== 'auto') return; |
|
|
| const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); |
| const handleChange = (e: MediaQueryListEvent) => { |
| const newMode = e.matches ? 'dark' : 'light'; |
| setEffectiveThemeMode(newMode); |
| }; |
|
|
| |
| if (mediaQuery.addEventListener) { |
| mediaQuery.addEventListener('change', handleChange); |
| return () => mediaQuery.removeEventListener('change', handleChange); |
| } |
| |
| else if (mediaQuery.addListener) { |
| mediaQuery.addListener(handleChange); |
| return () => mediaQuery.removeListener(handleChange); |
| } |
| }, [themeMode]); |
|
|
|
|
| |
| const autoLookupClipboard = useSettingsStore((state) => state.globalSettings.autoLookupClipboard); |
|
|
| |
| useEffect(() => { |
| let unlisten: (() => void) | undefined; |
| let lastClipboardText: string | null = null; |
|
|
| const setupWindowFocusListener = async () => { |
| try { |
| |
| try { |
| lastClipboardText = await tauriReadText(); |
| } catch (error) { |
| console.error('Failed to read initial clipboard:', error); |
| } |
|
|
| |
| unlisten = await listen('window-focus', async () => { |
| |
| |
| if (!autoLookupClipboard) { |
| return; |
| } |
|
|
| try { |
| |
| const currentClipboardText = await tauriReadText(); |
| |
| |
| if (currentClipboardText && |
| currentClipboardText.trim() && |
| currentClipboardText !== lastClipboardText) { |
| |
| |
| lastClipboardText = currentClipboardText; |
| |
| |
| if (searchViewRef.current) { |
| searchViewRef.current.displayByKey(currentClipboardText.trim(), true); |
| } |
| setActiveTab('search'); |
| } |
| } catch (error) { |
| console.error('Failed to read clipboard on window focus:', error); |
| } |
| }); |
| } catch (error) { |
| console.error('Failed to setup window focus listener:', error); |
| } |
| }; |
| setupWindowFocusListener(); |
|
|
| |
| return () => { |
| if (unlisten) { |
| unlisten(); |
| } |
| }; |
| }, [searchViewRef, autoLookupClipboard]); |
|
|
| return ( |
| <IonApp> |
| <IonReactRouter> |
| <IonTabs> |
| {/* Main Content */} |
| <IonRouterOutlet> |
| <Route |
| path="/search" |
| render={() => <SearchView ref={searchViewRef} />} |
| exact |
| /> |
| |
| <Route |
| path="/library" |
| render={() => <LibraryView onSelectLibrary={handleSelectLibrary} />} |
| exact |
| /> |
| |
| <Route |
| path="/history" |
| render={() => <HistoryView |
| onEntrySelect={(groupIndexes) => handleEntrySelect(groupIndexes, false)} |
| />} |
| exact |
| /> |
| |
| <Route |
| path="/favorites" |
| render={() => <FavoritesView |
| onEntrySelect={(groupIndexes) => handleEntrySelect(groupIndexes, true)} |
| />} |
| exact |
| /> |
| |
| <Route |
| path="/settings" |
| render={() => <SettingsView |
| onSettingsChanged={(setting, value) => console.log('Setting changed:', setting, value)} |
| />} |
| exact |
| /> |
| |
| <Route path="/" exact> |
| <Redirect to="/search" /> |
| </Route> |
| </IonRouterOutlet> |
| |
| {/* Tab Bar - adaptive positioning */} |
| <IonTabBar slot="bottom" className="tab-bar"> |
| {tabItems.map((item) => ( |
| <IonTabButton |
| key={item.id} |
| tab={item.id} |
| href={item.path} |
| > |
| <IonIcon icon={item.icon} /> |
| <IonLabel>{t(item.titleKey)}</IonLabel> |
| </IonTabButton> |
| ))} |
| </IonTabBar> |
| </IonTabs> |
| |
| {/* Error Alert */} |
| <IonAlert |
| isOpen={errorAlert.open} |
| onDidDismiss={handleCloseErrorAlert} |
| header={t('Error')} |
| message={errorAlert.message} |
| buttons={[ |
| { |
| text: t('OK'), |
| role: 'confirm', |
| handler: handleCloseErrorAlert, |
| }, |
| ]} |
| /> |
| </IonReactRouter> |
| |
| {/* Global portal container for imperative dialogs */} |
| <div id="dialog-portal-root" /> |
| </IonApp> |
| ); |
| } |
|
|
| export default App; |
|
|