2023-02-15 04:38:48 +00:00
|
|
|
import { getCurrentAccount } from '@hcengineering/core'
|
2022-12-08 01:17:53 +00:00
|
|
|
import { fetchMetadataLocalStorage, setMetadataLocalStorage } from '@hcengineering/ui'
|
2023-02-15 04:38:48 +00:00
|
|
|
import { get, writable } from 'svelte/store'
|
2022-12-08 01:17:53 +00:00
|
|
|
import presentation from './plugin'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line
|
|
|
|
export const draftStore = writable<Record<string, any>>(fetchMetadataLocalStorage(presentation.metadata.Draft) || {})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function updateDraftStore (id: string, draft: any): void {
|
|
|
|
draftStore.update((drafts) => {
|
2023-02-15 04:38:48 +00:00
|
|
|
if (draft !== undefined) {
|
2022-12-20 04:25:45 +00:00
|
|
|
drafts[id] = draft
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
|
|
delete drafts[id]
|
|
|
|
}
|
2022-12-08 01:17:53 +00:00
|
|
|
setMetadataLocalStorage(presentation.metadata.Draft, drafts)
|
|
|
|
return drafts
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function updateUserDraft (id: string, draft: any): void {
|
2023-02-15 04:38:48 +00:00
|
|
|
const me = getCurrentAccount()._id
|
2022-12-08 01:17:53 +00:00
|
|
|
draftStore.update((drafts) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
2023-02-15 04:38:48 +00:00
|
|
|
const userDrafts: Record<string, any> = drafts[me] || {}
|
2022-12-08 01:17:53 +00:00
|
|
|
userDrafts[id] = draft
|
2023-02-15 04:38:48 +00:00
|
|
|
drafts[me] = userDrafts
|
2022-12-08 01:17:53 +00:00
|
|
|
setMetadataLocalStorage(presentation.metadata.Draft, drafts)
|
|
|
|
return drafts
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function getUserDraft (id: string): any {
|
2023-02-15 04:38:48 +00:00
|
|
|
const me = getCurrentAccount()._id
|
2022-12-08 01:17:53 +00:00
|
|
|
const drafts: Record<string, any> = get(draftStore)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
2023-02-15 04:38:48 +00:00
|
|
|
const userDrafts: Record<string, any> = drafts[me] || {}
|
2022-12-08 01:17:53 +00:00
|
|
|
const draft: Record<string, any> = userDrafts[id]
|
|
|
|
return draft
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isUserDraftExists (id: string): boolean {
|
2023-02-15 04:38:48 +00:00
|
|
|
const me = getCurrentAccount()._id
|
2022-12-08 01:17:53 +00:00
|
|
|
const drafts: Record<string, any> = get(draftStore)
|
2023-02-15 04:38:48 +00:00
|
|
|
const userDrafts: Record<string, any> = drafts[me]
|
2022-12-08 01:17:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
|
|
if (!userDrafts) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const draftRecord: Record<string, any> = userDrafts[id]
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
|
|
if (!draftRecord) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|