config system + move api to frontend

This commit is contained in:
nicwands
2026-03-11 12:26:40 -04:00
parent efc9c73751
commit a1b339f668
26 changed files with 10833 additions and 1631 deletions

View File

@@ -4,8 +4,7 @@ import { app, shell, BrowserWindow, ipcMain } from 'electron'
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
import supabasePlugin from '@takerofnotes/plugin-supabase'
import PluginRegistry from './core/PluginRegistry.js'
import PluginConfig from './core/PluginConfig.js'
import NotesAPI from './core/NotesAPI.js'
import Config from './core/Config.js'
import { join } from 'path'
const preloadPath = join(__dirname, '../preload/index.mjs')
@@ -79,23 +78,51 @@ app.whenReady().then(async () => {
registry.register(supabasePlugin)
// Pull plugin config
const config = await new PluginConfig(filesystemPlugin).load()
const config = new Config(filesystemPlugin)
const initialConfig = await config.load()
// Create instance of active adapter
const plugin = registry.get(config.activeAdapter)
// const plugin = registry.get(supabasePlugin.id)
const adapter = plugin.createAdapter(config.adapterConfig)
// const adapter = plugin.createAdapter({
// supabaseKey: process.env.SUPABASE_KEY,
// supabaseUrl: process.env.SUPABASE_URL,
// })
const setActivePlugin = async (pluginId) => {
const currentConfig = await config.load()
await config.write({ ...currentConfig, activeAdapter: pluginId })
// Init Notes API
const notesAPI = new NotesAPI(
adapter,
'729a0d21d783654c68f1a0123e2a0e986350de536b5324f1f35876ea12ffeaf5',
)
await notesAPI.init()
const plugin = registry.get(pluginId)
const adapterConfig = currentConfig.adapters[pluginId] || {}
const adapter = plugin.createAdapter(adapterConfig)
// Initialize adapter
await adapter.init()
// Handle adapter methods via IPC
ipcMain.removeHandler('adapter:call')
ipcMain.handle('adapter:call', async (_, method, args) => {
if (!adapter[method]) {
throw new Error(`Invalid adapter method: ${method}`)
}
return await adapter[method](...args)
})
return true
}
// Set active plugin
await setActivePlugin(initialConfig.activeAdapter)
// Get/set config
ipcMain.handle('getConfig', async () => {
return await config.load()
})
ipcMain.handle('setConfig', async (_, newConfig) => {
await config.write(newConfig)
})
// Get/set plugins
ipcMain.handle('listPlugins', async () => {
return registry.list()
})
ipcMain.handle('setActivePlugin', async (_, pluginId) => {
return await setActivePlugin(pluginId)
})
// Broadcast note changes to all windows
const broadcastNoteChange = (event, data) => {
@@ -104,26 +131,9 @@ app.whenReady().then(async () => {
})
}
// Handle Notes API
ipcMain.handle('notesAPI:call', async (_, method, args) => {
if (!notesAPI[method]) {
throw new Error('Invalid method')
}
const result = await notesAPI[method](...args)
// Broadcast changes to all windows
if (method === 'createNote') {
broadcastNoteChange('note-created', result)
} else if (method === 'updateNote') {
broadcastNoteChange('note-updated', result)
} else if (method === 'updateNoteMetadata') {
broadcastNoteChange('note-updated', result)
} else if (method === 'deleteNote') {
broadcastNoteChange('note-deleted', { id: args[0] })
}
return result
// Handle note change events from renderer
ipcMain.on('note-changed', (_, event, data) => {
broadcastNoteChange(event, data)
})
electronApp.setAppUserModelId('com.electron')