mirror of
https://github.com/haexhub/haex-hub.git
synced 2025-12-17 06:30:50 +01:00
implemented device name
This commit is contained in:
@ -1,5 +1,20 @@
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { z } from 'zod'
|
||||
import * as schema from '@/../src-tauri/database/schemas/vault'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import type { Locale } from 'vue-i18n'
|
||||
|
||||
export enum VaultSettingsTypeEnum {
|
||||
deviceName = 'deviceName',
|
||||
settings = 'settings',
|
||||
}
|
||||
|
||||
export enum VaultSettingsKeyEnum {
|
||||
locale = 'locale',
|
||||
theme = 'theme',
|
||||
vaultName = 'vaultName',
|
||||
}
|
||||
|
||||
export const vaultDeviceNameSchema = z.string().min(3).max(255)
|
||||
|
||||
export const useVaultSettingsStore = defineStore('vaultSettingsStore', () => {
|
||||
const { currentVault, currentVaultName } = storeToRefs(useVaultStore())
|
||||
@ -12,20 +27,21 @@ export const useVaultSettingsStore = defineStore('vaultSettingsStore', () => {
|
||||
try {
|
||||
const app = useNuxtApp()
|
||||
|
||||
const currentLocaleRow = await currentVault.value?.drizzle
|
||||
.select()
|
||||
.from(schema.haexSettings)
|
||||
.where(eq(schema.haexSettings.key, 'locale'))
|
||||
const currentLocaleRow =
|
||||
await currentVault.value?.drizzle.query.haexSettings.findFirst({
|
||||
where: eq(schema.haexSettings.key, VaultSettingsKeyEnum.locale),
|
||||
})
|
||||
|
||||
if (currentLocaleRow?.[0]?.value) {
|
||||
if (currentLocaleRow?.value) {
|
||||
const currentLocale = app.$i18n.availableLocales.find(
|
||||
(locale) => locale === currentLocaleRow[0].value,
|
||||
(locale) => locale === currentLocaleRow.value,
|
||||
)
|
||||
await app.$i18n.setLocale(currentLocale ?? app.$i18n.defaultLocale)
|
||||
} else {
|
||||
await currentVault.value?.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
key: 'locale',
|
||||
key: VaultSettingsKeyEnum.locale,
|
||||
type: VaultSettingsTypeEnum.settings,
|
||||
value: app.$i18n.locale.value,
|
||||
})
|
||||
}
|
||||
@ -34,44 +50,62 @@ export const useVaultSettingsStore = defineStore('vaultSettingsStore', () => {
|
||||
}
|
||||
}
|
||||
|
||||
const updateLocaleAsync = async (locale: Locale) => {
|
||||
await currentVault.value?.drizzle
|
||||
.update(schema.haexSettings)
|
||||
.set({ key: VaultSettingsKeyEnum.locale, value: locale })
|
||||
.where(
|
||||
and(
|
||||
eq(schema.haexSettings.key, VaultSettingsKeyEnum.locale),
|
||||
eq(schema.haexSettings.type, VaultSettingsTypeEnum.settings),
|
||||
),
|
||||
)
|
||||
}
|
||||
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'))
|
||||
const currentThemeRow =
|
||||
await currentVault.value?.drizzle.query.haexSettings.findFirst({
|
||||
where: eq(schema.haexSettings.key, VaultSettingsKeyEnum.theme),
|
||||
})
|
||||
|
||||
if (currentThemeRow?.[0]?.value) {
|
||||
if (currentThemeRow?.value) {
|
||||
const theme = availableThemes.value.find(
|
||||
(theme) => theme.name === currentThemeRow[0].value,
|
||||
(theme) => theme.value === currentThemeRow.value,
|
||||
)
|
||||
currentTheme.value = theme ?? defaultTheme.value
|
||||
} else {
|
||||
await currentVault.value?.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
key: 'theme',
|
||||
key: VaultSettingsKeyEnum.theme,
|
||||
type: VaultSettingsTypeEnum.settings,
|
||||
value: currentTheme.value.value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const syncVaultNameAsync = async () => {
|
||||
const currentVaultNameRow = await currentVault.value?.drizzle
|
||||
.select()
|
||||
.from(schema.haexSettings)
|
||||
.where(eq(schema.haexSettings.key, 'vaultName'))
|
||||
const updateThemeAsync = async (theme: string) => {
|
||||
return await currentVault.value?.drizzle
|
||||
.update(schema.haexSettings)
|
||||
.set({ key: VaultSettingsKeyEnum.theme, value: theme })
|
||||
.where(eq(schema.haexSettings.key, VaultSettingsKeyEnum.theme))
|
||||
}
|
||||
|
||||
if (currentVaultNameRow?.[0]?.value) {
|
||||
const syncVaultNameAsync = async () => {
|
||||
const currentVaultNameRow =
|
||||
await currentVault.value?.drizzle.query.haexSettings.findFirst({
|
||||
where: eq(schema.haexSettings.key, VaultSettingsKeyEnum.vaultName),
|
||||
})
|
||||
|
||||
if (currentVaultNameRow?.value) {
|
||||
currentVaultName.value =
|
||||
currentVaultNameRow.at(0)?.value ||
|
||||
haexVault.defaultVaultName ||
|
||||
'HaexHub'
|
||||
currentVaultNameRow.value || haexVault.defaultVaultName || 'HaexHub'
|
||||
} else {
|
||||
await currentVault.value?.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
key: 'vaultName',
|
||||
key: VaultSettingsKeyEnum.vaultName,
|
||||
type: VaultSettingsTypeEnum.settings,
|
||||
value: currentVaultName.value,
|
||||
})
|
||||
}
|
||||
@ -84,10 +118,76 @@ export const useVaultSettingsStore = defineStore('vaultSettingsStore', () => {
|
||||
.where(eq(schema.haexSettings.key, 'vaultName'))
|
||||
}
|
||||
|
||||
const readDeviceNameAsync = async (id: string) => {
|
||||
const { currentVault } = useVaultStore()
|
||||
|
||||
const deviceName = await currentVault.drizzle.query.haexSettings.findFirst({
|
||||
where: and(
|
||||
eq(schema.haexSettings.type, VaultSettingsTypeEnum.deviceName),
|
||||
eq(schema.haexSettings.key, id),
|
||||
),
|
||||
})
|
||||
console.log('readDeviceNameAsync', deviceName)
|
||||
return deviceName
|
||||
}
|
||||
|
||||
const addDeviceNameAsync = async ({
|
||||
deviceId,
|
||||
deviceName,
|
||||
}: {
|
||||
deviceId: string
|
||||
deviceName: string
|
||||
}) => {
|
||||
const { currentVault } = useVaultStore()
|
||||
|
||||
const isNameOk = vaultDeviceNameSchema.safeParse(deviceName)
|
||||
if (!isNameOk.success) {
|
||||
console.log('deviceName not OK', isNameOk.error)
|
||||
return
|
||||
}
|
||||
|
||||
return currentVault.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
type: VaultSettingsTypeEnum.deviceName,
|
||||
key: deviceId,
|
||||
value: deviceName,
|
||||
})
|
||||
}
|
||||
|
||||
const updateDeviceNameAsync = async ({
|
||||
deviceId,
|
||||
deviceName,
|
||||
}: {
|
||||
deviceId: string
|
||||
deviceName: string
|
||||
}) => {
|
||||
const { currentVault } = useVaultStore()
|
||||
|
||||
const isNameOk = vaultDeviceNameSchema.safeParse(deviceName)
|
||||
if (!isNameOk.success) return
|
||||
|
||||
return currentVault.drizzle
|
||||
.update(schema.haexSettings)
|
||||
.set({
|
||||
value: deviceName,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(schema.haexSettings.key, deviceId),
|
||||
eq(schema.haexSettings.type, VaultSettingsTypeEnum.deviceName),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
addDeviceNameAsync,
|
||||
readDeviceNameAsync,
|
||||
syncLocaleAsync,
|
||||
syncThemeAsync,
|
||||
syncVaultNameAsync,
|
||||
updateDeviceNameAsync,
|
||||
updateLocaleAsync,
|
||||
updateThemeAsync,
|
||||
updateVaultNameAsync,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user