Skip to content

Commit

Permalink
loading workspaces on first load
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpokar committed Apr 2, 2024
1 parent a5e3697 commit 97b1b1e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
25 changes: 24 additions & 1 deletion apps/ui/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,39 @@ import clsx from 'clsx'
import { useTheme } from '../providers/ThemeProvider'
import { useWorkspace } from '../providers/WorkspaceProvider'
import AddWorkspace from './AddWorkspace'
import { EventWithPayload, ReadyEvent } from '@manasai/events'
import { useSocket } from '../providers/SocketProvider'

const Navbar: React.FC = () => {
const { isDarkMode, toggleTheme } = useTheme()
const { workspaces, activeWorkspace, onWorkspaceChange } = useWorkspace()
const { workspaces, activeWorkspace, onWorkspaceChange, setWorkspaces } =
useWorkspace()
const [workspaceSelectorOpen, shouldOpenShowWorkspaceSelector] =
useState(false)
const [showAddWorkspace, shouldShowAddWorkspace] = useState(false)
const { on } = useSocket()

const workspaceSelectorRef = useRef<HTMLDivElement>(null)

useEffect(() => {
on('READY', (event: EventWithPayload<unknown>) => {
const { workspaces } = event.payload as ReadyEvent['payload']
setWorkspaces(workspaces)

if (workspaces.length === 1) {
onWorkspaceChange(workspaces[0].id)
return
}

if (workspaces.length === 0) {
shouldShowAddWorkspace(true)
return
}

shouldOpenShowWorkspaceSelector(true)
})
}, [on, setWorkspaces, onWorkspaceChange, workspaceSelectorRef])

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
Expand Down
30 changes: 28 additions & 2 deletions apps/ui/src/providers/SocketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import {
import {
ConnectedEvent,
DisconnectedEvent,
EventWithPayload
EventWithPayload,
EventTypes
} from '@manasai/events'
import ShortUniqueId from 'short-unique-id'

interface SocketContextProps {
emit: (event: EventWithPayload<unknown>) => void
on: (
event: EventTypes,
listener: (event: EventWithPayload<unknown>) => void
) => void
}

interface SocketProviderProps {
Expand All @@ -26,6 +31,9 @@ const DEVICE_TOKEN_KEY = 'device_token'
const SocketContext = createContext<SocketContextProps>({
emit: () => {
throw new Error('SocketProvider is not initialized')
},
on: () => {
throw new Error('SocketProvider is not initialized')
}
})

Expand All @@ -50,6 +58,22 @@ export const SocketProvider: React.FC<SocketProviderProps> = ({
[client, deviceToken]
)

const on = useCallback(
(
event: EventTypes,
listener: (event: EventWithPayload<unknown>) => void
) => {
client.addEventListener('message', (e: MessageEvent) => {
const data = JSON.parse(e.data) as EventWithPayload<unknown>

if (data.type !== event) return

listener(data)
})
},
[client]
)

useEffect(() => {
if (!deviceToken) {
let deviceId = localStorage.getItem(DEVICE_TOKEN_KEY)
Expand Down Expand Up @@ -95,7 +119,9 @@ export const SocketProvider: React.FC<SocketProviderProps> = ({
}, [client, emit, deviceToken])

return (
<SocketContext.Provider value={{ emit }}>{children}</SocketContext.Provider>
<SocketContext.Provider value={{ emit, on }}>
{children}
</SocketContext.Provider>
)
}

Expand Down
7 changes: 5 additions & 2 deletions apps/ui/src/providers/WorkspaceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface WorkspaceContextProps {
addWorkspace: (workspace: Workspace) => void
onWorkspaceChange: (id: string) => void
isNameTaken: (name: string) => boolean
setWorkspaces: (workspaces: Workspace[]) => void
}

interface WorkspaceProviderProps {
Expand All @@ -17,7 +18,8 @@ const WorkspaceContext = createContext<WorkspaceContextProps>({
workspaces: [],
addWorkspace: () => null,
onWorkspaceChange: () => null,
isNameTaken: () => false
isNameTaken: () => false,
setWorkspaces: () => null
})

export const WorkspaceProvider: React.FC<WorkspaceProviderProps> = ({
Expand Down Expand Up @@ -45,7 +47,8 @@ export const WorkspaceProvider: React.FC<WorkspaceProviderProps> = ({
workspaces,
addWorkspace,
onWorkspaceChange,
isNameTaken
isNameTaken,
setWorkspaces
}

if (activeWorkspaceId) {
Expand Down

0 comments on commit 97b1b1e

Please sign in to comment.