2023-01-14 10:54:54 +00:00
|
|
|
import { SortingOrder } from '@hcengineering/core'
|
2022-09-21 08:08:25 +00:00
|
|
|
import { getCurrentLocation, locationToUrl } from '@hcengineering/ui'
|
2023-01-18 05:14:59 +00:00
|
|
|
import { DropdownViewOption, ToggleViewOption, Viewlet, ViewOptionModel, ViewOptions } from '@hcengineering/view'
|
2022-07-10 15:29:59 +00:00
|
|
|
|
2023-01-14 10:54:54 +00:00
|
|
|
export const noCategory = '#no_category'
|
2022-07-10 15:29:59 +00:00
|
|
|
|
2023-01-14 10:54:54 +00:00
|
|
|
export const defaulOptions: ViewOptions = {
|
|
|
|
groupBy: noCategory,
|
|
|
|
orderBy: ['modifiedBy', SortingOrder.Descending]
|
|
|
|
}
|
|
|
|
|
2022-07-10 15:29:59 +00:00
|
|
|
export function isToggleType (viewOption: ViewOptionModel): viewOption is ToggleViewOption {
|
|
|
|
return viewOption.type === 'toggle'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isDropdownType (viewOption: ViewOptionModel): viewOption is DropdownViewOption {
|
|
|
|
return viewOption.type === 'dropdown'
|
|
|
|
}
|
|
|
|
|
2023-01-21 14:16:14 +00:00
|
|
|
function makeViewOptionsKey (viewlet: Viewlet): string {
|
|
|
|
const prefix = viewlet?._id + (viewlet?.variant !== undefined ? `-${viewlet.variant}` : '')
|
2022-07-10 15:29:59 +00:00
|
|
|
const loc = getCurrentLocation()
|
|
|
|
loc.fragment = undefined
|
|
|
|
loc.query = undefined
|
|
|
|
return `viewOptions:${prefix}:${locationToUrl(loc)}`
|
|
|
|
}
|
|
|
|
|
2023-01-21 14:16:14 +00:00
|
|
|
function _setViewOptions (viewlet: Viewlet, options: ViewOptions): void {
|
|
|
|
const key = makeViewOptionsKey(viewlet)
|
2022-07-10 15:29:59 +00:00
|
|
|
localStorage.setItem(key, JSON.stringify(options))
|
|
|
|
}
|
|
|
|
|
2023-01-18 05:14:59 +00:00
|
|
|
export function setViewOptions (viewlet: Viewlet, options: ViewOptions): void {
|
2023-01-21 14:16:14 +00:00
|
|
|
_setViewOptions(viewlet, options)
|
2023-01-18 05:14:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 14:16:14 +00:00
|
|
|
function _getViewOptions (viewlet: Viewlet): ViewOptions | null {
|
|
|
|
const key = makeViewOptionsKey(viewlet)
|
2022-07-10 15:29:59 +00:00
|
|
|
const options = localStorage.getItem(key)
|
|
|
|
if (options === null) return null
|
|
|
|
return JSON.parse(options)
|
|
|
|
}
|
2023-01-18 05:14:59 +00:00
|
|
|
|
|
|
|
export function getViewOptions (viewlet: Viewlet | undefined, defaults = defaulOptions): ViewOptions {
|
|
|
|
if (viewlet === undefined) {
|
|
|
|
return { ...defaults }
|
|
|
|
}
|
2023-01-21 14:16:14 +00:00
|
|
|
return _getViewOptions(viewlet) ?? defaults
|
2023-01-18 05:14:59 +00:00
|
|
|
}
|