Skip to content

Commit

Permalink
Update: Add Error notification via dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
srilakshmikanthanp committed Oct 11, 2023
1 parent cad78fd commit 93735eb
Show file tree
Hide file tree
Showing 9 changed files with 1,584 additions and 1,188 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/electron/constants/ipcevents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export const QUICKNOTE_API_VAL = "QuickNoteAPI";
export const SEND_IN_MAIN_CHAN = "ipc:note:send";

// IPC note receive channel
export const RECV_IN_MAIN_CHAN = "ipc:note:recv";
export const RECV_IN_MAIN_CHAN = "ipc:note:recv";

// IPC to receive Error
export const ONER_IN_MAIN_CHAN = "ipc:evt:error";
22 changes: 16 additions & 6 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT


import { Menu, MenuItem, app, ipcMain } from 'electron';
import { Menu, MenuItem, dialog, app, ipcMain } from 'electron';
import { configure } from 'electron-settings';

import open from 'open';
Expand Down Expand Up @@ -79,16 +79,26 @@ app.on('ready', async () => {
// file store to store the note
const fileStore = new FileStore(path.join(C.APPLICATION_HOME, ".quicknote"));

// ipc event for send
ipcMain.handle(E.SEND_IN_MAIN_CHAN, async () => {
try {
return await fileStore.getNote();
} catch(e) {
dialog.showErrorBox(C.APPLICATION_NAME, e.message);
return "";
}
});

// ipc event for error
ipcMain.on(E.ONER_IN_MAIN_CHAN, async (e, arg) => {
dialog.showErrorBox(C.APPLICATION_NAME, arg);
});

// ipc event for recv
ipcMain.on(E.RECV_IN_MAIN_CHAN, async (e, arg) => {
await fileStore.setNote(arg);
});

// ipc event for send
ipcMain.handle(E.SEND_IN_MAIN_CHAN, async () => {
return await fileStore.getNote();
});

// create the tray window
const noteWindow = new TrayWindow({
tooltip: C.APPLICATION_NAME,
Expand Down
1 change: 1 addition & 0 deletions src/electron/preload/NotePreload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import * as E from "../constants/ipcevents";
contextBridge.exposeInMainWorld(E.QUICKNOTE_API_VAL, {
sendNote: (note: string) => ipcRenderer.send(E.RECV_IN_MAIN_CHAN, note),
recvNote: () => ipcRenderer.invoke(E.SEND_IN_MAIN_CHAN),
onError: (err: string) => ipcRenderer.send(E.ONER_IN_MAIN_CHAN, err),
});
15 changes: 6 additions & 9 deletions src/electron/storage/FIleStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// https://opensource.org/licenses/MIT

import INoteStore from '../interface/INoteStore';
import file from 'node:fs/promises';
import asyncFile from 'node:fs/promises';
import file from 'node:fs';

/**
* FileStore That stores notes in a file
Expand All @@ -16,11 +17,7 @@ export default class FileStore implements INoteStore {
* @returns Note from the file
*/
public async getNote(): Promise<string> {
try {
return await file.readFile(this.file_path, 'utf-8');
} catch (e) {
return "";
}
return await asyncFile.readFile(this.file_path, 'utf-8');
}

/**
Expand All @@ -29,15 +26,15 @@ export default class FileStore implements INoteStore {
* @param note Note to be stored
*/
public async setNote(note: string): Promise<void> {
return await file.writeFile(this.file_path, note);
return await asyncFile.writeFile(this.file_path, note);
}

/**
* Constructor for FileStore
* @param file File to store the note
*/
public constructor(file: string) {
this.file_path = file;
public constructor(name: string) {
file.openSync(this.file_path = name, 'a+');
}


Expand Down
6 changes: 6 additions & 0 deletions src/render/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ export default function App() {
dispatch(SetNote(note));
};

// on error occured
const onError = (err: Error) => {
window.QuickNoteAPI.onError(err.message);
};

// render the App
return (
<NoteEditor
placeHolder={C.PLACEHOLDER_TEXT}
onUpdate={setNote}
noteContent={noteContent}
onError={onError}
/>
);
}
9 changes: 3 additions & 6 deletions src/render/components/NoteEditor/NoteEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface INoteEditorProps {
noteContent: string;
placeHolder: string;
onUpdate?: (content: string) => void;
onError?: (content: Error) => void;
}

// top level container For Scrolling
Expand Down Expand Up @@ -76,18 +77,14 @@ const editorTheme = {
export default function NoteEditor({
noteContent,
onUpdate,
placeHolder
placeHolder,
onError,
}: INoteEditorProps) {
// change handler for note editor
const onChange = (state: EditorState) => {
onUpdate && onUpdate(JSON.stringify(state.toJSON()));
}

// handle the error for note editor
const onError = (error: Error) => {
console.error(error);
}

// editor config
const editorConfig = {
editorState: noteContent,
Expand Down
1 change: 1 addition & 0 deletions src/render/render.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export interface IQuickNoteAPI {
sendNote(note: string): Promise<void>;
recvNote(): Promise<string>;
onError(err: string): Promise<void>;
}

declare global {
Expand Down
Loading

0 comments on commit 93735eb

Please sign in to comment.