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

feat: add the ability to open files from the UI #486

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions components/chat/chatBar/upload/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ const Files: React.FC<FilesProps> = ({ files, setFiles }) => {
color="primary"
radius="full"
onPress={async () =>
window.alert(
'Coming soon, we promise! This will open your file.'
// This submits a request to the electron main process to open the file
(window as any).electronAPI.openFile(
Copy link
Contributor

@ryanhopperlowe ryanhopperlowe Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you globally update the window object to add TypeScript support?

ideally add a file @/types/window.d.ts with these contents:

declare global {
  interface Window {
    electronApi: <ElectronApiType>; // You may have to type this yourself
  }
}

export {} // this is required to allow the types to be globally accessible throughout the application

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

path.join(file.path, file.name)
)
}
/>
Expand Down
6 changes: 6 additions & 0 deletions electron/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { existsSync, mkdirSync, writeFileSync } from 'fs';
import fixPath from 'fix-path';
import os from 'os';
import { config } from './config.mjs';
import { ipcMain } from 'electron/main';

app.on('window-all-closed', () => app.quit());
app.on('ready', startServer);
Expand Down Expand Up @@ -73,6 +74,11 @@ function createWindow(url) {
},
});

// This is necessary to allow the renderer process (NextJS) to open files in the default system application
ipcMain.on('open-file', (event, arg) => {
shell.openPath(arg);
});

// Check if the platform is macOS before calling setWindowButtonVisibility
if (isMac) {
win.setWindowButtonVisibility(true);
Expand Down
1 change: 1 addition & 0 deletions electron/preload.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ contextBridge.exposeInMainWorld('electronAPI', {
send: (channel, args) => {
ipcRenderer.send(channel, args);
},
openFile: (file) => ipcRenderer.send('open-file', file),
});
Loading