Files
JRedisDesktop/src/main/ipc-handlers.ts
T
Jokul ff09f5dc03 feat(phase4): list/set/zset editors, CLI, slow log, DB selector
- ListEditor: pagination, push, edit, remove elements
- SetEditor: filter, add, remove members
- ZsetEditor: filter, add with score, remove members
- CliView: interactive terminal with command history
- SlowLogView: slow log table with time/duration/command
- DbSelector: dropdown to switch Redis databases (0-15)
- StatusBar integrated with DB selector
- MainArea: added CLI and Slow Log tabs
- KeyDetail: dispatch to List/Set/Zset editors
- RedisService: list/set/zset/slowlog/memory operations
- Preload: all new IPC channels typed
2026-07-04 18:07:33 +08:00

183 lines
7.3 KiB
TypeScript

// src/main/ipc-handlers.ts
import { ipcMain, BrowserWindow, nativeTheme, dialog } from 'electron'
import store, { ConnectionConfig } from './store'
import { encrypt, decrypt } from './credential'
import * as redisService from './redis-service'
export function registerIpcHandlers(): void {
// Window
ipcMain.on('window:minimize', () => BrowserWindow.getFocusedWindow()?.minimize())
ipcMain.on('window:maximize', () => {
const win = BrowserWindow.getFocusedWindow()
if (win) win.isMaximized() ? win.unmaximize() : win.maximize()
})
ipcMain.on('window:close', () => BrowserWindow.getFocusedWindow()?.close())
// Theme
ipcMain.handle('theme:get', () => nativeTheme.shouldUseDarkColors)
ipcMain.on('theme:set', (_e, theme: 'system' | 'dark' | 'light') => {
nativeTheme.themeSource = theme
})
// Dialog
ipcMain.handle('dialog:openFile', async (_e, options: Electron.OpenDialogOptions) => {
const win = BrowserWindow.getFocusedWindow()
if (!win) return null
const result = await dialog.showOpenDialog(win, options)
if (result.canceled || result.filePaths.length === 0) return null
const fs = await import('fs')
try {
return { path: result.filePaths[0], content: fs.readFileSync(result.filePaths[0], 'utf-8') }
} catch { return null }
})
// Credential
ipcMain.handle('credential:encrypt', (_e, text: string) => encrypt(text))
ipcMain.handle('credential:decrypt', (_e, encoded: string) => decrypt(encoded))
// Storage - connections
ipcMain.handle('storage:getConnections', () => store.get('connections', []))
ipcMain.handle('storage:saveConnection', (_e, conn: ConnectionConfig) => {
const connections = store.get('connections', [])
const idx = connections.findIndex((c: ConnectionConfig) => c.id === conn.id)
if (idx >= 0) connections[idx] = conn
else connections.push(conn)
store.set('connections', connections)
return connections
})
ipcMain.handle('storage:deleteConnection', (_e, id: string) => {
const connections = store.get('connections', []).filter((c: ConnectionConfig) => c.id !== id)
store.set('connections', connections)
return connections
})
ipcMain.handle('storage:reorderConnections', (_e, ids: string[]) => {
const connections = store.get('connections', [])
const reordered = ids.map((id, i) => {
const conn = connections.find((c: ConnectionConfig) => c.id === id)!
conn.order = i
return conn
})
store.set('connections', reordered)
return reordered
})
// Storage - settings
ipcMain.handle('storage:getSettings', () => store.get('settings'))
ipcMain.handle('storage:saveSettings', (_e, settings) => {
store.set('settings', settings)
})
// Redis
ipcMain.handle('redis:connect', async (_e, id: string, opts) => {
try {
await redisService.connect(id, opts)
return { success: true }
} catch (err: any) {
return { success: false, error: err.message }
}
})
ipcMain.handle('redis:disconnect', async (_e, id: string) => {
await redisService.disconnect(id)
})
ipcMain.handle('redis:execute', async (_e, id: string, command: string, args: string[]) => {
return redisService.execute(id, command, args)
})
ipcMain.handle('redis:getInfo', async (_e, id: string) => {
return redisService.getServerInfo(id)
})
ipcMain.handle('redis:scanKeys', async (_e, id: string, cursor: string, pattern: string, count: number) => {
return redisService.scanKeys(id, cursor, pattern, count)
})
ipcMain.handle('redis:getKeyType', async (_e, id: string, key: string) => {
return redisService.getKeyType(id, key)
})
ipcMain.handle('redis:getKeyTTL', async (_e, id: string, key: string) => {
return redisService.getKeyTTL(id, key)
})
ipcMain.handle('redis:getString', async (_e, id: string, key: string) => {
return redisService.getStringValue(id, key)
})
ipcMain.handle('redis:setString', async (_e, id: string, key: string, value: string) => {
await redisService.setStringValue(id, key, value)
})
ipcMain.handle('redis:deleteKey', async (_e, id: string, key: string) => {
await redisService.deleteKey(id, key)
})
ipcMain.handle('redis:hashScan', async (_e, id: string, key: string, cursor: string, count: number) => {
return redisService.getHashFields(id, key, cursor, count)
})
ipcMain.handle('redis:hashSet', async (_e, id: string, key: string, field: string, value: string) => {
await redisService.setHashField(id, key, field, value)
})
ipcMain.handle('redis:hashDel', async (_e, id: string, key: string, field: string) => {
await redisService.deleteHashField(id, key, field)
})
ipcMain.handle('redis:setTTL', async (_e, id: string, key: string, ttl: number) => {
await redisService.setKeyTTL(id, key, ttl)
})
ipcMain.handle('redis:selectDb', async (_e, id: string, db: number) => {
await redisService.selectDb(id, db)
})
ipcMain.handle('redis:dbSize', async (_e, id: string) => {
return redisService.dbSize(id)
})
// List
ipcMain.handle('redis:listRange', async (_e, id: string, key: string, start: number, stop: number) => {
return redisService.listRange(id, key, start, stop)
})
ipcMain.handle('redis:listLength', async (_e, id: string, key: string) => {
return redisService.listLength(id, key)
})
ipcMain.handle('redis:listPush', async (_e, id: string, key: string, values: string[]) => {
return redisService.listPush(id, key, ...values)
})
ipcMain.handle('redis:listSet', async (_e, id: string, key: string, index: number, value: string) => {
await redisService.listSet(id, key, index, value)
})
ipcMain.handle('redis:listRemove', async (_e, id: string, key: string, count: number, value: string) => {
return redisService.listRemove(id, key, count, value)
})
// Set
ipcMain.handle('redis:setMembers', async (_e, id: string, key: string) => {
return redisService.setMembers(id, key)
})
ipcMain.handle('redis:setSize', async (_e, id: string, key: string) => {
return redisService.setSize(id, key)
})
ipcMain.handle('redis:setAdd', async (_e, id: string, key: string, members: string[]) => {
return redisService.setAdd(id, key, ...members)
})
ipcMain.handle('redis:setRemove', async (_e, id: string, key: string, members: string[]) => {
return redisService.setRemove(id, key, ...members)
})
// Sorted set
ipcMain.handle('redis:zsetRange', async (_e, id: string, key: string, start: number, stop: number, withScores: boolean) => {
return redisService.zsetRange(id, key, start, stop, withScores)
})
ipcMain.handle('redis:zsetSize', async (_e, id: string, key: string) => {
return redisService.zsetSize(id, key)
})
ipcMain.handle('redis:zsetAdd', async (_e, id: string, key: string, score: number, member: string) => {
return redisService.zsetAdd(id, key, score, member)
})
ipcMain.handle('redis:zsetRemove', async (_e, id: string, key: string, members: string[]) => {
return redisService.zsetRemove(id, key, ...members)
})
// Slow log
ipcMain.handle('redis:slowLogGet', async (_e, id: string, count: number) => {
return redisService.slowLogGet(id, count)
})
ipcMain.handle('redis:slowLogLen', async (_e, id: string) => {
return redisService.slowLogLen(id)
})
// Memory
ipcMain.handle('redis:memoryUsage', async (_e, id: string, key: string) => {
return redisService.memoryUsage(id, key)
})
}