Skip to content

Commit

Permalink
migrate settings apis to new api
Browse files Browse the repository at this point in the history
  • Loading branch information
astudentinearth committed Jan 8, 2025
1 parent 9a9ef8c commit a753925
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
DEFAULT_USER_SETTINGS,
DarkwriteDesktopClientInfo,
UserSettings,
buildUserSettings,
} from "@darkwrite/common";
import log from "electron-log/main.js";
import { FileHandle, open, writeFile } from "fs/promises";
import { SETTINGS_PATH } from "../lib/paths";
import { isNodeError, isValidJSON } from "../util";
import { app } from "electron";
import os from "os";

const filename = SETTINGS_PATH;

Expand Down Expand Up @@ -62,3 +65,13 @@ export async function writeUserPrefs(prefs: UserSettings) {
if (isNodeError(err)) log.error(err.message);
}
}

export function getAppInfo(): DarkwriteDesktopClientInfo {
return {
isPackaged: app.isPackaged,
version: app.getVersion(),
os: `${os.type()} ${os.release()}`,
nodeVersion: process.versions.node,
electronVersion: process.versions.electron,
};
}
6 changes: 6 additions & 0 deletions packages/app-desktop/src/electron/ipc/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
import * as NoteAPI from "@main/api/note.electron";
import * as SettingsAPI from "@main/api/settings.electron"
// @ts-ignore
import {ImportAPI as FileImportAPI} from "@main/api/import"
import { deepAssign, find, recursiveKeys } from "@darkwrite/common";
Expand All @@ -21,6 +22,11 @@ export const DarkwriteElectronAPI = {
export: new IPCHandler(false, NoteAPI.exportNote),
import: new IPCHandler(false, FileImportAPI.importFile),
},
settings: {
load: new IPCHandler(false, SettingsAPI.readUserPrefs),
save: new IPCHandler(false, SettingsAPI.writeUserPrefs),
getClientInfo: new IPCHandler(false, SettingsAPI.getAppInfo)
}
} satisfies DarkwriteAPI;
export type DarkwritePreloadAPI = InferPreloadAPI<typeof DarkwriteElectronAPI>;

Expand Down
3 changes: 2 additions & 1 deletion packages/app-desktop/src/electron/ipc/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @deprecated */
import { DarkwriteDesktopClientInfo, UserSettings } from "@darkwrite/common";
import { app, ipcMain } from "electron";
import { readUserPrefs, writeUserPrefs } from "../api/settings";
import { readUserPrefs, writeUserPrefs } from "../api/settings.electron";
import { ChannelNames } from "../channels";
import os from "os";

Expand Down
2 changes: 1 addition & 1 deletion packages/app-desktop/src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { initAppMenu } from "./menu";
} from "electron-devtools-installer";*/
import { DEFAULT_USER_SETTINGS } from "@darkwrite/common";
import { fileURLToPath } from "node:url";
import { readUserPrefs } from "./api/settings";
import { readUserPrefs } from "./api/settings.electron";
import { Paths } from "./lib/paths";
import { constructWindow } from "./window";
import "./ipc/api"
Expand Down
2 changes: 1 addition & 1 deletion packages/app-desktop/src/lib/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DarkwriteDesktopClientInfo } from "@darkwrite/common";
export const ClientAPI = {
getClientInfo: async (): Promise<DarkwriteDesktopClientInfo> => {
if (window.api) {
return await window.api.getClientInfo();
return await window.newApi.settings.getClientInfo();
}
return {
electronVersion: "",
Expand Down
4 changes: 2 additions & 2 deletions packages/app-desktop/src/lib/api/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { UserSettings } from "@darkwrite/common";

export const SettingsModel = {
async save(data: UserSettings) {
window.api.settings.save(data);
window.newApi.settings.save(data);
},
async load() {
return await window.api.settings.load();
return await window.newApi.settings.load();
},
};

0 comments on commit a753925

Please sign in to comment.