2022-11-13 21:04:57 +00:00
|
|
|
import type { IProject } from '@/modelTypes/IProject'
|
2022-10-17 11:14:07 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
type ProjectView = Record<IProject['id'], string>
|
2022-10-17 11:14:07 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
const DEFAULT_PROJECT_VIEW = 'project.list' as const
|
|
|
|
const PROJECT_VIEW_SETTINGS_KEY = 'projectView'
|
2022-10-17 11:14:07 +00:00
|
|
|
|
2022-09-27 20:54:37 +00:00
|
|
|
/**
|
2022-11-13 21:04:57 +00:00
|
|
|
* Save the current project view to local storage
|
2022-09-27 20:54:37 +00:00
|
|
|
*/
|
2022-11-13 21:04:57 +00:00
|
|
|
export function saveProjectView(projectId: IProject['id'], routeName: string) {
|
2021-08-06 21:34:37 +00:00
|
|
|
if (routeName.includes('settings.')) {
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 20:54:37 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
if (!projectId) {
|
2021-01-30 16:17:04 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-27 20:54:37 +00:00
|
|
|
|
|
|
|
// We use local storage and not the store here to make it persistent across reloads.
|
2022-11-13 21:04:57 +00:00
|
|
|
const savedProjectView = localStorage.getItem(PROJECT_VIEW_SETTINGS_KEY)
|
|
|
|
let savedProjectViewJson: ProjectView | false = false
|
|
|
|
if (savedProjectView !== null) {
|
|
|
|
savedProjectViewJson = JSON.parse(savedProjectView) as ProjectView
|
2020-05-29 14:33:57 +00:00
|
|
|
}
|
2020-05-22 15:28:26 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
let projectView: ProjectView = {}
|
|
|
|
if (savedProjectViewJson) {
|
|
|
|
projectView = savedProjectViewJson
|
2020-05-22 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
projectView[projectId] = routeName
|
|
|
|
localStorage.setItem(PROJECT_VIEW_SETTINGS_KEY, JSON.stringify(projectView))
|
2020-05-22 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
export const getProjectView = (projectId: IProject['id']) => {
|
|
|
|
// Migrate old setting over
|
|
|
|
// TODO: remove when 1.0 release
|
|
|
|
const oldListViewSettings = localStorage.getItem('listView')
|
|
|
|
if (oldListViewSettings !== null) {
|
|
|
|
localStorage.setItem(PROJECT_VIEW_SETTINGS_KEY, oldListViewSettings)
|
2020-05-22 15:28:26 +00:00
|
|
|
localStorage.removeItem('listView')
|
|
|
|
}
|
2022-11-13 21:04:57 +00:00
|
|
|
|
|
|
|
// Remove old stored settings
|
|
|
|
// TODO: remove when 1.0 release
|
|
|
|
const savedProjectView = localStorage.getItem(PROJECT_VIEW_SETTINGS_KEY)
|
|
|
|
if (savedProjectView !== null && savedProjectView.startsWith('project.')) {
|
|
|
|
localStorage.removeItem(PROJECT_VIEW_SETTINGS_KEY)
|
|
|
|
}
|
2020-05-22 15:28:26 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
if (!savedProjectView) {
|
|
|
|
return DEFAULT_PROJECT_VIEW
|
2020-05-22 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
const savedProjectViewJson: ProjectView = JSON.parse(savedProjectView)
|
2020-05-22 15:28:26 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
if (!savedProjectViewJson[projectId]) {
|
|
|
|
return DEFAULT_PROJECT_VIEW
|
2020-05-22 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
return savedProjectViewJson[projectId]
|
2020-05-22 15:28:26 +00:00
|
|
|
}
|