feat: basic browsing (#1)
This commit is contained in:
parent
824e2ea9d5
commit
9b0251240c
104 changed files with 5624 additions and 122 deletions
85
src/main/services/storage.service.ts
Normal file
85
src/main/services/storage.service.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import { TConfig, TRelayGroup, TThemeSetting } from '@common/types'
|
||||
import { app, ipcMain } from 'electron'
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export class StorageService {
|
||||
private storage: Storage
|
||||
|
||||
constructor() {
|
||||
this.storage = new Storage()
|
||||
}
|
||||
|
||||
init() {
|
||||
ipcMain.handle('storage:getRelayGroups', () => this.getRelayGroups())
|
||||
ipcMain.handle('storage:setRelayGroups', (_, relayGroups: TRelayGroup[]) =>
|
||||
this.setRelayGroups(relayGroups)
|
||||
)
|
||||
}
|
||||
|
||||
getRelayGroups(): TRelayGroup[] {
|
||||
return (
|
||||
this.storage.get('relayGroups') ?? [
|
||||
{
|
||||
groupName: 'Global',
|
||||
relayUrls: [
|
||||
'wss://relay.damus.io/',
|
||||
'wss://nos.lol/',
|
||||
'wss://nostr.mom/',
|
||||
'wss://relay.primal.net/'
|
||||
],
|
||||
isActive: true
|
||||
}
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
setRelayGroups(relayGroups: TRelayGroup[]) {
|
||||
this.storage.set('relayGroups', relayGroups)
|
||||
}
|
||||
|
||||
getTheme() {
|
||||
return this.storage.get('theme') ?? 'system'
|
||||
}
|
||||
|
||||
setTheme(theme: TThemeSetting) {
|
||||
this.storage.set('theme', theme)
|
||||
}
|
||||
}
|
||||
|
||||
class Storage {
|
||||
private path: string
|
||||
private config: TConfig
|
||||
private writeTimer: NodeJS.Timeout | null = null
|
||||
|
||||
constructor() {
|
||||
this.path = path.join(app.getPath('userData'), 'config.json')
|
||||
this.checkConfigFile(this.path)
|
||||
const json = readFileSync(this.path, 'utf-8')
|
||||
this.config = JSON.parse(json)
|
||||
}
|
||||
|
||||
get<K extends keyof TConfig, V extends TConfig[K]>(key: K): V | undefined {
|
||||
return this.config[key] as V
|
||||
}
|
||||
|
||||
set<K extends keyof TConfig>(key: K, value: TConfig[K]) {
|
||||
this.config[key] = value
|
||||
if (this.writeTimer) return
|
||||
|
||||
this.writeTimer = setTimeout(() => {
|
||||
this.writeTimer = null
|
||||
writeFileSync(this.path, JSON.stringify(this.config))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
private checkConfigFile(path: string) {
|
||||
try {
|
||||
if (!existsSync(path)) {
|
||||
writeFileSync(path, '{}')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue