diff --git a/nuxt.config.ts b/nuxt.config.ts index 37adb67..93dcf73 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -84,7 +84,8 @@ export default defineNuxtConfig({ public: { haexVault: { lastVaultFileName: 'lastVaults.json', - //defaultDatabase: 'src/database/default.db', + instanceFileName: 'instance.json', + defaultVaultName: 'HaexHub', }, }, }, diff --git a/src/components/vault/button/open.vue b/src/components/vault/button/open.vue index 6128578..b7b9c9e 100644 --- a/src/components/vault/button/open.vue +++ b/src/components/vault/button/open.vue @@ -106,7 +106,9 @@ const onLoadDatabase = async () => { const localePath = useLocalePath() -const { syncLocaleAsync, syncThemeAsync, syncVaultNameAsync } = useVaultStore() +const { syncLocaleAsync, syncThemeAsync, syncVaultNameAsync } = + useVaultSettingsStore() + const onOpenDatabase = async () => { try { check.value = true diff --git a/src/pages/vault.vue b/src/pages/vault.vue index dc536d5..ce384d2 100644 --- a/src/pages/vault.vue +++ b/src/pages/vault.vue @@ -3,6 +3,13 @@ + + +
+ Das scheint das erste Mal zu sein, dass du auf diesem Gerät diese Vault + öffnest. Bitte gib diesem Gerät einen Namen +
+
@@ -10,4 +17,21 @@ definePageMeta({ middleware: 'database', }) + +const showInstanceDialog = ref(false) + +const { readNotificationsAsync } = useNotificationStore() +const { isFirstTimeAsync } = useVaultInstanceStore() +const { loadExtensionsAsync } = useExtensionsStore() + +onMounted(async () => { + await loadExtensionsAsync() + await readNotificationsAsync() + + if (await isFirstTimeAsync()) { + showInstanceDialog.value = true + } +}) + +onMounted(() => {}) diff --git a/src/pages/vault/[vaultId]/index.vue b/src/pages/vault/[vaultId]/index.vue index b4b9f23..e0dada4 100644 --- a/src/pages/vault/[vaultId]/index.vue +++ b/src/pages/vault/[vaultId]/index.vue @@ -1,10 +1,12 @@ @@ -14,10 +16,4 @@ definePageMeta({ }) const extensionStore = useExtensionsStore() -const { readNotificationsAsync } = useNotificationStore() - -onMounted(async () => { - await extensionStore.loadExtensionsAsync() - await readNotificationsAsync() -}) diff --git a/src/pages/vault/[vaultId]/settings.vue b/src/pages/vault/[vaultId]/settings.vue index 999a6ea..afc55f7 100644 --- a/src/pages/vault/[vaultId]/settings.vue +++ b/src/pages/vault/[vaultId]/settings.vue @@ -51,7 +51,7 @@ definePageMeta({ const { t, setLocale } = useI18n() const { currentVault, currentVaultName } = storeToRefs(useVaultStore()) -const { updateVaultNameAsync } = useVaultStore() +const { updateVaultNameAsync } = useVaultSettingsStore() const onSelectLocaleAsync = async (locale: Locale) => { await currentVault.value?.drizzle diff --git a/src/stores/vault/index.ts b/src/stores/vault/index.ts index e1a1920..7a8abe2 100644 --- a/src/stores/vault/index.ts +++ b/src/stores/vault/index.ts @@ -1,10 +1,8 @@ -import * as schema from '@/../src-tauri/database/schemas/vault' -import { invoke } from '@tauri-apps/api/core' -import { exists } from '@tauri-apps/plugin-fs' -import { platform } from '@tauri-apps/plugin-os' -import { eq } from 'drizzle-orm' -import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy' import { drizzle } from 'drizzle-orm/sqlite-proxy' +import { invoke } from '@tauri-apps/api/core' +import { platform } from '@tauri-apps/plugin-os' +import * as schema from '@/../src-tauri/database/schemas/vault' +import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy' interface IVault { name: string @@ -15,6 +13,10 @@ interface IOpenVaults { } export const useVaultStore = defineStore('vaultStore', () => { + const { + public: { haexVault }, + } = useRuntimeConfig() + const currentVaultId = computed({ get: () => getSingleRouteParam(useRouter().currentRoute.value.params.vaultId), @@ -23,30 +25,7 @@ export const useVaultStore = defineStore('vaultStore', () => { }, }) - const defaultVaultName = ref('HaexHub') - const currentVaultName = ref(defaultVaultName.value) - - const read_only = computed({ - get: () => { - console.log( - 'query showSidebar', - useRouter().currentRoute.value.query.readonly, - ) - return JSON.parse( - getSingleRouteParam(useRouter().currentRoute.value.query.readonly) || - 'false', - ) - }, - set: (readonly) => { - const router = useRouter() - router.replace({ - query: { - ...router.currentRoute.value.query, - readonly: JSON.stringify(readonly ? true : false), - }, - }) - }, - }) + const currentVaultName = ref(haexVault.defaultVaultName || 'HaexHub') const openVaults = ref({}) @@ -92,7 +71,6 @@ export const useVaultStore = defineStore('vaultStore', () => { console.error('SQL select Error:', e, sql, params) return [] }) - console.log('select', rows) } else { console.log('sql_execute', sql, params) // Otherwise, use the execute method @@ -125,13 +103,6 @@ export const useVaultStore = defineStore('vaultStore', () => { } } - const refreshDatabaseAsync = async () => { - console.log('refreshDatabaseAsync') - /* if (!currentVault.value?.database.close) { - return navigateTo(useLocaleRoute()({ name: 'vaultOpen' })); - } */ - } - const createAsync = async ({ path, password, @@ -139,109 +110,19 @@ export const useVaultStore = defineStore('vaultStore', () => { path: string password: string }) => { - /* const existDb = await exists('default.db', { - baseDir: BaseDirectory.Resource, - }); */ - - /* const existDb = await resolveResource('resources/default.db'); - if (!existDb) throw new Error('Keine Datenbank da'); - await copyFile(existDb, path); */ - const result = await invoke('create_encrypted_database', { + await invoke('create_encrypted_database', { path, key: password, }) - console.log('create_encrypted_database', result) return await openAsync({ path, password }) } const closeAsync = async () => { if (!currentVaultId.value) return - /* if ( - typeof openVaults.value?.[currentVaultId.value]?.database?.close === - 'function' - ) { - console.log('close db', openVaults.value?.[currentVaultId.value]); - return openVaults.value?.[currentVaultId.value]?.database?.close(); - } */ delete openVaults.value?.[currentVaultId.value] } - const syncLocaleAsync = async () => { - try { - const app = useNuxtApp() - - const currentLocaleRow = await currentVault.value?.drizzle - .select() - .from(schema.haexSettings) - .where(eq(schema.haexSettings.key, 'locale')) - - if (currentLocaleRow?.[0]?.value) { - const currentLocale = app.$i18n.availableLocales.find( - (locale) => locale === currentLocaleRow[0].value, - ) - await app.$i18n.setLocale(currentLocale ?? app.$i18n.defaultLocale) - } else { - await currentVault.value?.drizzle.insert(schema.haexSettings).values({ - id: crypto.randomUUID(), - key: 'locale', - value: app.$i18n.locale.value, - }) - } - } catch (error) { - console.log('ERROR syncLocaleAsync', error) - } - } - - const syncThemeAsync = async () => { - const { availableThemes, defaultTheme, currentTheme } = storeToRefs( - useUiStore(), - ) - const currentThemeRow = await currentVault.value?.drizzle - .select() - .from(schema.haexSettings) - .where(eq(schema.haexSettings.key, 'theme')) - - if (currentThemeRow?.[0]?.value) { - const theme = availableThemes.value.find( - (theme) => theme.name === currentThemeRow[0].value, - ) - currentTheme.value = theme ?? defaultTheme.value - } else { - await currentVault.value?.drizzle.insert(schema.haexSettings).values({ - id: crypto.randomUUID(), - key: 'theme', - value: currentTheme.value.value, - }) - } - } - - const syncVaultNameAsync = async () => { - const currentVaultNameRow = await currentVault.value?.drizzle - .select() - .from(schema.haexSettings) - .where(eq(schema.haexSettings.key, 'vaultName')) - - if (currentVaultNameRow?.[0]?.value) { - currentVaultName.value = - currentVaultNameRow.at(0)?.value ?? defaultVaultName.value - } else { - await currentVault.value?.drizzle.insert(schema.haexSettings).values({ - id: crypto.randomUUID(), - key: 'vaultName', - value: currentVaultName.value, - }) - } - } - - const updateVaultNameAsync = async (newVaultName?: string | null) => { - console.log('set new vaultName', newVaultName) - return currentVault.value?.drizzle - .update(schema.haexSettings) - .set({ value: newVaultName ?? defaultVaultName.value }) - .where(eq(schema.haexSettings.key, 'vaultName')) - } - return { closeAsync, createAsync, @@ -250,12 +131,6 @@ export const useVaultStore = defineStore('vaultStore', () => { currentVaultName, openAsync, openVaults, - read_only, - refreshDatabaseAsync, - syncLocaleAsync, - syncThemeAsync, - syncVaultNameAsync, - updateVaultNameAsync, } }) @@ -264,9 +139,8 @@ const getVaultIdAsync = async (path: string) => { const data = encoder.encode(path) const hashBuffer = await crypto.subtle.digest('SHA-256', data) - const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array - const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') // convert bytes to hex string - console.log('vaultId', hashHex) + const hashArray = Array.from(new Uint8Array(hashBuffer)) + const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') return hashHex } diff --git a/src/stores/vault/instance.ts b/src/stores/vault/instance.ts new file mode 100644 index 0000000..1001667 --- /dev/null +++ b/src/stores/vault/instance.ts @@ -0,0 +1,47 @@ +import { load } from '@tauri-apps/plugin-store' + +export const useVaultInstanceStore = defineStore('vaultInstanceStore', () => { + const instanceId = ref() + + const getInstanceIdAsync = async () => { + const store = await getStoreAsync() + instanceId.value = await store.get('id') + + return instanceId.value + } + + const getStoreAsync = async () => { + const { + public: { haexVault }, + } = useRuntimeConfig() + + return await load(haexVault.instanceFileName || 'instance.json') + } + + const setInstanceIdAsync = async (id?: string) => { + const store = await getStoreAsync() + const _id = id || crypto.randomUUID() + await store.set('id', _id) + + return _id + } + + const setInstanceIdIfNotExistsAsync = async () => { + const id = await getInstanceIdAsync() + return id ?? (await setInstanceIdAsync()) + } + + const isFirstTimeAsync = async () => { + const { currentVault } = useVaultStore() + + currentVault.drizzle.select + return !(await getInstanceIdAsync()) + } + + return { + instanceId, + isFirstTimeAsync, + setInstanceIdAsync, + setInstanceIdIfNotExistsAsync, + } +}) diff --git a/src/stores/vault/lastVaults.ts b/src/stores/vault/lastVaults.ts index 4f6ffd7..5a13854 100644 --- a/src/stores/vault/lastVaults.ts +++ b/src/stores/vault/lastVaults.ts @@ -16,7 +16,7 @@ export const useLastVaultStore = defineStore('lastVaultStore', () => { const keyName = 'lastVaults' const getStoreAsync = async () => { - return await load(haexVault.lastVaultFileName) + return await load(haexVault.lastVaultFileName || 'lastVaults.json') } const syncLastVaultsAsync = async () => { @@ -45,7 +45,6 @@ export const useLastVaultStore = defineStore('lastVaultStore', () => { } const removeVaultAsync = async (vaultPath: string) => { - console.log('remove', vaultPath, lastVaults.value) lastVaults.value = lastVaults.value.filter( (vault) => vault.path !== vaultPath, ) @@ -54,14 +53,11 @@ export const useLastVaultStore = defineStore('lastVaultStore', () => { const saveLastVaultsAsync = async () => { const store = await getStoreAsync() - console.log('save lastVaults', keyName, lastVaults.value) await store.set(keyName, lastVaults.value) await syncLastVaultsAsync() } - const test = async () => console.log('test') return { - test, addVaultAsync, syncLastVaultsAsync, lastVaults, diff --git a/src/stores/vault/settings.ts b/src/stores/vault/settings.ts new file mode 100644 index 0000000..a995d78 --- /dev/null +++ b/src/stores/vault/settings.ts @@ -0,0 +1,93 @@ +import * as schema from '@/../src-tauri/database/schemas/vault' +import { eq } from 'drizzle-orm' + +export const useVaultSettingsStore = defineStore('vaultSettingsStore', () => { + const { currentVault, currentVaultName } = storeToRefs(useVaultStore()) + + const { + public: { haexVault }, + } = useRuntimeConfig() + + const syncLocaleAsync = async () => { + try { + const app = useNuxtApp() + + const currentLocaleRow = await currentVault.value?.drizzle + .select() + .from(schema.haexSettings) + .where(eq(schema.haexSettings.key, 'locale')) + + if (currentLocaleRow?.[0]?.value) { + const currentLocale = app.$i18n.availableLocales.find( + (locale) => locale === currentLocaleRow[0].value, + ) + await app.$i18n.setLocale(currentLocale ?? app.$i18n.defaultLocale) + } else { + await currentVault.value?.drizzle.insert(schema.haexSettings).values({ + id: crypto.randomUUID(), + key: 'locale', + value: app.$i18n.locale.value, + }) + } + } catch (error) { + console.log('ERROR syncLocaleAsync', error) + } + } + + const syncThemeAsync = async () => { + const { availableThemes, defaultTheme, currentTheme } = storeToRefs( + useUiStore(), + ) + const currentThemeRow = await currentVault.value?.drizzle + .select() + .from(schema.haexSettings) + .where(eq(schema.haexSettings.key, 'theme')) + + if (currentThemeRow?.[0]?.value) { + const theme = availableThemes.value.find( + (theme) => theme.name === currentThemeRow[0].value, + ) + currentTheme.value = theme ?? defaultTheme.value + } else { + await currentVault.value?.drizzle.insert(schema.haexSettings).values({ + id: crypto.randomUUID(), + key: 'theme', + value: currentTheme.value.value, + }) + } + } + + const syncVaultNameAsync = async () => { + const currentVaultNameRow = await currentVault.value?.drizzle + .select() + .from(schema.haexSettings) + .where(eq(schema.haexSettings.key, 'vaultName')) + + if (currentVaultNameRow?.[0]?.value) { + currentVaultName.value = + currentVaultNameRow.at(0)?.value || + haexVault.defaultVaultName || + 'HaexHub' + } else { + await currentVault.value?.drizzle.insert(schema.haexSettings).values({ + id: crypto.randomUUID(), + key: 'vaultName', + value: currentVaultName.value, + }) + } + } + + const updateVaultNameAsync = async (newVaultName?: string | null) => { + return currentVault.value?.drizzle + .update(schema.haexSettings) + .set({ value: newVaultName || haexVault.defaultVaultName || 'HaexHub' }) + .where(eq(schema.haexSettings.key, 'vaultName')) + } + + return { + syncLocaleAsync, + syncThemeAsync, + syncVaultNameAsync, + updateVaultNameAsync, + } +})