bingocode 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/bingo-win.cjs +34 -3
- package/desktop/README.md +30 -0
- package/desktop/bunfig.toml +1 -0
- package/desktop/index.html +17 -0
- package/desktop/package.json +55 -0
- package/desktop/pnpm-lock.yaml +3832 -0
- package/desktop/public/app-icon.jpg +0 -0
- package/desktop/public/fonts/inter-latin-ext.woff2 +0 -0
- package/desktop/public/fonts/inter-latin.woff2 +0 -0
- package/desktop/public/fonts/jetbrains-mono-latin-ext.woff2 +0 -0
- package/desktop/public/fonts/jetbrains-mono-latin.woff2 +0 -0
- package/desktop/public/fonts/manrope-latin-ext.woff2 +0 -0
- package/desktop/public/fonts/manrope-latin.woff2 +0 -0
- package/desktop/public/fonts/material-symbols-outlined.woff2 +0 -0
- package/desktop/public/icons/bilibili.svg +1 -0
- package/desktop/public/icons/douyin.svg +1 -0
- package/desktop/public/icons/github.svg +3 -0
- package/desktop/public/icons/xiaohongshu.svg +1 -0
- package/desktop/scripts/build-macos-arm64.sh +270 -0
- package/desktop/scripts/build-sidecars.ts +183 -0
- package/desktop/scripts/build-windows-x64.ps1 +295 -0
- package/desktop/scripts/scan-missing-imports.ts +235 -0
- package/desktop/sidecars/claude-sidecar.ts +156 -0
- package/desktop/src/App.tsx +5 -0
- package/desktop/src/__tests__/agentsSettings.test.tsx +349 -0
- package/desktop/src/__tests__/pages.test.tsx +290 -0
- package/desktop/src/__tests__/skillsSettings.test.tsx +205 -0
- package/desktop/src/api/adapters.ts +12 -0
- package/desktop/src/api/agents.ts +36 -0
- package/desktop/src/api/cliTasks.ts +28 -0
- package/desktop/src/api/client.ts +63 -0
- package/desktop/src/api/computerUse.ts +76 -0
- package/desktop/src/api/filesystem.ts +30 -0
- package/desktop/src/api/hahaOAuth.ts +38 -0
- package/desktop/src/api/models.ts +28 -0
- package/desktop/src/api/providers.ts +63 -0
- package/desktop/src/api/search.ts +29 -0
- package/desktop/src/api/sessions.ts +56 -0
- package/desktop/src/api/settings.ts +20 -0
- package/desktop/src/api/skills.ts +19 -0
- package/desktop/src/api/tasks.ts +36 -0
- package/desktop/src/api/teams.ts +44 -0
- package/desktop/src/api/websocket.ts +164 -0
- package/desktop/src/components/chat/AskUserQuestion.tsx +268 -0
- package/desktop/src/components/chat/AssistantMessage.tsx +29 -0
- package/desktop/src/components/chat/AttachmentGallery.tsx +113 -0
- package/desktop/src/components/chat/ChatInput.tsx +622 -0
- package/desktop/src/components/chat/CodeViewer.tsx +161 -0
- package/desktop/src/components/chat/ComputerUsePermissionModal.test.tsx +174 -0
- package/desktop/src/components/chat/ComputerUsePermissionModal.tsx +311 -0
- package/desktop/src/components/chat/DiffViewer.tsx +157 -0
- package/desktop/src/components/chat/FileSearchMenu.tsx +198 -0
- package/desktop/src/components/chat/ImageGalleryModal.tsx +91 -0
- package/desktop/src/components/chat/InlineImageGallery.tsx +106 -0
- package/desktop/src/components/chat/InlineTaskSummary.tsx +60 -0
- package/desktop/src/components/chat/MermaidRenderer.test.tsx +98 -0
- package/desktop/src/components/chat/MermaidRenderer.tsx +361 -0
- package/desktop/src/components/chat/MessageActionBar.tsx +27 -0
- package/desktop/src/components/chat/MessageList.test.tsx +313 -0
- package/desktop/src/components/chat/MessageList.tsx +249 -0
- package/desktop/src/components/chat/PermissionDialog.tsx +262 -0
- package/desktop/src/components/chat/SessionTaskBar.test.tsx +99 -0
- package/desktop/src/components/chat/SessionTaskBar.tsx +159 -0
- package/desktop/src/components/chat/StreamingIndicator.tsx +41 -0
- package/desktop/src/components/chat/TerminalChrome.tsx +35 -0
- package/desktop/src/components/chat/ThinkingBlock.tsx +87 -0
- package/desktop/src/components/chat/ToolCallBlock.tsx +247 -0
- package/desktop/src/components/chat/ToolCallGroup.tsx +617 -0
- package/desktop/src/components/chat/ToolResultBlock.tsx +107 -0
- package/desktop/src/components/chat/UserMessage.tsx +38 -0
- package/desktop/src/components/chat/chatBlocks.test.tsx +136 -0
- package/desktop/src/components/chat/clipboard.ts +25 -0
- package/desktop/src/components/chat/composerUtils.test.ts +55 -0
- package/desktop/src/components/chat/composerUtils.ts +149 -0
- package/desktop/src/components/controls/ModelSelector.tsx +156 -0
- package/desktop/src/components/controls/PermissionModeSelector.tsx +229 -0
- package/desktop/src/components/layout/AppShell.tsx +107 -0
- package/desktop/src/components/layout/ContentRouter.tsx +27 -0
- package/desktop/src/components/layout/ProjectFilter.tsx +126 -0
- package/desktop/src/components/layout/Sidebar.test.tsx +158 -0
- package/desktop/src/components/layout/Sidebar.tsx +384 -0
- package/desktop/src/components/layout/StatusBar.tsx +31 -0
- package/desktop/src/components/layout/TabBar.test.tsx +136 -0
- package/desktop/src/components/layout/TabBar.tsx +318 -0
- package/desktop/src/components/layout/TitleBar.tsx +96 -0
- package/desktop/src/components/layout/WindowControls.test.tsx +69 -0
- package/desktop/src/components/layout/WindowControls.tsx +89 -0
- package/desktop/src/components/markdown/MarkdownRenderer.test.tsx +100 -0
- package/desktop/src/components/markdown/MarkdownRenderer.tsx +229 -0
- package/desktop/src/components/settings/ClaudeOfficialLogin.tsx +107 -0
- package/desktop/src/components/shared/Button.tsx +63 -0
- package/desktop/src/components/shared/CopyButton.tsx +58 -0
- package/desktop/src/components/shared/DirectoryPicker.tsx +316 -0
- package/desktop/src/components/shared/Dropdown.tsx +91 -0
- package/desktop/src/components/shared/Input.tsx +38 -0
- package/desktop/src/components/shared/Modal.tsx +65 -0
- package/desktop/src/components/shared/ProjectContextChip.tsx +30 -0
- package/desktop/src/components/shared/Spinner.tsx +30 -0
- package/desktop/src/components/shared/Textarea.tsx +38 -0
- package/desktop/src/components/shared/Toast.tsx +47 -0
- package/desktop/src/components/shared/UpdateChecker.tsx +90 -0
- package/desktop/src/components/skills/SkillDetail.test.tsx +89 -0
- package/desktop/src/components/skills/SkillDetail.tsx +403 -0
- package/desktop/src/components/skills/SkillList.tsx +254 -0
- package/desktop/src/components/tasks/DayOfWeekPicker.tsx +57 -0
- package/desktop/src/components/tasks/NewTaskModal.tsx +407 -0
- package/desktop/src/components/tasks/PromptEditor.tsx +74 -0
- package/desktop/src/components/tasks/TaskEmptyState.tsx +30 -0
- package/desktop/src/components/tasks/TaskList.tsx +46 -0
- package/desktop/src/components/tasks/TaskRow.tsx +253 -0
- package/desktop/src/components/tasks/TaskRunsPanel.tsx +195 -0
- package/desktop/src/components/teams/TeamStatusBar.tsx +147 -0
- package/desktop/src/config/providerPresets.ts +78 -0
- package/desktop/src/config/spinnerVerbs.ts +193 -0
- package/desktop/src/hooks/useKeyboardShortcuts.ts +60 -0
- package/desktop/src/i18n/index.ts +54 -0
- package/desktop/src/i18n/locales/en.ts +670 -0
- package/desktop/src/i18n/locales/zh.ts +670 -0
- package/desktop/src/lib/__tests__/cronDescribe.test.ts +93 -0
- package/desktop/src/lib/cronDescribe.ts +188 -0
- package/desktop/src/lib/desktopRuntime.ts +54 -0
- package/desktop/src/lib/parseRunOutput.ts +79 -0
- package/desktop/src/main.tsx +13 -0
- package/desktop/src/mocks/data.ts +202 -0
- package/desktop/src/pages/ActiveSession.test.tsx +181 -0
- package/desktop/src/pages/ActiveSession.tsx +219 -0
- package/desktop/src/pages/AdapterSettings.tsx +375 -0
- package/desktop/src/pages/AgentTeams.tsx +200 -0
- package/desktop/src/pages/ComputerUseSettings.tsx +420 -0
- package/desktop/src/pages/EmptySession.tsx +518 -0
- package/desktop/src/pages/NewTaskModal.tsx +346 -0
- package/desktop/src/pages/ScheduledTasks.tsx +66 -0
- package/desktop/src/pages/ScheduledTasksEmpty.tsx +152 -0
- package/desktop/src/pages/ScheduledTasksList.tsx +416 -0
- package/desktop/src/pages/SessionControls.tsx +460 -0
- package/desktop/src/pages/Settings.tsx +1448 -0
- package/desktop/src/pages/ToolInspection.tsx +235 -0
- package/desktop/src/stores/adapterStore.ts +106 -0
- package/desktop/src/stores/agentStore.ts +34 -0
- package/desktop/src/stores/chatStore.test.ts +505 -0
- package/desktop/src/stores/chatStore.ts +850 -0
- package/desktop/src/stores/cliTaskStore.ts +152 -0
- package/desktop/src/stores/hahaOAuthStore.test.ts +77 -0
- package/desktop/src/stores/hahaOAuthStore.ts +97 -0
- package/desktop/src/stores/providerStore.ts +101 -0
- package/desktop/src/stores/sessionStore.test.ts +63 -0
- package/desktop/src/stores/sessionStore.ts +102 -0
- package/desktop/src/stores/settingsStore.ts +120 -0
- package/desktop/src/stores/skillStore.ts +51 -0
- package/desktop/src/stores/tabStore.ts +169 -0
- package/desktop/src/stores/taskStore.ts +68 -0
- package/desktop/src/stores/teamStore.ts +344 -0
- package/desktop/src/stores/uiStore.ts +100 -0
- package/desktop/src/stores/updateStore.test.ts +71 -0
- package/desktop/src/stores/updateStore.ts +221 -0
- package/desktop/src/theme/globals.css +465 -0
- package/desktop/src/types/adapter.ts +33 -0
- package/desktop/src/types/chat.ts +152 -0
- package/desktop/src/types/cliTask.ts +24 -0
- package/desktop/src/types/provider.ts +62 -0
- package/desktop/src/types/session.ts +27 -0
- package/desktop/src/types/settings.ts +22 -0
- package/desktop/src/types/skill.ts +38 -0
- package/desktop/src/types/task.ts +56 -0
- package/desktop/src/types/team.ts +38 -0
- package/desktop/src-tauri/Cargo.lock +5549 -0
- package/desktop/src-tauri/Cargo.toml +20 -0
- package/desktop/src-tauri/app-icon.svg +13 -0
- package/desktop/src-tauri/build.rs +3 -0
- package/desktop/src-tauri/capabilities/default.json +106 -0
- package/desktop/src-tauri/icons/android/mipmap-anydpi-v26/ic_launcher.xml +5 -0
- package/desktop/src-tauri/icons/android/values/ic_launcher_background.xml +4 -0
- package/desktop/src-tauri/icons/icon.icns +0 -0
- package/desktop/src-tauri/icons/icon.ico +0 -0
- package/desktop/src-tauri/src/lib.rs +408 -0
- package/desktop/src-tauri/src/main.rs +6 -0
- package/desktop/src-tauri/tauri.conf.json +78 -0
- package/desktop/src-tauri/tauri.macos.conf.json +18 -0
- package/desktop/src-tauri/tauri.release-ci.json +5 -0
- package/desktop/src-tauri/tauri.windows.conf.json +16 -0
- package/desktop/src-tauri/windows-installer-hooks.nsh +17 -0
- package/desktop/tsconfig.json +25 -0
- package/desktop/vite.config.ts +26 -0
- package/desktop/vitest.config.ts +18 -0
- package/package.json +1 -1
- package/src/commands/desktop/desktop.tsx +9 -0
- package/src/commands/desktop/index.ts +26 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
const SPINNER_VERBS = [
|
|
2
|
+
'Accomplishing',
|
|
3
|
+
'Actioning',
|
|
4
|
+
'Actualizing',
|
|
5
|
+
'Architecting',
|
|
6
|
+
'Baking',
|
|
7
|
+
'Beaming',
|
|
8
|
+
"Beboppin'",
|
|
9
|
+
'Befuddling',
|
|
10
|
+
'Billowing',
|
|
11
|
+
'Blanching',
|
|
12
|
+
'Bloviating',
|
|
13
|
+
'Boogieing',
|
|
14
|
+
'Boondoggling',
|
|
15
|
+
'Booping',
|
|
16
|
+
'Bootstrapping',
|
|
17
|
+
'Brewing',
|
|
18
|
+
'Bunning',
|
|
19
|
+
'Burrowing',
|
|
20
|
+
'Calculating',
|
|
21
|
+
'Canoodling',
|
|
22
|
+
'Caramelizing',
|
|
23
|
+
'Cascading',
|
|
24
|
+
'Catapulting',
|
|
25
|
+
'Cerebrating',
|
|
26
|
+
'Channeling',
|
|
27
|
+
'Channelling',
|
|
28
|
+
'Choreographing',
|
|
29
|
+
'Churning',
|
|
30
|
+
'Clauding',
|
|
31
|
+
'Coalescing',
|
|
32
|
+
'Cogitating',
|
|
33
|
+
'Combobulating',
|
|
34
|
+
'Composing',
|
|
35
|
+
'Computing',
|
|
36
|
+
'Concocting',
|
|
37
|
+
'Considering',
|
|
38
|
+
'Contemplating',
|
|
39
|
+
'Cooking',
|
|
40
|
+
'Crafting',
|
|
41
|
+
'Creating',
|
|
42
|
+
'Crunching',
|
|
43
|
+
'Crystallizing',
|
|
44
|
+
'Cultivating',
|
|
45
|
+
'Deciphering',
|
|
46
|
+
'Deliberating',
|
|
47
|
+
'Determining',
|
|
48
|
+
'Dilly-dallying',
|
|
49
|
+
'Discombobulating',
|
|
50
|
+
'Doing',
|
|
51
|
+
'Doodling',
|
|
52
|
+
'Drizzling',
|
|
53
|
+
'Ebbing',
|
|
54
|
+
'Effecting',
|
|
55
|
+
'Elucidating',
|
|
56
|
+
'Embellishing',
|
|
57
|
+
'Enchanting',
|
|
58
|
+
'Envisioning',
|
|
59
|
+
'Evaporating',
|
|
60
|
+
'Fermenting',
|
|
61
|
+
'Fiddle-faddling',
|
|
62
|
+
'Finagling',
|
|
63
|
+
'Flambéing',
|
|
64
|
+
'Flibbertigibbeting',
|
|
65
|
+
'Flowing',
|
|
66
|
+
'Flummoxing',
|
|
67
|
+
'Fluttering',
|
|
68
|
+
'Forging',
|
|
69
|
+
'Forming',
|
|
70
|
+
'Frolicking',
|
|
71
|
+
'Frosting',
|
|
72
|
+
'Gallivanting',
|
|
73
|
+
'Galloping',
|
|
74
|
+
'Garnishing',
|
|
75
|
+
'Generating',
|
|
76
|
+
'Gesticulating',
|
|
77
|
+
'Germinating',
|
|
78
|
+
'Gitifying',
|
|
79
|
+
'Grooving',
|
|
80
|
+
'Gusting',
|
|
81
|
+
'Harmonizing',
|
|
82
|
+
'Hashing',
|
|
83
|
+
'Hatching',
|
|
84
|
+
'Herding',
|
|
85
|
+
'Honking',
|
|
86
|
+
'Hullaballooing',
|
|
87
|
+
'Hyperspacing',
|
|
88
|
+
'Ideating',
|
|
89
|
+
'Imagining',
|
|
90
|
+
'Improvising',
|
|
91
|
+
'Incubating',
|
|
92
|
+
'Inferring',
|
|
93
|
+
'Infusing',
|
|
94
|
+
'Ionizing',
|
|
95
|
+
'Jitterbugging',
|
|
96
|
+
'Julienning',
|
|
97
|
+
'Kneading',
|
|
98
|
+
'Leavening',
|
|
99
|
+
'Levitating',
|
|
100
|
+
'Lollygagging',
|
|
101
|
+
'Manifesting',
|
|
102
|
+
'Marinating',
|
|
103
|
+
'Meandering',
|
|
104
|
+
'Metamorphosing',
|
|
105
|
+
'Misting',
|
|
106
|
+
'Moonwalking',
|
|
107
|
+
'Moseying',
|
|
108
|
+
'Mulling',
|
|
109
|
+
'Mustering',
|
|
110
|
+
'Musing',
|
|
111
|
+
'Nebulizing',
|
|
112
|
+
'Nesting',
|
|
113
|
+
'Newspapering',
|
|
114
|
+
'Noodling',
|
|
115
|
+
'Nucleating',
|
|
116
|
+
'Orbiting',
|
|
117
|
+
'Orchestrating',
|
|
118
|
+
'Osmosing',
|
|
119
|
+
'Perambulating',
|
|
120
|
+
'Percolating',
|
|
121
|
+
'Perusing',
|
|
122
|
+
'Philosophising',
|
|
123
|
+
'Photosynthesizing',
|
|
124
|
+
'Pollinating',
|
|
125
|
+
'Pondering',
|
|
126
|
+
'Pontificating',
|
|
127
|
+
'Pouncing',
|
|
128
|
+
'Precipitating',
|
|
129
|
+
'Prestidigitating',
|
|
130
|
+
'Processing',
|
|
131
|
+
'Proofing',
|
|
132
|
+
'Propagating',
|
|
133
|
+
'Puttering',
|
|
134
|
+
'Puzzling',
|
|
135
|
+
'Quantumizing',
|
|
136
|
+
'Razzle-dazzling',
|
|
137
|
+
'Razzmatazzing',
|
|
138
|
+
'Recombobulating',
|
|
139
|
+
'Reticulating',
|
|
140
|
+
'Roosting',
|
|
141
|
+
'Ruminating',
|
|
142
|
+
'Sautéing',
|
|
143
|
+
'Scampering',
|
|
144
|
+
'Schlepping',
|
|
145
|
+
'Scurrying',
|
|
146
|
+
'Seasoning',
|
|
147
|
+
'Shenaniganing',
|
|
148
|
+
'Shimmying',
|
|
149
|
+
'Simmering',
|
|
150
|
+
'Skedaddling',
|
|
151
|
+
'Sketching',
|
|
152
|
+
'Slithering',
|
|
153
|
+
'Smooshing',
|
|
154
|
+
'Sock-hopping',
|
|
155
|
+
'Spelunking',
|
|
156
|
+
'Spinning',
|
|
157
|
+
'Sprouting',
|
|
158
|
+
'Stewing',
|
|
159
|
+
'Sublimating',
|
|
160
|
+
'Swirling',
|
|
161
|
+
'Swooping',
|
|
162
|
+
'Symbioting',
|
|
163
|
+
'Synthesizing',
|
|
164
|
+
'Tempering',
|
|
165
|
+
'Thinking',
|
|
166
|
+
'Thundering',
|
|
167
|
+
'Tinkering',
|
|
168
|
+
'Tomfoolering',
|
|
169
|
+
'Topsy-turvying',
|
|
170
|
+
'Transfiguring',
|
|
171
|
+
'Transmuting',
|
|
172
|
+
'Twisting',
|
|
173
|
+
'Undulating',
|
|
174
|
+
'Unfurling',
|
|
175
|
+
'Unravelling',
|
|
176
|
+
'Vibing',
|
|
177
|
+
'Waddling',
|
|
178
|
+
'Wandering',
|
|
179
|
+
'Warping',
|
|
180
|
+
'Whatchamacalliting',
|
|
181
|
+
'Whirlpooling',
|
|
182
|
+
'Whirring',
|
|
183
|
+
'Whisking',
|
|
184
|
+
'Wibbling',
|
|
185
|
+
'Working',
|
|
186
|
+
'Wrangling',
|
|
187
|
+
'Zesting',
|
|
188
|
+
'Zigzagging',
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
export function randomSpinnerVerb(): string {
|
|
192
|
+
return SPINNER_VERBS[Math.floor(Math.random() * SPINNER_VERBS.length)] ?? 'Thinking'
|
|
193
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react'
|
|
2
|
+
import { useSessionStore } from '../stores/sessionStore'
|
|
3
|
+
import { useChatStore } from '../stores/chatStore'
|
|
4
|
+
import { useTabStore } from '../stores/tabStore'
|
|
5
|
+
import { useUIStore } from '../stores/uiStore'
|
|
6
|
+
|
|
7
|
+
export function useKeyboardShortcuts() {
|
|
8
|
+
const setActiveSession = useSessionStore((s) => s.setActiveSession)
|
|
9
|
+
const setActiveView = useUIStore((s) => s.setActiveView)
|
|
10
|
+
const closeModal = useUIStore((s) => s.closeModal)
|
|
11
|
+
const activeModal = useUIStore((s) => s.activeModal)
|
|
12
|
+
const stopGeneration = useChatStore((s) => s.stopGeneration)
|
|
13
|
+
const activeTabId = useTabStore((s) => s.activeTabId)
|
|
14
|
+
const chatState = useChatStore((s) => activeTabId ? s.sessions[activeTabId]?.chatState ?? 'idle' : 'idle')
|
|
15
|
+
|
|
16
|
+
const activeModalRef = useRef(activeModal)
|
|
17
|
+
activeModalRef.current = activeModal
|
|
18
|
+
const chatStateRef = useRef(chatState)
|
|
19
|
+
chatStateRef.current = chatState
|
|
20
|
+
const activeTabIdRef = useRef(activeTabId)
|
|
21
|
+
activeTabIdRef.current = activeTabId
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const handler = (e: KeyboardEvent) => {
|
|
25
|
+
const meta = e.metaKey || e.ctrlKey
|
|
26
|
+
|
|
27
|
+
// Cmd+N — New session
|
|
28
|
+
if (meta && e.key === 'n') {
|
|
29
|
+
e.preventDefault()
|
|
30
|
+
setActiveSession(null)
|
|
31
|
+
setActiveView('code')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Cmd+K — Focus search (sidebar search input)
|
|
35
|
+
if (meta && e.key === 'k') {
|
|
36
|
+
e.preventDefault()
|
|
37
|
+
const searchInput = document.querySelector('aside input[type="text"]') as HTMLInputElement
|
|
38
|
+
searchInput?.focus()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Escape — Close modal or clear state
|
|
42
|
+
if (e.key === 'Escape') {
|
|
43
|
+
if (activeModalRef.current) {
|
|
44
|
+
closeModal()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Cmd+. — Stop generation
|
|
49
|
+
if (meta && e.key === '.') {
|
|
50
|
+
if (chatStateRef.current !== 'idle' && activeTabIdRef.current) {
|
|
51
|
+
e.preventDefault()
|
|
52
|
+
stopGeneration(activeTabIdRef.current)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
document.addEventListener('keydown', handler)
|
|
58
|
+
return () => document.removeEventListener('keydown', handler)
|
|
59
|
+
}, [closeModal, setActiveSession, setActiveView, stopGeneration])
|
|
60
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useSettingsStore } from '../stores/settingsStore'
|
|
2
|
+
import { en, type TranslationKey } from './locales/en'
|
|
3
|
+
import { zh } from './locales/zh'
|
|
4
|
+
|
|
5
|
+
export type Locale = 'en' | 'zh'
|
|
6
|
+
|
|
7
|
+
const translations: Record<Locale, Record<string, string>> = { en, zh }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Translate a key with optional interpolation params.
|
|
11
|
+
* Falls back to the key itself if no translation is found.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* translate('en', 'settings.providers.connected', { latency: '42' })
|
|
15
|
+
* // => "Connected (42ms)"
|
|
16
|
+
*/
|
|
17
|
+
export function translate(
|
|
18
|
+
locale: Locale,
|
|
19
|
+
key: TranslationKey,
|
|
20
|
+
params?: Record<string, string | number>,
|
|
21
|
+
): string {
|
|
22
|
+
let text = translations[locale]?.[key] ?? translations.en[key] ?? key
|
|
23
|
+
if (params) {
|
|
24
|
+
for (const [k, v] of Object.entries(params)) {
|
|
25
|
+
text = text.replace(new RegExp(`\\{${k}\\}`, 'g'), String(v))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return text
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* React hook that returns a `t()` function bound to the current locale.
|
|
33
|
+
* Re-renders when the locale changes.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const t = useTranslation()
|
|
37
|
+
* t('sidebar.newSession') // => "New session" or "新建会话"
|
|
38
|
+
*/
|
|
39
|
+
export function useTranslation() {
|
|
40
|
+
const locale = useSettingsStore((s) => s.locale)
|
|
41
|
+
return (key: TranslationKey, params?: Record<string, string | number>) =>
|
|
42
|
+
translate(locale, key, params)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get a translation outside of React (e.g. in stores).
|
|
47
|
+
* Reads the current locale from the Zustand store directly.
|
|
48
|
+
*/
|
|
49
|
+
export function t(key: TranslationKey, params?: Record<string, string | number>): string {
|
|
50
|
+
const locale = useSettingsStore.getState().locale
|
|
51
|
+
return translate(locale, key, params)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type { TranslationKey }
|