99 lines
2.7 KiB
TypeScript
99 lines
2.7 KiB
TypeScript
import { isDesktop } from './platform';
|
|
|
|
export type DesktopUpdatePhase =
|
|
| 'checking'
|
|
| 'uptodate'
|
|
| 'available'
|
|
| 'downloading'
|
|
| 'installing'
|
|
| 'relaunching'
|
|
| 'error';
|
|
|
|
export interface DesktopUpdateProgress {
|
|
phase: DesktopUpdatePhase;
|
|
message: string;
|
|
currentVersion?: string;
|
|
newVersion?: string;
|
|
downloadPercent?: number;
|
|
downloadedBytes?: number;
|
|
totalBytes?: number;
|
|
}
|
|
|
|
export interface DesktopUpdateResult {
|
|
ok: boolean;
|
|
phase: DesktopUpdatePhase;
|
|
message: string;
|
|
currentVersion?: string;
|
|
newVersion?: string;
|
|
}
|
|
|
|
export interface DesktopBridge {
|
|
openWidgetWindow: (instance: import('../types/floatingWidget').FloatingWidgetInstance) => Promise<void>;
|
|
closeWidgetWindow: (id: string) => Promise<void>;
|
|
focusWidgetWindow: (id: string) => Promise<void>;
|
|
showNativeNotification: (title: string, body: string) => Promise<void>;
|
|
checkForUpdates: () => Promise<{ available: boolean; version?: string; message?: string }>;
|
|
runDesktopUpdate: (
|
|
onProgress?: (progress: DesktopUpdateProgress) => void,
|
|
) => Promise<DesktopUpdateResult>;
|
|
getAppVersion: () => Promise<string>;
|
|
syncMainWindowTitle: () => Promise<string>;
|
|
}
|
|
|
|
declare global {
|
|
interface Window {
|
|
__goldenDesktopBridge?: DesktopBridge;
|
|
}
|
|
}
|
|
|
|
export function getDesktopBridge(): DesktopBridge | null {
|
|
if (!isDesktop()) return null;
|
|
return window.__goldenDesktopBridge ?? null;
|
|
}
|
|
|
|
export async function showDesktopNativeNotification(title: string, body: string): Promise<void> {
|
|
const bridge = getDesktopBridge();
|
|
if (!bridge) return;
|
|
try {
|
|
await bridge.showNativeNotification(title, body);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
export async function checkDesktopUpdates(): Promise<{ available: boolean; version?: string; message?: string }> {
|
|
const bridge = getDesktopBridge();
|
|
if (!bridge) return { available: false, message: '데스크톱 앱에서만 사용 가능합니다.' };
|
|
return bridge.checkForUpdates();
|
|
}
|
|
|
|
export async function runDesktopUpdate(
|
|
onProgress?: (progress: DesktopUpdateProgress) => void,
|
|
): Promise<DesktopUpdateResult> {
|
|
const bridge = getDesktopBridge();
|
|
if (!bridge?.runDesktopUpdate) {
|
|
return { ok: false, phase: 'error', message: '데스크톱 앱에서만 사용 가능합니다.' };
|
|
}
|
|
return bridge.runDesktopUpdate(onProgress);
|
|
}
|
|
|
|
export async function getDesktopAppVersion(): Promise<string | null> {
|
|
const bridge = getDesktopBridge();
|
|
if (!bridge) return null;
|
|
try {
|
|
return await bridge.getAppVersion();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function syncDesktopMainWindowTitle(): Promise<string | null> {
|
|
const bridge = getDesktopBridge();
|
|
if (!bridge?.syncMainWindowTitle) return null;
|
|
try {
|
|
return await bridge.syncMainWindowTitle();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|