-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,60 @@ | ||
import type { Workspace } from '@/types' | ||
import { atom, useAtom } from 'jotai' | ||
import type { Workspace } from '@/types' | ||
|
||
const defaultWorkspaceAtom = atom<Workspace | null>(null) | ||
const currentWorkspaceAtom = atom<Workspace | null>(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) | ||
} |