Skip to content

Commit 5d0fc8a

Browse files
authored
refactor(dashboard): replace legacy isElectron bridge fields with isDesktop (#5269)
* refactor dashboard desktop bridge fields from isElectron to isDesktop * refactor dashboard runtime detection into shared helper
1 parent a4d37e2 commit 5d0fc8a

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

dashboard/src/App.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { RouterView } from 'vue-router';
1818
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
1919
import { useToastStore } from '@/stores/toast'
2020
import WaitingForRestart from '@/components/shared/WaitingForRestart.vue'
21-
import { restartAstrBot } from '@/utils/restartAstrBot'
2221
2322
const toastStore = useToastStore()
2423
const globalWaitingRef = ref(null)
@@ -33,12 +32,12 @@ const snackbarShow = computed({
3332
3433
onMounted(() => {
3534
const desktopBridge = window.astrbotDesktop
36-
if (!desktopBridge?.isElectron || !desktopBridge.onTrayRestartBackend) {
35+
if (!desktopBridge?.onTrayRestartBackend) {
3736
return
3837
}
3938
disposeTrayRestartListener = desktopBridge.onTrayRestartBackend(async () => {
4039
try {
41-
await restartAstrBot(globalWaitingRef.value)
40+
await globalWaitingRef.value?.check?.()
4241
} catch (error) {
4342
globalWaitingRef.value?.stop?.()
4443
console.error('Tray restart backend failed:', error)

dashboard/src/layouts/full/vertical-header/VerticalHeader.vue

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import StyledMenu from '@/components/shared/StyledMenu.vue';
1818
import { useLanguageSwitcher } from '@/i18n/composables';
1919
import type { Locale } from '@/i18n/types';
2020
import AboutPage from '@/views/AboutPage.vue';
21+
import { getDesktopRuntimeInfo } from '@/utils/desktopRuntime';
2122
2223
enableKatex();
2324
enableMermaid();
@@ -46,8 +47,8 @@ let version = ref('');
4647
let releases = ref([]);
4748
let updatingDashboardLoading = ref(false);
4849
let installLoading = ref(false);
49-
const isElectronApp = ref(
50-
typeof window !== 'undefined' && !!window.astrbotDesktop?.isElectron
50+
const isDesktopReleaseMode = ref(
51+
typeof window !== 'undefined' && !!window.astrbotDesktop?.isDesktop
5152
);
5253
const redirectConfirmDialog = ref(false);
5354
const pendingRedirectUrl = ref('');
@@ -133,7 +134,7 @@ function confirmExternalRedirect() {
133134
}
134135
}
135136
136-
const getReleaseUrlForElectron = () => {
137+
const getReleaseUrlForDesktop = () => {
137138
const firstRelease = (releases.value as any[])?.[0];
138139
if (firstRelease?.tag_name) {
139140
const tag = firstRelease.tag_name as string;
@@ -147,12 +148,12 @@ const getReleaseUrlForElectron = () => {
147148
};
148149
149150
function handleUpdateClick() {
150-
if (isElectronApp.value) {
151+
if (isDesktopReleaseMode.value) {
151152
requestExternalRedirect('');
152153
resolvingReleaseTarget.value = true;
153154
checkUpdate();
154155
void getReleases().finally(() => {
155-
pendingRedirectUrl.value = getReleaseUrlForElectron() || fallbackReleaseUrl;
156+
pendingRedirectUrl.value = getReleaseUrlForDesktop() || fallbackReleaseUrl;
156157
resolvingReleaseTarget.value = false;
157158
});
158159
return;
@@ -246,7 +247,7 @@ function checkUpdate() {
246247
} else {
247248
updateStatus.value = res.data.message;
248249
}
249-
dashboardHasNewVersion.value = isElectronApp.value
250+
dashboardHasNewVersion.value = isDesktopReleaseMode.value
250251
? false
251252
: res.data.data.dashboard_has_new_version;
252253
})
@@ -388,13 +389,9 @@ const changeLanguage = async (langCode: string) => {
388389
};
389390
390391
onMounted(async () => {
391-
try {
392-
isElectronApp.value = !!window.astrbotDesktop?.isElectron ||
393-
!!(await window.astrbotDesktop?.isElectronRuntime?.());
394-
} catch {
395-
isElectronApp.value = false;
396-
}
397-
if (isElectronApp.value) {
392+
const runtimeInfo = await getDesktopRuntimeInfo();
393+
isDesktopReleaseMode.value = runtimeInfo.isDesktopRuntime;
394+
if (isDesktopReleaseMode.value) {
398395
dashboardHasNewVersion.value = false;
399396
}
400397
});
@@ -441,7 +438,7 @@ onMounted(async () => {
441438
<small v-if="hasNewVersion">
442439
{{ t('core.header.version.hasNewVersion') }}
443440
</small>
444-
<small v-else-if="dashboardHasNewVersion && !isElectronApp">
441+
<small v-else-if="dashboardHasNewVersion && !isDesktopReleaseMode">
445442
{{ t('core.header.version.dashboardHasNewVersion') }}
446443
</small>
447444
</div>
@@ -524,7 +521,7 @@ onMounted(async () => {
524521
<v-icon>mdi-arrow-up-circle</v-icon>
525522
</template>
526523
<v-list-item-title>{{ t('core.header.updateDialog.title') }}</v-list-item-title>
527-
<template v-slot:append v-if="hasNewVersion || (dashboardHasNewVersion && !isElectronApp)">
524+
<template v-slot:append v-if="hasNewVersion || (dashboardHasNewVersion && !isDesktopReleaseMode)">
528525
<v-chip size="x-small" color="primary" variant="tonal" class="ml-2">!</v-chip>
529526
</template>
530527
</v-list-item>

dashboard/src/types/electron-bridge.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ export {};
33
declare global {
44
interface Window {
55
astrbotDesktop?: {
6-
isElectron: boolean;
7-
isElectronRuntime: () => Promise<boolean>;
6+
isDesktop: boolean;
7+
isDesktopRuntime: () => Promise<boolean>;
88
getBackendState: () => Promise<{
99
running: boolean;
1010
spawning: boolean;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type DesktopRuntimeInfo = {
2+
bridge: Window['astrbotDesktop'] | undefined
3+
hasDesktopRuntimeProbe: boolean
4+
hasDesktopRestartCapability: boolean
5+
isDesktopRuntime: boolean
6+
}
7+
8+
export async function getDesktopRuntimeInfo(): Promise<DesktopRuntimeInfo> {
9+
const bridge = window.astrbotDesktop
10+
const hasDesktopRuntimeProbe =
11+
!!bridge && typeof bridge.isDesktopRuntime === 'function'
12+
const hasDesktopRestartCapability =
13+
!!bridge &&
14+
typeof bridge.restartBackend === 'function' &&
15+
hasDesktopRuntimeProbe
16+
17+
let isDesktopRuntime = !!bridge?.isDesktop
18+
if (hasDesktopRuntimeProbe) {
19+
try {
20+
isDesktopRuntime = isDesktopRuntime || !!(await bridge.isDesktopRuntime())
21+
} catch (error) {
22+
console.warn('[desktop-runtime] Failed to detect desktop runtime.', error)
23+
}
24+
}
25+
26+
return {
27+
bridge,
28+
hasDesktopRuntimeProbe,
29+
hasDesktopRestartCapability,
30+
isDesktopRuntime,
31+
}
32+
}

dashboard/src/utils/restartAstrBot.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import { getDesktopRuntimeInfo } from '@/utils/desktopRuntime'
23

34
type WaitingForRestartRef = {
45
check: (initialStartTime?: number | null) => void | Promise<void>
@@ -27,9 +28,10 @@ async function fetchCurrentStartTime(): Promise<number | null> {
2728
export async function restartAstrBot(
2829
waitingRef?: WaitingForRestartRef | null
2930
): Promise<void> {
30-
const desktopBridge = window.astrbotDesktop
31+
const { bridge: desktopBridge, hasDesktopRestartCapability, isDesktopRuntime } =
32+
await getDesktopRuntimeInfo()
3133

32-
if (desktopBridge?.isElectron) {
34+
if (desktopBridge && hasDesktopRestartCapability && isDesktopRuntime) {
3335
const authToken = localStorage.getItem('token')
3436
const initialStartTime = await fetchCurrentStartTime()
3537
try {

0 commit comments

Comments
 (0)