Skip to content

Commit

Permalink
add initial toast
Browse files Browse the repository at this point in the history
  • Loading branch information
renardeinside committed Nov 25, 2024
1 parent bde8717 commit b73a63c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
48 changes: 32 additions & 16 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { EditorContent, Editor as TiptapEditor } from "@tiptap/react";
import { lazy, Suspense, useEffect, useMemo } from "react";
import { loadExtensions } from "@/lib/extensions";
import { LoaderCircle } from "lucide-react";
import { useDoc } from "@/hooks/use-doc";
import { dbExistsResource, useDoc } from "@/hooks/use-doc";
import * as Y from "yjs";
import { toast } from "sonner";

const EditorMenu = lazy(() => import("./EditorMenu"));

Expand All @@ -16,20 +17,23 @@ const Loading = () => {
);
};

const EditorView = ({ doc }: { doc: Y.Doc }) => {
const editor = useMemo(
() =>
new TiptapEditor({
extensions: [...loadExtensions(doc)],
editorProps: {
attributes: {
class:
"!w-full prose !max-w-none dark:prose-invert prose-md leading-tight focus:outline-none min-h-[90vh]",
},
const EditorView = ({ doc, firstLoad }: { doc: Y.Doc; firstLoad: boolean }) => {
const editor = useMemo(() => {
const editor = new TiptapEditor({
autofocus: true,
extensions: [...loadExtensions(doc)],
editorProps: {
attributes: {
class:
"!w-full prose !max-w-none dark:prose-invert prose-md leading-tight focus:outline-none min-h-[90vh]",
},
}),
[doc]
);
},
});
if (firstLoad) {
editor.chain().setContent("<p></p>".repeat(10)).focus("end").run();
}
return editor;
}, [doc, firstLoad]);

useEffect(() => {
if (!editor.isFocused) {
Expand All @@ -45,12 +49,24 @@ const EditorView = ({ doc }: { doc: Y.Doc }) => {
);
};

const Editor = () => {
export const Editor = () => {
const dbExists = dbExistsResource.read();
const doc = useDoc();

useEffect(() => {
if (!dbExists) {
// Display welcome toast
const timer = setTimeout(() => {
toast(<span>💜 Welcome to Strophe, your minimalistic notes</span>);
}, 100);

return () => clearTimeout(timer);
}
}, [dbExists, doc]);

return (
<Suspense fallback={<Loading />}>
<EditorView doc={doc} />
<EditorView doc={doc} firstLoad={!dbExists} />
</Suspense>
);
};
Expand Down
32 changes: 31 additions & 1 deletion src/hooks/use-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,39 @@ import * as Y from "yjs";
import { IndexeddbPersistence } from "y-indexeddb";
import { useEffect } from "react";

const dbName = "st-content";

export const getDbExists = () => {
let status = "pending";
let exists: boolean;

const suspender = indexedDB.databases().then((databases) => {
const dbExists = databases.some((db) => db.name === dbName);
status = "success";
exists = dbExists;
}).catch(() => {
status = "error";
});

return {
read() {
if (status === "pending") {
throw suspender; // Suspend rendering until the promise resolves
} else if (status === "error") {
throw new Error("Database check failed");
} else {
return exists;
}
},
};
};


export const getSyncedDoc = () => {
let status = "pending";
const doc = new Y.Doc();
const persistence = new IndexeddbPersistence("st-content", doc);

const persistence = new IndexeddbPersistence(dbName, doc);

const suspender = new Promise((resolve, reject) => {
persistence.whenSynced
Expand Down Expand Up @@ -33,6 +62,7 @@ export const getSyncedDoc = () => {
};

export const docResource = getSyncedDoc();
export const dbExistsResource = getDbExists();

export const useDoc = () => {
const doc = docResource.read();
Expand Down
1 change: 1 addition & 0 deletions src/lib/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const loadExtensions = (doc: Y.Doc) => {
Underline,
StarterKit.configure({
codeBlock: false,
history: false,
}),
CodeBlockLowlight.configure({
lowlight,
Expand Down
10 changes: 10 additions & 0 deletions tests/init.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from "./fixtures";

test("on the first load, a welcome toast should be visible", async ({
newtab,
}) => {
const toast = await newtab.waitForSelector(
'text="💜 Welcome to Strophe, your minimalistic notes"'
);
expect(toast).not.toBeNull();
});

0 comments on commit b73a63c

Please sign in to comment.