-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): integrate RxDB for settings management and refactor settin…
…gs handling
- Loading branch information
Showing
13 changed files
with
1,482 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Plugin } from 'vue' | ||
import { addRxPlugin, createRxDatabase } from 'rxdb' | ||
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie' | ||
|
||
// import typings | ||
import type { RxMqttxCollections, RxMqttxDatabase } from './schemas/RxDB' | ||
|
||
import settingsSchema from './schemas/Settings.schema' | ||
|
||
// import modules | ||
import { disableWarnings, RxDBDevModePlugin } from 'rxdb/plugins/dev-mode' | ||
import { RxDBLeaderElectionPlugin } from 'rxdb/plugins/leader-election' | ||
import { RxDBUpdatePlugin } from 'rxdb/plugins/update' | ||
import { wrappedValidateAjvStorage } from 'rxdb/plugins/validate-ajv' | ||
|
||
const KEY_DATABASE = Symbol('database') | ||
|
||
if (import.meta.env.DEV) { | ||
disableWarnings() | ||
// in dev-mode we add the dev-mode plugin | ||
// which does many checks and adds full error messages | ||
addRxPlugin(RxDBDevModePlugin) | ||
} | ||
addRxPlugin(RxDBLeaderElectionPlugin) | ||
addRxPlugin(RxDBUpdatePlugin) | ||
|
||
export function useDatabase(): RxMqttxDatabase { | ||
return inject<RxMqttxDatabase>(KEY_DATABASE) ?? window.db | ||
} | ||
|
||
export async function createDatabase(): Promise<Plugin> { | ||
const db = await createRxDatabase<RxMqttxCollections>({ | ||
name: 'mqttx', | ||
storage: wrappedValidateAjvStorage({ | ||
storage: getRxStorageDexie(), | ||
}), | ||
eventReduce: true, | ||
}) | ||
|
||
// write to window for debugging | ||
;(window as any).db = db | ||
|
||
// show leadership in title | ||
db.waitForLeadership().then(() => { | ||
document.title = `♛ ${document.title}` | ||
}) | ||
|
||
await db.addCollections({ | ||
settings: { | ||
schema: settingsSchema, | ||
}, | ||
}) | ||
|
||
return { | ||
install(app: any) { | ||
app.provide(KEY_DATABASE, db) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { RxDatabase } from 'rxdb' | ||
import type { RxSettingsCollection } from './Settings.schema' | ||
|
||
export interface RxMqttxCollections { | ||
settings: RxSettingsCollection | ||
} | ||
|
||
export type RxMqttxDatabase = RxDatabase<RxMqttxCollections> | ||
|
||
declare global { | ||
interface Window { | ||
db: RxMqttxDatabase | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { Settings } from 'mqttx' | ||
import type { RxCollection, RxDocument, RxJsonSchema } from 'rxdb' | ||
|
||
export type RxSettingsDocumentType = Settings & { id: string } | ||
|
||
// ORM methods | ||
interface RxSettingsDocMethods { | ||
// hpPercent: () => number | ||
} | ||
|
||
export type RxSettingsDocument = RxDocument<RxSettingsDocumentType, RxSettingsDocMethods> | ||
|
||
export type RxSettingsCollection = RxCollection<RxSettingsDocumentType, RxSettingsDocMethods> | ||
|
||
const settingsSchema: RxJsonSchema<RxSettingsDocumentType> = { | ||
title: 'settings schema', | ||
description: 'describes the settings', | ||
version: 0, | ||
keyCompression: false, | ||
primaryKey: 'id', | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'string', | ||
maxLength: 100, // <- the primary key must have set maxLength | ||
}, | ||
currentLang: { | ||
type: 'string', | ||
default: 'en', | ||
}, | ||
autoCheck: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
autoResub: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
multiTopics: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
maxReconnectTimes: { | ||
type: 'number', | ||
default: 10, | ||
}, | ||
syncOsTheme: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
currentTheme: { | ||
type: 'string', | ||
default: 'light', | ||
}, | ||
jsonHighlight: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
logLevel: { | ||
type: 'string', | ||
default: 'info', | ||
}, | ||
ignoreQoS0Message: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
enableCopilot: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
openAIAPIHost: { | ||
type: 'string', | ||
default: 'https://api.openai.com/v1', | ||
}, | ||
openAIAPIKey: { | ||
type: 'string', | ||
default: '', | ||
}, | ||
model: { | ||
type: 'string', | ||
default: 'gpt-4o', | ||
}, | ||
}, | ||
required: ['id'], | ||
} | ||
|
||
export default settingsSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { RxSettingsDocument, RxSettingsDocumentType } from '@/database/schemas/Settings.schema' | ||
|
||
import type { Subscription } from 'rxjs' | ||
import { useDatabase } from '@/database' | ||
import { useSettingsStore } from '@mqttx/ui' | ||
|
||
export default function useSettingsService() { | ||
const db = useDatabase() | ||
const { settings, updateSettings } = useSettingsStore() | ||
|
||
async function getSettingsInDB(): Promise<Subscription> { | ||
const data = await db.settings.findOne().exec() ?? await updateSettingsInDB() | ||
const sub = data.$.subscribe((data) => { | ||
const { ...settings } = data.toJSON() | ||
updateSettings(settings) | ||
}) | ||
return sub | ||
} | ||
async function updateSettingsInDB(data?: Partial<RxSettingsDocumentType>): Promise<RxSettingsDocument> { | ||
const id = Math.random().toString(36).substring(2) | ||
return db.settings.upsert(data ?? { id }) | ||
} | ||
|
||
if (settings) { | ||
watch(settings, (newSettings) => { | ||
updateSettingsInDB(newSettings) | ||
}) | ||
} | ||
|
||
return { | ||
settings: settings!, | ||
updateSettings, | ||
getSettingsInDB, | ||
updateSettingsInDB, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import { i18n } from '@mqttx/ui/i18n' | ||
import { useSettingsStore } from '@mqttx/ui/stores' | ||
|
||
import App from './App.vue' | ||
import { router } from './router' | ||
|
||
import { createDatabase } from './database' | ||
import useSettingsService from './database/services/SettingsService' | ||
|
||
import { router } from './router' | ||
import '@mqttx/ui/styles.scss' | ||
import './assets/scss/main.scss' | ||
|
||
const database = createDatabase() | ||
|
||
// Create Vue | ||
const app = createApp(App) | ||
|
||
const pinia = createPinia() | ||
|
||
app.use(router).use(pinia) | ||
|
||
// I18n | ||
const settingsStore = useSettingsStore() | ||
i18n.global.locale = settingsStore.lang | ||
database.then(async (db) => { | ||
const { getSettingsInDB } = useSettingsService() | ||
const sub = await getSettingsInDB() | ||
const { settings } = useSettingsService() | ||
i18n.global.locale = settings.currentLang | ||
sub.unsubscribe() | ||
|
||
app.use(i18n).mount('#app') | ||
app.use(i18n).use(db).mount('#app') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.