Skip to content

Commit

Permalink
Bug Fixes and refactoring collaboration module (#75)
Browse files Browse the repository at this point in the history
*  Refactor collaboration library to its own module
* Add Loading Skeleton to Sidebar items

* Remove demo page
  • Loading branch information
BrianUribe6 authored Mar 5, 2024
1 parent 7af2717 commit f2e6a88
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 198 deletions.
2 changes: 1 addition & 1 deletion apps/front-end/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
// FIXME Collaboration features not working correctly on React.StrictMode
reactStrictMode: false,
reactStrictMode: true,
};

module.exports = nextConfig;
1 change: 1 addition & 0 deletions apps/front-end/src/app/ActiveUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ function ActiveUsers() {
</div>
);
}

export default ActiveUsers;
20 changes: 17 additions & 3 deletions apps/front-end/src/app/SideNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
setLabel,
sidebarState,
} from "@/components/Sidebar/state";
import { useRootDocument } from "@/lib/collaboration";
import Skeleton from "@/components/Skeleton";
import { useIsSynced, useRootDocument } from "@/lib/collaboration";
import { atom, useAtom } from "jotai";
import { useParams, useRouter } from "next/navigation";
import { useEffect } from "react";
Expand All @@ -30,7 +31,7 @@ export const activeItemAtom = atom<Item | null>(null);

function SideNavigation() {
// Synchorize Yjs shared-types with valtio proxy state
useYjs();
const loaded = useYjs();
const router = useRouter();
const params = useParams<{ notebookId: string; noteId: string }>();
const [activeItem, setActiveItem] = useAtom(activeItemAtom);
Expand Down Expand Up @@ -91,7 +92,7 @@ function SideNavigation() {
onNewPage={handleNewPage}
>
<div className="flex-1 overflow-auto">
<SidebarItems />
{loaded ? <SidebarItems /> : <SidebarItemsSkeleton />}
</div>
<div className="space-y-1">
<SidebarDivider />
Expand All @@ -106,9 +107,22 @@ function SideNavigation() {

function useYjs() {
const rootDocument = useRootDocument();
const isSynced = useIsSynced();
useEffect(() => {
return bind(sidebarState, rootDocument.getArray("sidebar"));
}, [rootDocument]);

return isSynced;
}

function SidebarItemsSkeleton() {
return (
<div className="space-y-1">
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
</div>
);
}

export default SideNavigation;
2 changes: 1 addition & 1 deletion apps/front-end/src/app/[notebookId]/[noteId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function NotePage({ params }: { params: { noteId: string } }) {
notFound();
}

return <Editor documentId={params.noteId} />;
return <Editor collaboration={{ documentId: params.noteId }} />;
}

export default NotePage;
23 changes: 10 additions & 13 deletions apps/front-end/src/app/[notebookId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { getNotebookById } from "@/actions/document";
import { getToken } from "@/actions/token";
import { getSession } from "@/config/auth-options";
import { StartCollaboration } from "@/lib/collaboration/StartCollaboration";
import { notFound } from "next/navigation";
import { PropsWithChildren } from "react";
import Header from "../Header";
import SideNavigation from "../SideNavigation";
import { StartCollaboration } from "@/lib/collaboration";
import NoSSR from "@/components/NoSSR";

type LayoutProps = PropsWithChildren<{
params: {
Expand All @@ -31,17 +30,15 @@ async function Layout({ children, params }: LayoutProps) {
<div>
<Header />
{session?.user && (
<NoSSR>
<StartCollaboration
user={session.user}
notebookName={notebook.document.documentName}
initialToken={token}
onTokenRefresh={async () => {
"use server";
return getToken([]);
}}
/>
</NoSSR>
<StartCollaboration
user={session.user}
notebookName={notebook.document.documentName}
initialToken={token}
onTokenRefresh={async () => {
"use server";
return getToken([]);
}}
/>
)}
{children}
</div>
Expand Down
9 changes: 6 additions & 3 deletions apps/front-end/src/components/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const CollaborationPlugin = dynamic(
const TreeViewPlugin = lazy(() => import("@/plugins/TreeViewPlugin"));

type EditorProps = {
documentId: string;
collaboration?: { documentId: string };
};

function Editor(props: EditorProps) {
Expand All @@ -56,7 +56,7 @@ function Editor(props: EditorProps) {
<ContentEditable className="duration-200 ease-in animate-in fade-in-30 focus:outline-none" />
}
placeholder={
<p className="absolute left-4 top-0 m-0 dark:text-gray-400">
<p className="pointer-events-none absolute left-4 top-0 m-0 dark:text-gray-400">
Press{" "}
<span className="rounded bg-gray-200 px-2 py-1 dark:bg-slate-800">
/
Expand All @@ -66,6 +66,7 @@ function Editor(props: EditorProps) {
}
ErrorBoundary={ErrorBoundary}
/>

<AutoFocusPlugin />

<LexicalAutoLinkPlugin />
Expand All @@ -85,7 +86,9 @@ function Editor(props: EditorProps) {
<ListMaxIndentLevelPlugin />
<TabIndentationPlugin />
<ImagePlugin />
<CollaborationPlugin id={props.documentId} />
{props.collaboration && (
<CollaborationPlugin id={props.collaboration.documentId} />
)}

<CheckListPlugin />
</div>
Expand Down
19 changes: 19 additions & 0 deletions apps/front-end/src/components/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import clsx from "clsx";
import { ComponentPropsWithoutRef } from "react";

function Skeleton({
children,
className,
...rest
}: ComponentPropsWithoutRef<"div">) {
return (
<div
{...rest}
className={clsx("animate-pulse rounded-lg bg-slate-700", className)}
>
{children}
</div>
);
}

export default Skeleton;
177 changes: 0 additions & 177 deletions apps/front-end/src/lib/collaboration.tsx

This file was deleted.

Loading

0 comments on commit f2e6a88

Please sign in to comment.