implemented device name

This commit is contained in:
2025-06-17 16:46:44 +02:00
parent f765d5bdf0
commit e33fa804fa
24 changed files with 1004 additions and 190 deletions

View 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,
}
})