websocket updates

* sync page title on icon via websocket
* sync on page tree too
This commit is contained in:
Philipinho
2024-04-27 15:40:22 +01:00
parent 390d58793a
commit 8cc7d39146
17 changed files with 253 additions and 15 deletions

View File

@ -13,7 +13,9 @@ const useCollaborationURL = (): string => {
}
*/
const API_URL = window.location.protocol + "//" + window.location.host;
const API_URL = import.meta.env.DEV
? "http://localhost:3000"
: window.location.protocol + "//" + window.location.host;
const wsProtocol = API_URL.startsWith("https") ? "wss" : "ws";
return `${wsProtocol}://${API_URL.split("://")[1]}${PATH}`;

View File

@ -15,6 +15,8 @@ import { useDebouncedValue } from "@mantine/hooks";
import { useAtom } from "jotai";
import { treeDataAtom } from "@/features/page/tree/atoms/tree-data-atom";
import { updateTreeNodeName } from "@/features/page/tree/utils";
import { useQueryEmit } from "@/features/websocket/use-query-emit.ts";
import { History } from "@tiptap/extension-history";
export interface TitleEditorProps {
pageId: string;
@ -28,6 +30,7 @@ export function TitleEditor({ pageId, title }: TitleEditorProps) {
const pageEditor = useAtomValue(pageEditorAtom);
const [, setTitleEditor] = useAtom(titleEditorAtom);
const [treeData, setTreeData] = useAtom(treeDataAtom);
const emit = useQueryEmit();
const titleEditor = useEditor({
extensions: [
@ -41,6 +44,9 @@ export function TitleEditor({ pageId, title }: TitleEditorProps) {
Placeholder.configure({
placeholder: "Untitled",
}),
History.configure({
depth: 20,
}),
],
onCreate({ editor }) {
if (editor) {
@ -59,6 +65,15 @@ export function TitleEditor({ pageId, title }: TitleEditorProps) {
if (debouncedTitle !== "") {
updatePageMutation.mutate({ pageId, title: debouncedTitle });
setTimeout(() => {
emit({
operation: "updateOne",
entity: ["pages"],
id: pageId,
payload: { title: debouncedTitle },
});
}, 50);
const newTreeData = updateTreeNodeName(treeData, pageId, debouncedTitle);
setTreeData(newTreeData);
}