-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements first version of settings page with workspace name update
- Loading branch information
Showing
19 changed files
with
224 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './create' | ||
export * from './update' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
} |
Oops, something went wrong.