Skip to content

Commit

Permalink
feat: communicate existence of openaiapikey (blib-la#93)
Browse files Browse the repository at this point in the history
## Motivation

<!-- List motivation and changes here -->
live communication of openAiApiKey existence
allows other windows to be up to date of the key exists or not
## Issues closed

<!-- List closed issues here -->
  • Loading branch information
pixelass committed Mar 19, 2024
1 parent e3ae3cc commit 3cd2efb
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/client/apps/story/index.tsx
Expand Up @@ -112,7 +112,7 @@ export function Story() {

useEffect(() => {
window.ipc.send("hasOpenAiApiKey");
const unsubscribe = window.ipc.on("openAiApiKey", (hasKey: boolean) => {
const unsubscribe = window.ipc.on("hasOpenAiApiKey", (hasKey: boolean) => {
setHasOpenAiApiKey(hasKey);
});
return () => {
Expand Down
2 changes: 1 addition & 1 deletion src/electron/future/ipc/keys.ts
Expand Up @@ -3,5 +3,5 @@ import { ipcMain } from "electron";
import { keyStore } from "@/stores";

ipcMain.on("hasOpenAiApiKey", event => {
event.sender.send("openAiApiKey", Boolean(keyStore.get("openAiApiKey")));
event.sender.send("hasOpenAiApiKey", Boolean(keyStore.get("openAiApiKey")));
});
2 changes: 2 additions & 0 deletions src/electron/future/ipc/listeners.ts
Expand Up @@ -6,6 +6,7 @@ import { ipcMain } from "electron";
import { buildKey } from "#/build-key";
import { ID } from "#/enums";
import { keyStore, userStore } from "@/stores";
import { sendToAllWindows } from "@/stores/watchers";
import { clone, lfs } from "@/utils/git";
import { getCaptainData } from "@/utils/path-helpers";
import { readFilesRecursively } from "@/utils/read-files-recursively";
Expand All @@ -16,6 +17,7 @@ ipcMain.on(buildKey([ID.USER], { suffix: ":language" }), (_event, language) => {

ipcMain.on(buildKey([ID.KEYS], { suffix: ":set-openAiApiKey" }), (_event, openAiApiKey) => {
keyStore.set("openAiApiKey", openAiApiKey);
sendToAllWindows("hasOpenAiApiKey", Boolean(keyStore.get("openAiApiKey")));
});

ipcMain.on(buildKey([ID.KEYS], { suffix: ":get-openAiApiKey" }), event => {
Expand Down
4 changes: 2 additions & 2 deletions src/electron/future/stores/watchers.ts
Expand Up @@ -4,14 +4,14 @@ import { BrowserWindow } from "electron";

import { downloadsStore, inventoryStore, userStore } from "@/stores";

function sendToFocusedWindow<T>(key: string, value: T) {
export function sendToFocusedWindow<T>(key: string, value: T) {
const window_ = BrowserWindow.getFocusedWindow();
if (window_) {
window_.webContents.send(key, value);
}
}

function sendToAllWindows<T>(key: string, value: T) {
export function sendToAllWindows<T>(key: string, value: T) {
const windows_ = BrowserWindow.getAllWindows();
for (const window_ of windows_) {
window_.webContents.send(key, value);
Expand Down

0 comments on commit 3cd2efb

Please sign in to comment.