Enable native dialogs across shells
This commit is contained in:
39
packages/ui/src/lib/native/electron/functions.ts
Normal file
39
packages/ui/src/lib/native/electron/functions.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { NativeDialogOptions } from "../native-functions"
|
||||
|
||||
interface ElectronDialogResult {
|
||||
canceled?: boolean
|
||||
paths?: string[]
|
||||
path?: string | null
|
||||
}
|
||||
|
||||
interface ElectronAPI {
|
||||
openDialog?: (options: NativeDialogOptions) => Promise<ElectronDialogResult>
|
||||
}
|
||||
|
||||
function coerceFirstPath(result?: ElectronDialogResult | null): string | null {
|
||||
if (!result || result.canceled) {
|
||||
return null
|
||||
}
|
||||
const paths = Array.isArray(result.paths) ? result.paths : result.path ? [result.path] : []
|
||||
if (paths.length === 0) {
|
||||
return null
|
||||
}
|
||||
return paths[0] ?? null
|
||||
}
|
||||
|
||||
export async function openElectronNativeDialog(options: NativeDialogOptions): Promise<string | null> {
|
||||
if (typeof window === "undefined") {
|
||||
return null
|
||||
}
|
||||
const api = (window as Window & { electronAPI?: ElectronAPI }).electronAPI
|
||||
if (!api?.openDialog) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
const result = await api.openDialog(options)
|
||||
return coerceFirstPath(result)
|
||||
} catch (error) {
|
||||
console.error("[native] electron dialog failed", error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user