Skip to content

Commit

Permalink
feature: update workspace name
Browse files Browse the repository at this point in the history
Implements first version of settings page with workspace name update
  • Loading branch information
geclos committed Jul 26, 2024
1 parent c3bf7b1 commit 11ebfec
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 219 deletions.
29 changes: 29 additions & 0 deletions apps/web/src/actions/workspaces/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use server'

import { updateWorkspace, WorkspacesRepository } from '@latitude-data/core'
import { z } from 'zod'

import { authProcedure } from '../procedures'

export const updateWorkspaceAction = authProcedure
.createServerAction()
.input(
z.object({
workspaceId: z.number(),
name: z.string(),
}),
)
.handler(async ({ input, ctx }) => {
const userId = ctx.session.userId
const workspacesScope = new WorkspacesRepository(userId)
const workspace = await workspacesScope
.getWorkspaceById(input.workspaceId)
.then((r) => r.unwrap())

const updatedWorkspace = await updateWorkspace({
workspace,
name: input.name,
}).then((r) => r.unwrap())

return updatedWorkspace!
})
19 changes: 19 additions & 0 deletions apps/web/src/app/(private)/settings/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ReactNode } from 'react'

import { AppLayout } from '@latitude-data/web-ui/browser'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

import { NAV_LINKS } from '../_lib/constants'

export default async function Layout({ children }: { children: ReactNode }) {
const session = await getCurrentUser()
return (
<AppLayout
navigationLinks={NAV_LINKS}
currentUser={session.user}
breadcrumbs={[{ name: 'Settings' }]}
>
{children}
</AppLayout>
)
}
10 changes: 10 additions & 0 deletions apps/web/src/app/(private)/settings/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client'

import { Settings } from '@latitude-data/web-ui'
import useCurrentWorkspace from '$/stores/currentWorkspace'

export default function SettingsPage() {
const { data, update } = useCurrentWorkspace()

return <Settings workspace={data!} updateWorkspace={update} />
}
93 changes: 0 additions & 93 deletions apps/web/src/components/Sidebar/DocumentTree/index.tsx

This file was deleted.

38 changes: 0 additions & 38 deletions apps/web/src/components/Sidebar/index.tsx

This file was deleted.

32 changes: 0 additions & 32 deletions apps/web/src/components/Sidebar/toTree.ts

This file was deleted.

41 changes: 41 additions & 0 deletions apps/web/src/stores/currentWorkspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useCallback } from 'react'

import { SessionWorkspace, useSession, useToast } from '@latitude-data/web-ui'
import { updateWorkspaceAction } from '$/actions/workspaces/update'
import useSWR, { SWRConfiguration } from 'swr'
import { useServerAction } from 'zsa-react'

export default function useCurrentWorkspace(opts?: SWRConfiguration) {
const key = '/api/workspaces/current'
const session = useSession()
const { toast } = useToast()
const { mutate, data, ...rest } = useSWR<SessionWorkspace>(
key,
async (_) => session.workspace,
{
...opts,
fallbackData: session.workspace,
},
)

const { execute } = useServerAction(updateWorkspaceAction)
const update = useCallback(
async (payload: { name: string }) => {
const [workspace, error] = await execute({
workspaceId: data!.id,
name: payload.name,
})
if (error) throw error

toast({
title: 'Name updated to ' + payload.name,
})

mutate(workspace)
return workspace
},
[mutate],
)

return { data: data!, update, ...rest }
}
50 changes: 0 additions & 50 deletions apps/web/src/stores/documentVersions.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/src/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './commitsRepository'
export * from './projectsRepository'
export * from './documentVersionsRepository'
export * from './workspacesRepository'
37 changes: 37 additions & 0 deletions packages/core/src/repositories/workspacesRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Database, database } from '$core/client'
import { NotFoundError, Result } from '$core/lib'
import { memberships, workspaces } from '$core/schema'
import { eq, getTableColumns } from 'drizzle-orm'

export class WorkspacesRepository {
public userId: string
private db: Database

constructor(userId: string, db = database) {
this.userId = userId
this.db = db
}

get scope() {
return this.db
.select(getTableColumns(workspaces))
.from(workspaces)
.innerJoin(memberships, eq(memberships.workspaceId, workspaces.id))
.where(eq(memberships.userId, this.userId))
.as('workspacesScope')
}

async getWorkspaceById(workspaceId: number) {
const result = await this.db
.select()
.from(this.scope)
.where(eq(this.scope.id, workspaceId))
.limit(1)
const workspace = result[0]
if (!workspace) {
return Result.error(new NotFoundError('Workspace not found'))
}

return Result.ok(workspace)
}
}
1 change: 1 addition & 0 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './workspaces'
export * from './documents'
export * from './commits'
export * from './projects'
export * from './workspaces'
1 change: 1 addition & 0 deletions packages/core/src/services/workspaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create'
export * from './update'
21 changes: 21 additions & 0 deletions packages/core/src/services/workspaces/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Result, Transaction } from '$core/lib'
import { Workspace, workspaces } from '$core/schema'
import { eq } from 'drizzle-orm'

export async function updateWorkspace({
workspace,
name,
}: {
workspace: Workspace
name: string
}) {
return await Transaction.call(async (tx) => {
const updated = await tx
.update(workspaces)
.set({ name })
.where(eq(workspaces.id, workspace.id))
.returning()

return Result.ok(updated[0])
})
}
Loading

0 comments on commit 11ebfec

Please sign in to comment.