Enhance path picking functionality in Electron and LibraryManager

This commit is contained in:
2026-03-20 09:49:37 +01:00
parent b813e88634
commit b5d4085a4b
3 changed files with 30 additions and 14 deletions

View File

@@ -475,9 +475,20 @@ ipcMain.handle('update-settings', (event, settings) => {
return true return true
}) })
ipcMain.handle('pick-paths', async () => { function pickDialogProperties(kind) {
if (kind === 'directories') {
return ['openDirectory', 'multiSelections']
}
if (kind === 'mixed') {
return ['openFile', 'openDirectory', 'multiSelections']
}
return ['openFile', 'multiSelections']
}
ipcMain.handle('pick-paths', async (event, options = {}) => {
const kind = typeof options?.kind === 'string' ? options.kind : 'files'
const result = await dialog.showOpenDialog(mainWindow, { const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'openDirectory', 'multiSelections'], properties: pickDialogProperties(kind),
}) })
return result.canceled ? [] : result.filePaths return result.canceled ? [] : result.filePaths
}) })

View File

@@ -8,7 +8,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
checkForUpdates: () => ipcRenderer.invoke('check-for-updates'), checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
setSetting: (key, value) => ipcRenderer.invoke('set-setting', key, value), setSetting: (key, value) => ipcRenderer.invoke('set-setting', key, value),
updateSettings: (settings) => ipcRenderer.invoke('update-settings', settings), updateSettings: (settings) => ipcRenderer.invoke('update-settings', settings),
pickPaths: () => ipcRenderer.invoke('pick-paths'), pickPaths: (options = {}) => ipcRenderer.invoke('pick-paths', options),
openPath: (filePath) => ipcRenderer.invoke('open-path', filePath), openPath: (filePath) => ipcRenderer.invoke('open-path', filePath),
openExternalLink: (event) => { openExternalLink: (event) => {
event.preventDefault(); event.preventDefault();

View File

@@ -121,19 +121,23 @@ export default function LibraryManager({
} }
} }
async function addPaths() { async function registerPaths(paths) {
await runAction(async () => {
const response = await fetch(`${apiBase}/libraries/${library.slug}/files/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths })
})
await expectOk(response)
})
}
async function addPaths(kind = 'files') {
if (!library) return if (!library) return
const paths = await window.electronAPI?.pickPaths?.() const paths = await window.electronAPI?.pickPaths?.({ kind })
if (!Array.isArray(paths) || paths.length === 0) return if (!Array.isArray(paths) || paths.length === 0) return
try { try {
await runAction(async () => { await registerPaths(paths)
const response = await fetch(`${apiBase}/libraries/${library.slug}/files/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths })
})
await expectOk(response)
})
} catch (error) { } catch (error) {
setErrorMessage(String(error?.message || error)) setErrorMessage(String(error?.message || error))
} }
@@ -346,7 +350,8 @@ export default function LibraryManager({
</div> </div>
<div className="library-footer-actions"> <div className="library-footer-actions">
<button className="button" disabled={busy} onClick={addPaths}>Add Files</button> <button className="button" disabled={busy} onClick={() => addPaths('files')}>Add Files</button>
<button className="button ghost" disabled={busy} onClick={() => addPaths('directories')}>Add Folder</button>
{library.files?.length > 0 && !isSyncing && !isReadyForChat && ( {library.files?.length > 0 && !isSyncing && !isReadyForChat && (
<button className="button ghost" disabled={busy} onClick={retrySync}>Retry Sync</button> <button className="button ghost" disabled={busy} onClick={retrySync}>Retry Sync</button>
)} )}