-
-
Notifications
You must be signed in to change notification settings - Fork 19
Implement whiteboard management #3137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56425e7
feat: add new whiteboard page and maintain localstorage snapsnot mana…
khangronky 5a14899
feat: implement new whiteboard prototype page with search and sorting…
khangronky 7335216
feat: add whiteboards table and related RLS policies, implement snaps…
khangronky 6e51aef
feat: add functionality to load whiteboard snapshot and thumbnail fro…
khangronky 6fe2e17
refactor(whiteboards): Move save toolbar to a separate component
khangronky 9295caf
feat(whiteboards): add create and edit whiteboard dialogs with form h…
khangronky 2c61a37
fix(toolbar): update success toast message to reflect whiteboard save
khangronky 7205bbc
Merge remote-tracking branch 'origin/main' into feat/whiteboards
khangronky 6accf37
fix(types): remove duplicate fields from Database type definitions in…
khangronky 8f5216d
fix: Fix any issues raised by PR review bots
khangronky 1dd40f1
refactor(whiteboards): rename whiteboards table to workspace_whiteboa…
vhpx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
apps/db/supabase/migrations/20250620034755_create_whiteboards.sql
This file contains hidden or 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,88 @@ | ||
create table "public"."workspace_whiteboards" ( | ||
"id" uuid not null default gen_random_uuid(), | ||
"title" text not null, | ||
"description" text, | ||
"snapshot" jsonb, | ||
"ws_id" uuid not null, | ||
"created_at" timestamp with time zone not null default now(), | ||
"thumbnail_url" text, | ||
"updated_at" timestamp with time zone not null default now(), | ||
"creator_id" uuid not null | ||
); | ||
|
||
alter table "public"."workspace_whiteboards" enable row level security; | ||
|
||
CREATE UNIQUE INDEX workspace_whiteboards_pkey ON public.workspace_whiteboards USING btree (id); | ||
|
||
alter table "public"."workspace_whiteboards" add constraint "workspace_whiteboards_pkey" PRIMARY KEY using index "workspace_whiteboards_pkey"; | ||
|
||
alter table "public"."workspace_whiteboards" add constraint "workspace_whiteboards_creator_id_fkey" FOREIGN KEY (creator_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."workspace_whiteboards" validate constraint "workspace_whiteboards_creator_id_fkey"; | ||
|
||
alter table "public"."workspace_whiteboards" add constraint "workspace_whiteboards_ws_id_fkey" FOREIGN KEY (ws_id) REFERENCES workspaces(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."workspace_whiteboards" validate constraint "workspace_whiteboards_ws_id_fkey"; | ||
|
||
grant delete on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant insert on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant references on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant select on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant trigger on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant truncate on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant update on table "public"."workspace_whiteboards" to "anon"; | ||
|
||
grant delete on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant insert on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant references on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant select on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant trigger on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant truncate on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant update on table "public"."workspace_whiteboards" to "authenticated"; | ||
|
||
grant delete on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant insert on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant references on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant select on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant trigger on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant truncate on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
grant update on table "public"."workspace_whiteboards" to "service_role"; | ||
|
||
-- Add an index to improve join performance | ||
CREATE INDEX IF NOT EXISTS idx_whiteboards_ws_id ON workspace_whiteboards(ws_id); | ||
CREATE INDEX IF NOT EXISTS idx_whiteboards_creator_id ON workspace_whiteboards(creator_id); | ||
|
||
CREATE INDEX idx_whiteboards_snapshot_gin ON public.workspace_whiteboards USING GIN (snapshot); | ||
|
||
CREATE POLICY "Workspace members can read and write whiteboards" ON public.workspace_whiteboards | ||
FOR ALL TO authenticated | ||
USING ( | ||
ws_id IN ( | ||
SELECT ws_id FROM public.workspace_members | ||
WHERE user_id = auth.uid() | ||
) | ||
) | ||
WITH CHECK ( | ||
ws_id IN ( | ||
SELECT ws_id FROM public.workspace_members | ||
WHERE user_id = auth.uid() | ||
) | ||
); |
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
apps/web/src/app/[locale]/(dashboard)/[wsId]/whiteboards/[boardId]/loading.tsx
This file contains hidden or 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,8 @@ | ||
export default function Loading() { | ||
return ( | ||
<div className="flex h-full w-full flex-col gap-4 p-4"> | ||
<div className="h-10 w-48 animate-pulse rounded-lg bg-foreground/5" /> | ||
<div className="h-full w-full animate-pulse rounded-lg bg-foreground/5" /> | ||
</div> | ||
); | ||
} | ||
37 changes: 37 additions & 0 deletions
37
apps/web/src/app/[locale]/(dashboard)/[wsId]/whiteboards/[boardId]/page.tsx
This file contains hidden or 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 { CustomTldraw } from './custom-tldraw'; | ||
import { createClient } from '@tuturuuu/supabase/next/server'; | ||
import { notFound } from 'next/navigation'; | ||
import { TLStoreSnapshot } from 'tldraw'; | ||
|
||
interface TLDrawPageProps { | ||
params: Promise<{ wsId: string; boardId: string }>; | ||
} | ||
|
||
export default async function TLDrawPage({ params }: TLDrawPageProps) { | ||
const { wsId, boardId } = await params; | ||
|
||
const supabase = await createClient(); | ||
|
||
const { data: whiteboard } = await supabase | ||
.from('workspace_whiteboards') | ||
.select('*') | ||
.eq('id', boardId) | ||
.eq('ws_id', wsId) | ||
.single(); | ||
|
||
if (!whiteboard) return notFound(); | ||
|
||
return ( | ||
<div className="absolute inset-0"> | ||
<CustomTldraw | ||
wsId={wsId} | ||
boardId={boardId} | ||
initialData={ | ||
whiteboard.snapshot | ||
? (JSON.parse(whiteboard.snapshot as string) as TLStoreSnapshot) | ||
: undefined | ||
} | ||
/> | ||
</div> | ||
); | ||
} | ||
150 changes: 150 additions & 0 deletions
150
apps/web/src/app/[locale]/(dashboard)/[wsId]/whiteboards/[boardId]/toolbar.tsx
This file contains hidden or 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,150 @@ | ||
import { createClient } from '@tuturuuu/supabase/next/client'; | ||
import { Button } from '@tuturuuu/ui/button'; | ||
import { useToast } from '@tuturuuu/ui/hooks/use-toast'; | ||
import { ArrowLeftIcon } from '@tuturuuu/ui/icons'; | ||
import Link from 'next/link'; | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { useEditor } from 'tldraw'; | ||
|
||
export default function Toolbar({ | ||
wsId, | ||
boardId, | ||
}: { | ||
wsId: string; | ||
boardId: string; | ||
}) { | ||
const supabase = createClient(); | ||
|
||
const editor = useEditor(); | ||
const { toast } = useToast(); | ||
|
||
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); | ||
const lastSavedSnapshotRef = useRef<string | null>(null); | ||
|
||
// Initialize the last saved snapshot on mount | ||
useEffect(() => { | ||
if (editor && lastSavedSnapshotRef.current === null) { | ||
const currentSnapshot = JSON.stringify(editor.getSnapshot()); | ||
lastSavedSnapshotRef.current = currentSnapshot; | ||
} | ||
}, [editor]); | ||
|
||
// Listen for changes in the editor | ||
useEffect(() => { | ||
if (!editor) return; | ||
|
||
const checkForChanges = () => { | ||
const currentSnapshot = JSON.stringify(editor.getSnapshot()); | ||
const hasChanges = currentSnapshot !== lastSavedSnapshotRef.current; | ||
setHasUnsavedChanges(hasChanges); | ||
}; | ||
|
||
// Check for changes on various editor events | ||
const unsubscribeHistory = editor.store.listen(() => { | ||
checkForChanges(); | ||
}); | ||
|
||
return () => { | ||
unsubscribeHistory(); | ||
}; | ||
}, [editor]); | ||
|
||
const generateThumbnail = async () => { | ||
const shapeIds = editor.getCurrentPageShapeIds(); | ||
if (shapeIds.size === 0) { | ||
throw new Error('No shapes on canvas'); | ||
} | ||
|
||
const { blob } = await editor.toImage([...shapeIds], { | ||
format: 'png', | ||
background: true, | ||
scale: 0.3, | ||
padding: 16, | ||
}); | ||
|
||
return blob; | ||
}; | ||
|
||
const generateFileName = (name: string) => { | ||
const now = new Date(); | ||
const year = now.getFullYear(); | ||
const month = String(now.getMonth() + 1).padStart(2, '0'); | ||
const day = String(now.getDate()).padStart(2, '0'); | ||
const hours = String(now.getHours()).padStart(2, '0'); | ||
const minutes = String(now.getMinutes()).padStart(2, '0'); | ||
const seconds = String(now.getSeconds()).padStart(2, '0'); | ||
|
||
return `${year}${month}${day}-${hours}-${minutes}-${seconds}-${name}`; | ||
}; | ||
|
||
const save = useCallback(async () => { | ||
try { | ||
const thumbnailBlob = await generateThumbnail(); | ||
if (!thumbnailBlob) { | ||
throw new Error('Failed to generate thumbnail'); | ||
} | ||
|
||
const thumbnailFileName = generateFileName(`${boardId}.png`); | ||
const thumbnailFile = new File([thumbnailBlob], thumbnailFileName, { | ||
type: 'image/png', | ||
}); | ||
|
||
const { error: thumbnailError } = await supabase.storage | ||
.from('workspaces') | ||
.upload( | ||
`${wsId}/whiteboards/${boardId}/${thumbnailFileName}`, | ||
thumbnailFile | ||
); | ||
|
||
if (thumbnailError) throw thumbnailError; | ||
|
||
const { data: thumbnailUrl } = supabase.storage | ||
.from('workspaces') | ||
.getPublicUrl(`${wsId}/whiteboards/${boardId}/${thumbnailFileName}`); | ||
|
||
const snapshot = editor.getSnapshot(); | ||
|
||
const { error: updateError } = await supabase | ||
.from('workspace_whiteboards') | ||
.update({ | ||
snapshot: JSON.stringify(snapshot), | ||
thumbnail_url: thumbnailUrl.publicUrl, | ||
updated_at: new Date().toISOString(), | ||
}) | ||
.eq('id', boardId); | ||
|
||
if (updateError) throw updateError; | ||
|
||
// Update the last saved snapshot reference | ||
lastSavedSnapshotRef.current = JSON.stringify(snapshot); | ||
setHasUnsavedChanges(false); | ||
|
||
toast({ | ||
title: 'Saved successfully!', | ||
description: 'Your whiteboard has been saved.', | ||
variant: 'default', | ||
}); | ||
} catch (error: any) { | ||
console.error(error); | ||
toast({ | ||
title: 'Error saving', | ||
description: 'Failed to save whiteboard', | ||
variant: 'destructive', | ||
}); | ||
} | ||
}, [editor, boardId, supabase, toast, wsId]); | ||
|
||
return ( | ||
<div className="pointer-events-auto flex gap-4 p-4"> | ||
<Link href={`/${wsId}/whiteboards`}> | ||
<Button variant="ghost"> | ||
<ArrowLeftIcon className="h-4 w-4" /> | ||
Back | ||
</Button> | ||
</Link> | ||
<Button variant="default" onClick={save} disabled={!hasUnsavedChanges}> | ||
Save Snapshot | ||
</Button> | ||
</div> | ||
); | ||
} | ||
Check warning on line 150 in apps/web/src/app/[locale]/(dashboard)/[wsId]/whiteboards/[boardId]/toolbar.tsx
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.