Skip to content

Commit

Permalink
Merge pull request #271 from briefercloud/remember-user-workspace
Browse files Browse the repository at this point in the history
remember last workspace visited by user
  • Loading branch information
vieiralucas authored Nov 29, 2024
2 parents 0506741 + c3f09f1 commit 542724b
Show file tree
Hide file tree
Showing 9 changed files with 51 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,
lastVisitedWorkspaceId: 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,
lastVisitedWorkspaceId: user.lastVisitedWorkspaceId,
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: {
lastVisitedWorkspaceId: 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,
lastVisitedWorkspaceId: 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,
lastVisitedWorkspaceId: 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.lastVisitedWorkspaceId
) ??
workspaces.data.find((workspace) => workspace.ownerId === user.id) ??
workspaces.data[0]
if (workspace) {
router.replace(`/workspaces/${workspace.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?.lastVisitedWorkspaceId
) ??
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,5 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "lastVisitedWorkspaceId" UUID;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_lastVisitedWorkspaceId_fkey" FOREIGN KEY ("lastVisitedWorkspaceId") REFERENCES "Workspace"("id") ON DELETE SET NULL ON UPDATE CASCADE;
11 changes: 7 additions & 4 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,13 @@ 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?
lastVisitedWorkspaceId String? @db.Uuid
lastVisitedWorkspace Workspace? @relation(fields: [lastVisitedWorkspaceId], references: [id], name: "lastVisitedWorkspace", onDelete: SetNull)
comments Comment[]
Favorite Favorite[]
UserYjsAppDocument UserYjsAppDocument[]
Expand Down Expand Up @@ -502,6 +504,7 @@ model Workspace {
snowflakeDataSources SnowflakeDataSource[]
databricksSQLDataSources DatabricksSQLDataSource[]
environmentVariables EnvironmentVariable[]
lastVisitors User[] @relation("lastVisitedWorkspace")
}

enum UserWorkspaceRole {
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,
lastVisitedWorkspaceId: true,
},
})
}
Expand All @@ -40,6 +41,7 @@ export async function getUserById(id: string): Promise<ApiUser | null> {
email: true,
name: true,
picture: true,
lastVisitedWorkspaceId: 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,
lastVisitedWorkspaceId: true,
createdAt: true,
updatedAt: true,
},
Expand All @@ -78,6 +81,7 @@ export async function listWorkspaceUsers(
email: true,
name: true,
picture: true,
lastVisitedWorkspaceId: true,
createdAt: true,
updatedAt: true,
},
Expand Down Expand Up @@ -111,6 +115,7 @@ export async function addUserToWorkspace(
email: true,
name: true,
picture: true,
lastVisitedWorkspaceId: true,
createdAt: true,
updatedAt: true,
},
Expand Down Expand Up @@ -143,6 +148,7 @@ export async function deleteUserFromWorkspace(
email: true,
name: true,
picture: true,
lastVisitedWorkspaceId: 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,
lastVisitedWorkspaceId: true,
createdAt: true,
updatedAt: true,
},
Expand Down

0 comments on commit 542724b

Please sign in to comment.