Skip to content

Commit 8561313

Browse files
author
linyuan.yang
committed
packages/app
1 parent 871a96e commit 8561313

5 files changed

Lines changed: 115 additions & 21 deletions

File tree

‎packages/app/src/App.vue‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ const THEME_KEY = 'sbot-app-theme'
1717
1818
type ThemeMode = 'system' | 'light' | 'dark'
1919
20+
function readStorage(key: string): string | null {
21+
try { return window.localStorage?.getItem(key) ?? null }
22+
catch { return null }
23+
}
24+
25+
function writeStorage(key: string, value: string) {
26+
try { window.localStorage?.setItem(key, value) }
27+
catch {}
28+
}
29+
2030
function loadRemotes(): RemoteEntry[] {
21-
try { return JSON.parse(localStorage.getItem(REMOTES_KEY) || '[]') }
31+
try {
32+
const parsed = JSON.parse(readStorage(REMOTES_KEY) || '[]')
33+
return Array.isArray(parsed) ? parsed : []
34+
}
2235
catch { return [] }
2336
}
2437
function saveRemotes(list: RemoteEntry[]) {
25-
localStorage.setItem(REMOTES_KEY, JSON.stringify(list))
38+
writeStorage(REMOTES_KEY, JSON.stringify(list))
2639
}
2740
2841
const remotes = ref<RemoteEntry[]>(loadRemotes())
@@ -33,17 +46,19 @@ const connectError = ref('')
3346
const connecting = ref(false)
3447
3548
const themeMode = ref<ThemeMode>(((): ThemeMode => {
36-
const v = localStorage.getItem(THEME_KEY)
49+
const v = readStorage(THEME_KEY)
3750
return v === 'light' || v === 'dark' || v === 'system' ? v : 'system'
3851
})())
3952
40-
const mq = window.matchMedia('(prefers-color-scheme: dark)')
53+
const mq = typeof window.matchMedia === 'function'
54+
? window.matchMedia('(prefers-color-scheme: dark)')
55+
: null
4156
const themeStyleEl = document.createElement('style')
4257
themeStyleEl.id = 'sbot-app-theme'
4358
document.head.appendChild(themeStyleEl)
4459
4560
const resolvedTheme = computed<'light' | 'dark'>(() =>
46-
themeMode.value === 'system' ? (mq.matches ? 'dark' : 'light') : themeMode.value,
61+
themeMode.value === 'system' ? (mq?.matches ? 'dark' : 'light') : themeMode.value,
4762
)
4863
4964
function applyTheme(t: 'light' | 'dark') {
@@ -52,13 +67,26 @@ function applyTheme(t: 'light' | 'dark') {
5267
}
5368
5469
watch(resolvedTheme, applyTheme, { immediate: true })
55-
watch(themeMode, (m) => localStorage.setItem(THEME_KEY, m))
70+
watch(themeMode, (m) => writeStorage(THEME_KEY, m))
5671
5772
function onSystemChange() {
5873
if (themeMode.value === 'system') applyTheme(resolvedTheme.value)
5974
}
60-
onMounted(() => mq.addEventListener('change', onSystemChange))
61-
onUnmounted(() => mq.removeEventListener('change', onSystemChange))
75+
76+
function addSystemThemeListener() {
77+
if (!mq) return
78+
if (typeof mq.addEventListener === 'function') mq.addEventListener('change', onSystemChange)
79+
else mq.addListener?.(onSystemChange)
80+
}
81+
82+
function removeSystemThemeListener() {
83+
if (!mq) return
84+
if (typeof mq.removeEventListener === 'function') mq.removeEventListener('change', onSystemChange)
85+
else mq.removeListener?.(onSystemChange)
86+
}
87+
88+
onMounted(addSystemThemeListener)
89+
onUnmounted(removeSystemThemeListener)
6290
6391
async function selectServer(baseUrl: string) {
6492
if (connecting.value) return

‎packages/app/src/UpdaterDialog.vue‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ const percent = computed(() =>
1515
total.value > 0 ? Math.min(100, Math.round((downloaded.value / total.value) * 100)) : 0,
1616
)
1717
18+
function isMobileRuntime() {
19+
return /Android|iPhone|iPad|iPod/i.test(navigator.userAgent)
20+
}
21+
1822
onMounted(async () => {
23+
if (isMobileRuntime()) return
1924
try {
2025
const u = await check()
2126
if (u) {

‎packages/app/src/main.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1-
import { createApp } from 'vue'
2-
import App from './App.vue'
1+
function showStartupError(error: unknown) {
2+
const root = document.getElementById('app')
3+
if (!root) return
4+
const err = error instanceof Error ? error : null
5+
const message = err?.stack || err?.message || String(error)
6+
const escaped = message.replace(/[&<>"']/g, (char) => {
7+
const entities: Record<string, string> = {
8+
'&': '&amp;',
9+
'<': '&lt;',
10+
'>': '&gt;',
11+
'"': '&quot;',
12+
"'": '&#39;',
13+
}
14+
return entities[char] ?? char
15+
})
16+
root.innerHTML = `
17+
<div style="padding:16px;font:14px/1.5 sans-serif;color:#b42318;background:#fff;">
18+
<strong>应用启动失败</strong>
19+
<pre style="white-space:pre-wrap;margin-top:8px;font-size:12px;">${escaped}</pre>
20+
</div>
21+
`
22+
}
323

4-
createApp(App).mount('#app')
24+
window.addEventListener('error', (event) => showStartupError(event.error ?? event.message))
25+
window.addEventListener('unhandledrejection', (event) => showStartupError(event.reason))
26+
27+
;(async () => {
28+
try {
29+
const { createApp } = await import('vue')
30+
const { default: App } = await import('./App.vue')
31+
createApp(App).mount('#app')
32+
} catch (error) {
33+
showStartupError(error)
34+
}
35+
})()

‎packages/app/vite.config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import vue from '@vitejs/plugin-vue'
33

44
export default defineConfig({
55
plugins: [vue()],
6+
base: './',
67
clearScreen: false,
78
server: {
89
port: 1420,
@@ -11,7 +12,7 @@ export default defineConfig({
1112
},
1213
envPrefix: ['VITE_', 'TAURI_'],
1314
build: {
14-
target: 'esnext',
15+
target: 'chrome80',
1516
minify: !process.env.TAURI_DEBUG ? 'esbuild' : false,
1617
sourcemap: !!process.env.TAURI_DEBUG,
1718
},

‎packages/chat-ui/src/components/PathPickerModal.vue‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const pickerParent = ref<string | null>(null)
2323
const pickerItems = ref<string[]>([]) // 绝对路径
2424
const pickerDrives = ref<DriveEntry[]>([])
2525
const pickerQuickDirs = ref<QuickDir[]>([])
26+
const pickerError = ref('')
2627
const pickerCreating = ref(false)
2728
const pickerNewName = ref('')
2829
const newNameInput = ref<InstanceType<typeof SInput> | null>(null)
@@ -50,6 +51,10 @@ function resetCreate() {
5051
pickerNewName.value = ''
5152
}
5253
54+
function getErrorMessage(e: unknown): string {
55+
return e instanceof Error ? e.message : String(e)
56+
}
57+
5358
function enterDriveMode() {
5459
resetCreate()
5560
pickerPath.value = ''
@@ -66,9 +71,11 @@ async function navigate(absPath: string): Promise<boolean> {
6671
pickerPath.value = res.path
6772
pickerParent.value = res.parent
6873
pickerItems.value = res.items
74+
pickerError.value = ''
6975
return true
7076
} catch (e: any) {
71-
emit('error', e?.message ?? String(e))
77+
pickerError.value = getErrorMessage(e)
78+
emit('error', pickerError.value)
7279
return false
7380
} finally {
7481
pickerLoading.value = false
@@ -108,17 +115,29 @@ async function open(initialPath = '') {
108115
enterDriveMode()
109116
pickerDrives.value = []
110117
pickerQuickDirs.value = []
118+
pickerError.value = ''
119+
pickerLoading.value = true
111120
pickerOpen.value = true
112121
113-
const [drives, quicks] = await Promise.all([
114-
props.transport.listDrives().catch(() => [] as DriveEntry[]),
115-
props.transport.quickDirs().catch(() => [] as QuickDir[]),
122+
const [drivesResult, quicksResult] = await Promise.allSettled([
123+
props.transport.listDrives(),
124+
props.transport.quickDirs(),
116125
])
117-
pickerDrives.value = drives
118-
pickerQuickDirs.value = quicks
126+
127+
if (drivesResult.status === 'fulfilled') pickerDrives.value = drivesResult.value
128+
if (quicksResult.status === 'fulfilled') pickerQuickDirs.value = quicksResult.value
129+
130+
const errors: string[] = []
131+
if (drivesResult.status === 'rejected') errors.push(`磁盘列表:${getErrorMessage(drivesResult.reason)}`)
132+
if (quicksResult.status === 'rejected') errors.push(`常用目录:${getErrorMessage(quicksResult.reason)}`)
133+
if (errors.length) {
134+
pickerError.value = `请求目录失败:${errors.join(';')}`
135+
emit('error', pickerError.value)
136+
}
137+
pickerLoading.value = false
119138
120139
if (initialPath && await navigate(initialPath)) return
121-
if (drives.length === 1) await navigate(drives[0].path)
140+
if (pickerDrives.value.length === 1) await navigate(pickerDrives.value[0].path)
122141
// 多盘符:保持 driveMode 让用户选
123142
}
124143
@@ -156,11 +175,12 @@ defineExpose({ open })
156175
@click="navigate(d.path)"
157176
>{{ d.label }}</SChip>
158177
</div>
178+
<div v-if="pickerError" class="chatui-picker-error">{{ pickerError }}</div>
159179

160180
<div class="chatui-picker-list">
161181
<div v-if="pickerLoading" class="chatui-picker-empty">{{ L.loading }}</div>
162182
<template v-else-if="pickerDriveMode">
163-
<div v-if="pickerDrives.length === 0" class="chatui-picker-empty">{{ L.noSubdirs }}</div>
183+
<div v-if="pickerDrives.length === 0 && !pickerError" class="chatui-picker-empty">{{ L.noSubdirs }}</div>
164184
<div
165185
v-for="d in pickerDrives" :key="d.path"
166186
class="chatui-picker-item"
@@ -187,7 +207,7 @@ defineExpose({ open })
187207
<SButton type="outline" size="sm" @click="confirmCreate">✓</SButton>
188208
<SButton type="outline" size="sm" @click="cancelCreate">✕</SButton>
189209
</div>
190-
<div v-if="pickerItems.length === 0 && !pickerCreating" class="chatui-picker-empty">{{ L.noSubdirs }}</div>
210+
<div v-if="pickerItems.length === 0 && !pickerCreating && !pickerError" class="chatui-picker-empty">{{ L.noSubdirs }}</div>
191211
<div v-for="item in pickerItems" :key="item" class="chatui-picker-item" @click="navigate(item)">
192212
<span class="chatui-picker-icon">▶</span>{{ itemLabel(item) }}
193213
</div>
@@ -227,6 +247,15 @@ defineExpose({ open })
227247
background: var(--chatui-bg-hover);
228248
}
229249
.chatui-picker-qd-active { background: var(--chatui-bg-active) !important; }
250+
.chatui-picker-error {
251+
padding: 8px 12px;
252+
border-bottom: 1px solid var(--chatui-border);
253+
color: var(--chatui-btn-danger, #d14343);
254+
background: var(--chatui-bg-surface);
255+
font-size: 12px;
256+
line-height: 1.4;
257+
word-break: break-word;
258+
}
230259
.chatui-picker-create-row {
231260
display: flex; align-items: center; gap: 6px; padding: 5px 14px;
232261
border-bottom: 1px solid var(--chatui-border);

0 commit comments

Comments
 (0)