From de6a9f807aaa88566317172b7cee51218571866a Mon Sep 17 00:00:00 2001 From: NIL2000 Date: Tue, 17 Sep 2024 20:28:53 +0530 Subject: [PATCH] Solved linting error --- apps/platform/src/lib/workspace-storage.ts | 50 ++++++++++++++++------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/apps/platform/src/lib/workspace-storage.ts b/apps/platform/src/lib/workspace-storage.ts index 60bd8182..79a31244 100644 --- a/apps/platform/src/lib/workspace-storage.ts +++ b/apps/platform/src/lib/workspace-storage.ts @@ -1,36 +1,60 @@ -import type { Workspace } from '@/types' import { atom, useAtom } from 'jotai' +import type { Workspace } from '@/types' const defaultWorkspaceAtom = atom(null) const currentWorkspaceAtom = atom(null) -export function setWorkspace(workspaceData: Workspace[]): void { +export const useDefaultWorkspace = (): { + defaultWorkspace: Workspace | null + setDefaultWorkspace: (workspace: Workspace | null) => void +} => { + const [defaultWorkspace, setDefaultWorkspace] = useAtom(defaultWorkspaceAtom) + return { defaultWorkspace, setDefaultWorkspace } +} + +export const useCurrentWorkspace = (): { + currentWorkspace: Workspace | null + setCurrentWorkspace: (workspace: Workspace | null) => void +} => { + const [currentWorkspace, setCurrentWorkspace] = useAtom(currentWorkspaceAtom) + return { currentWorkspace, setCurrentWorkspace } +} + +export const SetWorkspace = (workspaceData: Workspace[]): void => { + const { setDefaultWorkspace } = useDefaultWorkspace() + const { currentWorkspace, setCurrentWorkspace } = useCurrentWorkspace() + const defaultWorkspace = workspaceData.find((workspace) => workspace.isDefault) || null - setDefaultWorkspaceAtom(defaultWorkspace) + setDefaultWorkspace(defaultWorkspace) - if (getCurrentWorkspace() === null) { - setCurrentWorkspace(defaultWorkspace!) + if (currentWorkspace === null && defaultWorkspace) { + setCurrentWorkspace(defaultWorkspace) } } -export function getCurrentWorkspace(): Workspace | null { - const [currentWorkspace] = useAtom(currentWorkspaceAtom) +export const GetCurrentWorkspace = (): Workspace | null => { + const { currentWorkspace } = useCurrentWorkspace() return currentWorkspace } -export function setCurrentWorkspace(workspace: Workspace): void { - setCurrentWorkspaceAtom(workspace) +export const SetCurrentWorkspace = (workspace: Workspace): void => { + const { setCurrentWorkspace } = useCurrentWorkspace() + setCurrentWorkspace(workspace) } //Utility functions -export function setDefaultWorkspaceAtom(workspace: Workspace | null): void { - const [, setDefaultWorkspace] = useAtom(defaultWorkspaceAtom) +export const UpdateDefaultWorkspaceAtom = ( + workspace: Workspace | null +): void => { + const { setDefaultWorkspace } = useDefaultWorkspace() setDefaultWorkspace(workspace) } -export function setCurrentWorkspaceAtom(workspace: Workspace | null): void { - const [, setCurrentWorkspace] = useAtom(currentWorkspaceAtom) +export const UpdateCurrentWorkspaceAtom = ( + workspace: Workspace | null +): void => { + const { setCurrentWorkspace } = useCurrentWorkspace() setCurrentWorkspace(workspace) }