Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement ipc for file selection #79

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
BrowserWindow,
MessageChannelMain,
app,
dialog,
ipcMain,
safeStorage,
utilityProcess,
} from 'electron/main'

import { Intl, getSystemLocale } from './intl.js'
import { APP_IPC_EVENT_TO_PARAMS_PARSER } from './ipc.js'

const log = debug('comapeo:main:app')

Expand Down Expand Up @@ -226,6 +228,25 @@ function initMainWindow({ appMode, services }) {
event.senderFrame?.postMessage('provide-comapeo-port', null, [port2])
})

// Set up IPC specific to the main window
mainWindow.webContents.ipc.handle('files:select', async (_event, params) => {
const parsedParams = APP_IPC_EVENT_TO_PARAMS_PARSER['files:select'](params)

const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: parsedParams?.extensionFilters
? [
{
name: 'Custom file type',
extensions: parsedParams.extensionFilters,
},
]
: undefined,
})

return result.filePaths[0]
})

APP_STATE.browserWindows.set(mainWindow, {
type: 'main',
})
Expand Down
29 changes: 29 additions & 0 deletions src/main/ipc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
array,
object,
optional,
parse,
string,
undefined,
union,
} from 'valibot'

const FilesSelectParamsSchema = union([
object({
extensionFilters: optional(array(string())),
}),
undefined(),
])

export const APP_IPC_EVENT_TO_PARAMS_PARSER = /** @type {const} */ ({
/**
* @param {unknown} value
*
* @returns {import('valibot').InferOutput<typeof FilesSelectParamsSchema>}
*/
'files:select': (value) => {
return parse(FilesSelectParamsSchema, value)
},
})

/** @typedef {keyof typeof APP_IPC_EVENT_TO_PARAMS_PARSER} AppIPCEvents */
17 changes: 16 additions & 1 deletion src/preload/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,27 @@ const runtimeApi = {
// Locale
async getLocale() {
const locale = await ipcRenderer.invoke('locale:get')
if (typeof locale !== 'string') throw Error('Locale must be a string')
if (typeof locale !== 'string') {
throw new Error('Locale must be a string')
}
return locale
},
updateLocale(locale) {
ipcRenderer.send('locale:update', locale)
},

// Files
async selectFile(extensionFilters) {
const filePath = await ipcRenderer.invoke('files:select', {
extensionFilters,
})

if (!(typeof filePath === 'string' || typeof filePath === 'undefined')) {
throw new Error(`File path is unexpected type: ${typeof filePath}`)
}

return filePath
},
}

contextBridge.exposeInMainWorld('runtime', runtimeApi)
1 change: 1 addition & 0 deletions src/preload/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type RuntimeApi = {
init: () => void
getLocale: () => Promise<string>
updateLocale: (locale: string) => void
selectFile: (extensionFilters?: Array<string>) => Promise<string | undefined>
}
13 changes: 13 additions & 0 deletions src/renderer/src/hooks/mutations/file-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useMutation } from '@tanstack/react-query'

/**
* If the resolved value is `undefined` that means the user did not select a
* file (i.e. cancelled the selection dialog)
*/
export function useSelectProjectConfigFile() {
return useMutation({
mutationFn: async () => {
return window.runtime.selectFile(['comapeocat'])
},
})
}
Loading