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:
@ -21,9 +21,7 @@ export const usePasswordItemStore = defineStore('passwordItemStore', () => {
|
||||
},
|
||||
})
|
||||
|
||||
const currentItem = computedAsync(
|
||||
async () => await readAsync(currentItemId.value),
|
||||
)
|
||||
const currentItem = computedAsync(() => readAsync(currentItemId.value))
|
||||
|
||||
return {
|
||||
currentItemId,
|
||||
@ -179,6 +177,8 @@ const readAsync = async (itemId: string | null) => {
|
||||
where: eq(haexPasswordsItemDetails.id, itemId),
|
||||
})
|
||||
|
||||
console.log('readAsync details', details)
|
||||
|
||||
if (!details) return null
|
||||
|
||||
const history = (await usePasswordHistoryStore().getAsync(itemId)) ?? []
|
||||
|
||||
98
src/stores/vault/device.ts
Normal file
98
src/stores/vault/device.ts
Normal file
@ -0,0 +1,98 @@
|
||||
import { load } from '@tauri-apps/plugin-store'
|
||||
import { hostname as tauriHostname } from '@tauri-apps/plugin-os'
|
||||
|
||||
export const useDeviceStore = defineStore('vaultInstanceStore', () => {
|
||||
const deviceId = ref<string>()
|
||||
|
||||
const hostname = computedAsync(() => tauriHostname())
|
||||
|
||||
const deviceName = ref<string>()
|
||||
|
||||
const getDeviceIdAsync = async () => {
|
||||
const store = await getStoreAsync()
|
||||
return store.get<string>('id')
|
||||
}
|
||||
|
||||
const getStoreAsync = async () => {
|
||||
const {
|
||||
public: { haexVault },
|
||||
} = useRuntimeConfig()
|
||||
|
||||
return await load(haexVault.instanceFileName || 'instance.json')
|
||||
}
|
||||
|
||||
const setDeviceIdAsync = async (id?: string) => {
|
||||
const store = await getStoreAsync()
|
||||
const _id = id || crypto.randomUUID()
|
||||
await store.set('id', _id)
|
||||
deviceId.value = _id
|
||||
return _id
|
||||
}
|
||||
|
||||
const setDeviceIdIfNotExistsAsync = async () => {
|
||||
const _deviceId = await getDeviceIdAsync()
|
||||
if (_deviceId) {
|
||||
deviceId.value = _deviceId
|
||||
return deviceId.value
|
||||
}
|
||||
return await setDeviceIdAsync()
|
||||
}
|
||||
|
||||
const isKnownDeviceAsync = async () => {
|
||||
const { readDeviceNameAsync } = useVaultSettingsStore()
|
||||
const deviceId = await getDeviceIdAsync()
|
||||
return deviceId ? (await readDeviceNameAsync(deviceId)) || false : false
|
||||
}
|
||||
|
||||
const readDeviceNameAsync = async (id: string) => {
|
||||
const { readDeviceNameAsync } = useVaultSettingsStore()
|
||||
deviceName.value = (await readDeviceNameAsync(id))?.value ?? ''
|
||||
return deviceName.value
|
||||
}
|
||||
|
||||
const updateDeviceNameAsync = async ({
|
||||
id,
|
||||
name,
|
||||
}: {
|
||||
id?: string
|
||||
name?: string
|
||||
}) => {
|
||||
const { updateDeviceNameAsync } = useVaultSettingsStore()
|
||||
const _id = id ?? deviceId.value
|
||||
if (!_id || !name) return
|
||||
|
||||
deviceName.value = name
|
||||
|
||||
return updateDeviceNameAsync({
|
||||
deviceId: _id,
|
||||
deviceName: name,
|
||||
})
|
||||
}
|
||||
|
||||
const addDeviceNameAsync = async ({
|
||||
id,
|
||||
name,
|
||||
}: {
|
||||
id?: string
|
||||
name: string
|
||||
}) => {
|
||||
const { addDeviceNameAsync } = useVaultSettingsStore()
|
||||
const _id = id ?? deviceId.value
|
||||
if (!_id || !name) throw new Error('Id oder Name fehlen')
|
||||
|
||||
return addDeviceNameAsync({
|
||||
deviceId: _id,
|
||||
deviceName: name,
|
||||
})
|
||||
}
|
||||
return {
|
||||
addDeviceNameAsync,
|
||||
hostname,
|
||||
deviceId,
|
||||
isKnownDeviceAsync,
|
||||
readDeviceNameAsync,
|
||||
setDeviceIdAsync,
|
||||
setDeviceIdIfNotExistsAsync,
|
||||
updateDeviceNameAsync,
|
||||
}
|
||||
})
|
||||
@ -1,47 +0,0 @@
|
||||
import { load } from '@tauri-apps/plugin-store'
|
||||
|
||||
export const useVaultInstanceStore = defineStore('vaultInstanceStore', () => {
|
||||
const instanceId = ref<string>()
|
||||
|
||||
const getInstanceIdAsync = async () => {
|
||||
const store = await getStoreAsync()
|
||||
instanceId.value = await store.get<string>('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,
|
||||
}
|
||||
})
|
||||
@ -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