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

fix: use electron store #2644

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions apps/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"electron-context-menu": "4.0.4",
"electron-log": "5.2.4",
"electron-squirrel-startup": "1.0.1",
"electron-store": "^10.0.0",
"electron-updater": "^6.3.9",
"es-toolkit": "1.29.0",
"fast-folder-size": "2.3.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/main/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const registerPushNotifications = async () => {
debug: true,
firebase: JSON.parse(env.VITE_FIREBASE_CONFIG),
persistentIds: persistentIds || [],
credentials: credentials || null,
credentials: credentials || undefined,
bundleId: "is.follow",
chromeId: "is.follow",
})
Expand Down
45 changes: 12 additions & 33 deletions apps/main/src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
import { resolve } from "node:path"
import type { Credentials } from "@eneris/push-receiver/dist/types"
import Store from "electron-store"

import { app } from "electron"
import { JSONFileSyncPreset } from "lowdb/node"

let db: {
data: Record<string, unknown>
write: () => void
read: () => void
}

const createOrGetDb = () => {
if (!db) {
db = JSONFileSyncPreset(resolve(app.getPath("userData"), "db.json"), {}) as typeof db
}
return db
// @keep-sorted
type StoreData = {
"notifications-credentials"?: Credentials | null
"notifications-persistent-ids"?: string[] | null
appearance?: "light" | "dark" | "system" | null
betterAuthSessionCookie?: string | null
cacheSizeLimit?: number | null
proxy?: string | null
user?: string | null
}
export const store = new Store<StoreData>({ name: "db" })

export enum StoreKey {
CacheSizeLimit = "cacheSizeLimit",
}

export const store = {
get: (key: string) => {
const db = createOrGetDb()

return db.data[key] as any
},
set: (key: string, value: any) => {
const db = createOrGetDb()
db.data[key] = value
db.write()
},
delete: (key: string) => {
const db = createOrGetDb()
delete db.data[key]
db.write()
},
}
2 changes: 1 addition & 1 deletion apps/main/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { store } from "./store"

const BetterAuthKey = "betterAuthSessionCookie"
export const setBetterAuthSessionCookie = (cookie: string) => store.set(BetterAuthKey, cookie)
export const getBetterAuthSessionCookie = (): string | null => store.get(BetterAuthKey)
export const getBetterAuthSessionCookie = (): string | null => store.get(BetterAuthKey) || null
export const cleanBetterAuthSessionCookie = () => store.set(BetterAuthKey, null)

const UserKey = "user"
Expand Down
4 changes: 2 additions & 2 deletions apps/renderer/src/hooks/biz/useProxySetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { tipcClient } from "~/lib/client"
const proxyAtom = atom("")

proxyAtom.onMount = (setAtom) => {
tipcClient?.getProxyConfig().then((proxy: string) => {
setAtom(proxy)
tipcClient?.getProxyConfig().then((proxy) => {
setAtom(proxy || "")
})
}

Expand Down
79 changes: 79 additions & 0 deletions pnpm-lock.yaml

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

Loading