platform/plugins/view-resources/src/viewOptions.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-01-14 10:54:54 +00:00
import { SortingOrder } from '@hcengineering/core'
import { getCurrentLocation, locationToUrl } from '@hcengineering/ui'
2023-01-14 10:54:54 +00:00
import { DropdownViewOption, ToggleViewOption, ViewOptionModel, ViewOptions } from '@hcengineering/view'
import { writable } from 'svelte/store'
2023-01-14 10:54:54 +00:00
export const noCategory = '#no_category'
2023-01-14 10:54:54 +00:00
export const defaulOptions: ViewOptions = {
groupBy: noCategory,
orderBy: ['modifiedBy', SortingOrder.Descending]
}
export const viewOptionsStore = writable<ViewOptions>(defaulOptions)
export function isToggleType (viewOption: ViewOptionModel): viewOption is ToggleViewOption {
return viewOption.type === 'toggle'
}
export function isDropdownType (viewOption: ViewOptionModel): viewOption is DropdownViewOption {
return viewOption.type === 'dropdown'
}
function makeViewOptionsKey (prefix: string): string {
const loc = getCurrentLocation()
loc.fragment = undefined
loc.query = undefined
return `viewOptions:${prefix}:${locationToUrl(loc)}`
}
export function setViewOptions (prefix: string, options: ViewOptions): void {
const key = makeViewOptionsKey(prefix)
localStorage.setItem(key, JSON.stringify(options))
}
export function getViewOptions (prefix: string): ViewOptions | null {
const key = makeViewOptionsKey(prefix)
const options = localStorage.getItem(key)
if (options === null) return null
return JSON.parse(options)
}