182 lines
5.9 KiB
JavaScript
182 lines
5.9 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
|
|
// @ts-ignore
|
|
export const useTempStore = defineStore('temp', () => {
|
|
const auth = useAuthStore()
|
|
|
|
const searchStrings = ref({})
|
|
const filters = ref({})
|
|
const columns = ref({})
|
|
const pages = ref({})
|
|
const settings = ref({})
|
|
|
|
// Drafts stay local: temp_config is also returned in tenant profile lists.
|
|
const chatDrafts = ref({})
|
|
const sendingChatDrafts = ref({})
|
|
const loadedChatDrafts = new Set()
|
|
const draftStoragePrefix = 'fedeo:chat-draft:v1:'
|
|
const toast = useToast()
|
|
let draftStorageWarningShown = false
|
|
|
|
function getChatDraft(key) {
|
|
if (!loadedChatDrafts.has(key)) {
|
|
loadedChatDrafts.add(key)
|
|
try {
|
|
const saved = JSON.parse(sessionStorage.getItem(draftStoragePrefix + key) || 'null')
|
|
if (saved && typeof saved.text === 'string' && typeof saved.clientId === 'string'
|
|
&& saved.text.length <= 10000 && (!saved.image || (
|
|
typeof saved.image.name === 'string' && typeof saved.image.dataUrl === 'string'
|
|
&& saved.image.dataUrl.length <= 480000
|
|
&& /^data:image\/(jpeg|png|webp);base64,[A-Za-z0-9+/=]+$/.test(saved.image.dataUrl)
|
|
))) chatDrafts.value[key] = saved
|
|
} catch { /* Disabled storage or an invalid entry must not block the chat. */ }
|
|
}
|
|
return chatDrafts.value[key] || null
|
|
}
|
|
|
|
function setChatDraft(key, draft) {
|
|
loadedChatDrafts.add(key)
|
|
if (draft) chatDrafts.value[key] = draft
|
|
else delete chatDrafts.value[key]
|
|
try {
|
|
if (draft) sessionStorage.setItem(draftStoragePrefix + key, JSON.stringify(draft))
|
|
else sessionStorage.removeItem(draftStoragePrefix + key)
|
|
} catch {
|
|
// Do not restore an older version after a failed storage update.
|
|
try { sessionStorage.removeItem(draftStoragePrefix + key) } catch {}
|
|
if (!draftStorageWarningShown) {
|
|
draftStorageWarningShown = true
|
|
toast.add({ title: 'Entwurf nur vorübergehend gespeichert', description: 'Der Browserspeicher ist voll oder gesperrt. Bis zum Neuladen bleibt dein Entwurf im Chat erhalten.', color: 'warning' })
|
|
}
|
|
}
|
|
}
|
|
|
|
function isChatDraftSending(key) {
|
|
return !!sendingChatDrafts.value[key]
|
|
}
|
|
|
|
function startChatDraftSend(key, clientId) {
|
|
sendingChatDrafts.value[key] = clientId
|
|
}
|
|
|
|
function finishChatDraftSend(key, clientId) {
|
|
if (sendingChatDrafts.value[key] === clientId) delete sendingChatDrafts.value[key]
|
|
}
|
|
|
|
watch(() => auth.user?.id || auth.user?.user_id, (user, previous) => {
|
|
if (!previous || user === previous) return
|
|
chatDrafts.value = {}
|
|
sendingChatDrafts.value = {}
|
|
loadedChatDrafts.clear()
|
|
try {
|
|
for (const key of Object.keys(sessionStorage)) {
|
|
if (key.startsWith(draftStoragePrefix)) sessionStorage.removeItem(key)
|
|
}
|
|
} catch { /* Storage may be unavailable. In-memory drafts are still cleared. */ }
|
|
}, { flush: 'sync' })
|
|
|
|
const storeTempConfig = async () => {
|
|
const config = {
|
|
searchStrings: searchStrings.value,
|
|
columns: columns.value,
|
|
pages: pages.value,
|
|
settings: settings.value,
|
|
filters: filters.value
|
|
}
|
|
|
|
await useNuxtApp().$api(`/api/profiles/${auth.profile.id}`, {
|
|
method: 'PUT',
|
|
body: { temp_config: config }
|
|
})
|
|
}
|
|
|
|
function setStoredTempConfig(config) {
|
|
searchStrings.value = config.searchStrings || {}
|
|
columns.value = config.columns || {}
|
|
pages.value = config.pages || {}
|
|
settings.value = config.settings || {}
|
|
filters.value = config.filters || {}
|
|
}
|
|
|
|
function modifySearchString(type, input) {
|
|
searchStrings.value[type] = input
|
|
storeTempConfig()
|
|
}
|
|
|
|
function clearSearchString(type) {
|
|
searchStrings.value[type] = ""
|
|
storeTempConfig()
|
|
}
|
|
|
|
function modifyFilter(domain, type, input) {
|
|
if (!filters.value[domain]) filters.value[domain] = {}
|
|
filters.value[domain][type] = input
|
|
storeTempConfig()
|
|
}
|
|
|
|
function setFilters(domain, nextFilters) {
|
|
filters.value[domain] = nextFilters
|
|
if (!filters.value[domain] || Object.keys(filters.value[domain]).length === 0) {
|
|
delete filters.value[domain]
|
|
}
|
|
storeTempConfig()
|
|
}
|
|
|
|
function clearFilter(domain, type) {
|
|
if (!filters.value[domain]) return
|
|
delete filters.value[domain][type]
|
|
if (Object.keys(filters.value[domain]).length === 0) {
|
|
delete filters.value[domain]
|
|
}
|
|
storeTempConfig()
|
|
}
|
|
|
|
function modifyColumns(type, input) {
|
|
columns.value[type] = input
|
|
storeTempConfig()
|
|
}
|
|
|
|
function modifyPages(type, input) {
|
|
pages.value[type] = input
|
|
storeTempConfig()
|
|
}
|
|
|
|
function modifySettings(type, input) {
|
|
settings.value[type] = input
|
|
storeTempConfig()
|
|
}
|
|
|
|
// Spezifisch für das Banking-Datum
|
|
function modifyBankingPeriod(periodKey, range) {
|
|
if (!settings.value['banking']) settings.value['banking'] = {}
|
|
|
|
settings.value['banking'].periodKey = periodKey
|
|
settings.value['banking'].range = range
|
|
|
|
storeTempConfig()
|
|
}
|
|
|
|
return {
|
|
getChatDraft,
|
|
setChatDraft,
|
|
isChatDraftSending,
|
|
startChatDraftSend,
|
|
finishChatDraftSend,
|
|
setStoredTempConfig,
|
|
searchStrings,
|
|
modifySearchString,
|
|
clearSearchString,
|
|
filters,
|
|
modifyFilter,
|
|
setFilters,
|
|
clearFilter,
|
|
columns,
|
|
modifyColumns,
|
|
modifyPages,
|
|
pages,
|
|
modifySettings,
|
|
modifyBankingPeriod, // Neue Funktion exportiert
|
|
settings
|
|
}
|
|
})
|