Files
ComfyUI_frontend/src/stores/systemStatsStore.ts
Christian Byrne 6c8473e4e4 refactor: replace runtime isElectron() with build-time isDesktop constant (#8710)
## Summary

Replace all runtime `isElectron()` function calls with the build-time
`isDesktop` constant from `@/platform/distribution/types`, enabling
dead-code elimination in non-desktop builds.

## Changes

- **What**: Migrate 30 files from runtime `isElectron()` detection
(checking `window.electronAPI`) to the compile-time `isDesktop` constant
(driven by `__DISTRIBUTION__` Vite define). Remove `isElectron` from
`envUtil.ts`. Update `isNativeWindow()` to use `isDesktop`. Guard
`electronAPI()` calls behind `isDesktop` checks in stores. Update 7 test
files to use `vi.hoisted` + getter mock pattern for per-test `isDesktop`
toggling. Add `DISTRIBUTION=desktop` to `dev:electron` script.

## Review Focus

- The `electronDownloadStore.ts` now guards the top-level
`electronAPI()` call behind `isDesktop` to prevent crashes on
non-desktop builds.
- Test mocking pattern uses `vi.hoisted` with a getter to allow per-test
toggling of the `isDesktop` value.
- Pre-existing issues not addressed: `as ElectronAPI` cast in
`envUtil.ts`, `:class="[]"` in `BaseViewTemplate.vue`,
`@ts-expect-error` in `ModelLibrarySidebarTab.vue`.
- This subsumes PR #8627 and renders PR #6122 and PR #7374 obsolete.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8710-refactor-replace-runtime-isElectron-with-build-time-isDesktop-constant-3006d73d365081c08037f0e61c2f6c77)
by [Unito](https://www.unito.io)
2026-02-07 19:47:05 -08:00

75 lines
1.6 KiB
TypeScript

import { useAsyncState } from '@vueuse/core'
import { defineStore } from 'pinia'
import { isCloud, isDesktop } from '@/platform/distribution/types'
import type { SystemStats } from '@/schemas/apiSchema'
import { api } from '@/scripts/api'
export const useSystemStatsStore = defineStore('systemStats', () => {
const fetchSystemStatsData = async () => {
try {
return await api.getSystemStats()
} catch (err) {
console.error('Error fetching system stats:', err)
throw err
}
}
const {
state: systemStats,
isLoading,
error,
isReady: isInitialized,
execute: refetchSystemStats
} = useAsyncState<SystemStats | null>(
fetchSystemStatsData,
null, // initial value
{
immediate: true
}
)
function getFormFactor(): string {
if (isCloud) {
return 'cloud'
}
if (!systemStats.value?.system?.os) {
return 'other'
}
const os = systemStats.value.system.os.toLowerCase()
if (isDesktop) {
if (os.includes('windows')) {
return 'desktop-windows'
}
if (os.includes('darwin') || os.includes('mac')) {
return 'desktop-mac'
}
} else {
// Git/source installation
if (os.includes('windows')) {
return 'git-windows'
}
if (os.includes('darwin') || os.includes('mac')) {
return 'git-mac'
}
if (os.includes('linux')) {
return 'git-linux'
}
}
return 'other'
}
return {
systemStats,
isLoading,
error,
isInitialized,
refetchSystemStats,
getFormFactor
}
})