Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): Replace localStorage with Jotai #439

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions apps/platform/src/components/ui/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { apiClient } from '@/lib/api-client'
import type { Workspace } from '@/types'
import { zWorkspace } from '@/types'
import {
getCurrentWorkspace,
setCurrentWorkspace,
setWorkspace
GetCurrentWorkspace,
SetCurrentWorkspace,
SetWorkspace
} from '@/lib/workspace-storage'
import { Input } from './input'
import { Label } from './label'
Expand Down Expand Up @@ -74,7 +74,7 @@ export function Combobox(): React.JSX.Element {
const response = await apiClient.post<Workspace>('/workspace', {
name
})
setCurrentWorkspace(response)
SetCurrentWorkspace(response)
setOpen(false)
} catch (error) {
// eslint-disable-next-line no-console -- we need to log the error
Expand All @@ -87,7 +87,7 @@ export function Combobox(): React.JSX.Element {
.then((data) => {
if (data) {
setAllWorkspace(data)
setWorkspace(data)
SetWorkspace(data)
}
})
.catch((error) => {
Expand All @@ -112,7 +112,7 @@ export function Combobox(): React.JSX.Element {
</div>
<div className="flex flex-col items-start">
<div className="text-lg text-white">
{getCurrentWorkspace()?.name ?? 'No workspace'}
{GetCurrentWorkspace()?.name ?? 'No workspace'}
</div>
<span className="text-xs text-white/55">100+ projects</span>
</div>
Expand All @@ -132,7 +132,7 @@ export function Combobox(): React.JSX.Element {
<CommandItem
key={workspace.id}
onSelect={() => {
setCurrentWorkspace(workspace)
SetCurrentWorkspace(workspace)
router.refresh()
setOpen(false)
}}
Expand All @@ -141,7 +141,7 @@ export function Combobox(): React.JSX.Element {
<Check
className={cn(
'mr-2 h-4 w-4',
getCurrentWorkspace()?.name === workspace.name
GetCurrentWorkspace()?.name === workspace.name
? 'opacity-100'
: 'opacity-0'
)}
Expand Down
65 changes: 48 additions & 17 deletions apps/platform/src/lib/workspace-storage.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
import { atom, useAtom } from 'jotai'
import type { Workspace } from '@/types'

export function setWorkspace(workspaceData: Workspace[]): void {
const defaultWorkspaceAtom = atom<Workspace | null>(null)
const currentWorkspaceAtom = atom<Workspace | null>(null)

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
if (typeof localStorage !== 'undefined') {
localStorage.setItem('defaultWorkspace', JSON.stringify(defaultWorkspace))

if (getCurrentWorkspace() === null) {
localStorage.setItem('currentWorkspace', JSON.stringify(defaultWorkspace))
}
setDefaultWorkspace(defaultWorkspace)

if (currentWorkspace === null && defaultWorkspace) {
setCurrentWorkspace(defaultWorkspace)
}
}

export function getCurrentWorkspace(): Workspace | null {
const currentWorkspace =
typeof localStorage !== 'undefined'
? localStorage.getItem('currentWorkspace')
: `{}`
export const GetCurrentWorkspace = (): Workspace | null => {
const { currentWorkspace } = useCurrentWorkspace()
return currentWorkspace
}

if (currentWorkspace) {
return JSON.parse(currentWorkspace) as Workspace
}
return null
export const SetCurrentWorkspace = (workspace: Workspace): void => {
const { setCurrentWorkspace } = useCurrentWorkspace()
setCurrentWorkspace(workspace)
}

//Utility functions
export const UpdateDefaultWorkspaceAtom = (
workspace: Workspace | null
): void => {
const { setDefaultWorkspace } = useDefaultWorkspace()
setDefaultWorkspace(workspace)
}

export function setCurrentWorkspace(workspace: Workspace): void {
localStorage.setItem('currentWorkspace', JSON.stringify(workspace))
export const UpdateCurrentWorkspaceAtom = (
workspace: Workspace | null
): void => {
const { setCurrentWorkspace } = useCurrentWorkspace()
setCurrentWorkspace(workspace)
}