31 lines
680 B
TypeScript
31 lines
680 B
TypeScript
import type { ChadClient } from '#shared/types'
|
|
import { createGlobalState, useLocalStorage } from '@vueuse/core'
|
|
|
|
export const useGlobalState = createGlobalState(() => {
|
|
const username = useLocalStorage<string>('username', '')
|
|
|
|
const clients = ref<ChadClient[]>([])
|
|
|
|
const me = computed(() => clients.value.find(client => client.isMe))
|
|
|
|
const clientByIdMap = computed(() => {
|
|
return clients.value.reduce<Record<ChadClient['id'], ChadClient>>((result, client) => {
|
|
result[client.id] = client
|
|
|
|
return result
|
|
}, {})
|
|
})
|
|
|
|
function reset() {
|
|
clients.value = []
|
|
}
|
|
|
|
return {
|
|
username,
|
|
me,
|
|
clients,
|
|
clientByIdMap,
|
|
reset,
|
|
}
|
|
})
|