Skip to content

Commit

Permalink
remember last workspace visited by user
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Nov 29, 2024
1 parent 0506741 commit 6aa5489
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 13 deletions.
6 changes: 3 additions & 3 deletions apps/api/src/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function sessionFromCookies(
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
workspaces: true,
Expand All @@ -130,6 +131,7 @@ export async function sessionFromCookies(
email: user.email,
name: user.name,
picture: user.picture,
lastWorkspaceVisited: user.lastWorkspaceVisited,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
},
Expand Down Expand Up @@ -319,9 +321,7 @@ export const isAuthorizedForDataSource = async (
return result !== null
}
case 'databrickssql': {
const result = await prisma().databricksSQLDataSource.findFirst(
query,
)
const result = await prisma().databricksSQLDataSource.findFirst(query)
return result !== null
}
}
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/websocket/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export const joinWorkspace =

if (!socket.rooms.has(workspaceId)) {
await socket.join(workspaceId)
await prisma().user.update({
where: { id: session.user.id },
data: {
lastWorkspaceVisited: workspaceId,
},
})
}

await emitInitialData(io, socket, workspaceId)
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class WorkspaceCreator implements IWorkspaceCreator {
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/yjs/v2/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function unknownUser(): ApiUser {
name: 'unknown',
email: 'unknown',
picture: null,
lastWorkspaceVisited: null,
createdAt: new Date(),
updatedAt: new Date(),
}
Expand Down
15 changes: 12 additions & 3 deletions apps/web/src/hooks/useSessionRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ export const useSessionRedirect = (shouldRedirect = true) => {
} else {
redirectToLogin(router)
}
} else if (workspaces.data.length === 0) {
signOut()
} else {
router.replace(`/workspaces/${workspaces.data[0].id}/documents`)
const user = session.data
const workspace =
workspaces.data.find(
(workspace) => workspace.id === user.lastWorkspaceVisited
) ??
workspaces.data.find((workspace) => workspace.ownerId === user.id) ??
workspaces.data[0]
if (workspace) {
router.replace(`/workspaces/${workspaces.data[0].id}/documents`)
} else {
signOut()
}
}
}, [properties, workspaces, session, router, shouldRedirect, signOut])
}
12 changes: 9 additions & 3 deletions apps/web/src/pages/workspaces/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useSignout } from '@/hooks/useAuth'
import { useSession, useSignout } from '@/hooks/useAuth'
import { useWorkspaces } from '@/hooks/useWorkspaces'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
Expand All @@ -7,13 +7,19 @@ export default function WorkspacesPage() {
const router = useRouter()
const [{ isLoading, data }] = useWorkspaces()
const signOut = useSignout()
const session = useSession()

useEffect(() => {
if (isLoading) {
if (isLoading || session.isLoading) {
return
}

const workspace = data[0]
const workspace =
data.find(
(workspace) => workspace.id === session.data?.lastWorkspaceVisited
) ??
data.find((workspace) => workspace.ownerId === session.data?.id) ??
data[0]
if (!workspace) {
signOut()
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "lastWorkspaceVisited" TEXT;
9 changes: 5 additions & 4 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,11 @@ model User {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
workspacesInvitees UserWorkspace[] @relation("UserWorkspaceInviter")
workspaces UserWorkspace[] @relation("UserWorkspaceUser")
ownedWorkspaces Workspace[]
confirmedAt DateTime?
workspacesInvitees UserWorkspace[] @relation("UserWorkspaceInviter")
workspaces UserWorkspace[] @relation("UserWorkspaceUser")
ownedWorkspaces Workspace[]
confirmedAt DateTime?
lastWorkspaceVisited String?
comments Comment[]
Favorite Favorite[]
Expand Down
7 changes: 7 additions & 0 deletions packages/database/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function createUser(
picture: true,
createdAt: true,
updatedAt: true,
lastWorkspaceVisited: true,
},
})
}
Expand All @@ -40,6 +41,7 @@ export async function getUserById(id: string): Promise<ApiUser | null> {
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand All @@ -58,6 +60,7 @@ export async function getUserByEmail(email: string): Promise<ApiUser | null> {
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand All @@ -78,6 +81,7 @@ export async function listWorkspaceUsers(
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand Down Expand Up @@ -111,6 +115,7 @@ export async function addUserToWorkspace(
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand Down Expand Up @@ -143,6 +148,7 @@ export async function deleteUserFromWorkspace(
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand Down Expand Up @@ -170,6 +176,7 @@ export async function confirmUser(userId: string): Promise<ApiUser | null> {
email: true,
name: true,
picture: true,
lastWorkspaceVisited: true,
createdAt: true,
updatedAt: true,
},
Expand Down

0 comments on commit 6aa5489

Please sign in to comment.