|
2 | 2 | import { createClient } from "actor-core/client";
|
3 | 3 | import { createReactActorCore } from "@actor-core/react";
|
4 | 4 | import { useState, useEffect } from "react";
|
5 |
| -import type { App } from "../actors/app"; |
| 5 | +import type { |
| 6 | + App, Cursor, TextUpdatedEvent, |
| 7 | + CursorUpdateEvent, UserDisconnectedEvent |
| 8 | +} from "../actors/app"; |
6 | 9 |
|
7 | 10 | const client = createClient<App>("http://localhost:6420");
|
8 | 11 | const { useActor, useActorEvent } = createReactActorCore(client);
|
9 | 12 |
|
10 | 13 | export function DocumentEditor() {
|
11 | 14 | // Connect to actor for this document ID from URL
|
12 | 15 | const documentId = new URLSearchParams(window.location.search).get('id') || 'default-doc';
|
13 |
| - const [{ actor, connectionId }] = useActor("document", { tags: { id: documentId } }); |
14 |
| - |
| 16 | + const [{ actor, state }] = useActor("document", { tags: { id: documentId } }); |
| 17 | + |
15 | 18 | // Local state
|
16 | 19 | const [text, setText] = useState("");
|
17 | 20 | const [cursorPos, setCursorPos] = useState({ x: 0, y: 0 });
|
18 |
| - const [otherCursors, setOtherCursors] = useState({}); |
| 21 | + const [otherCursors, setOtherCursors] = useState<Record<string, Cursor>>({}); |
19 | 22 |
|
20 | 23 | // Load initial document state
|
21 | 24 | useEffect(() => {
|
22 |
| - if (actor) { |
| 25 | + if (actor && state === "created") { |
23 | 26 | actor.getText().then(setText);
|
24 | 27 | actor.getCursors().then(setOtherCursors);
|
25 | 28 | }
|
26 |
| - }, [actor]); |
| 29 | + }, [actor, state]); |
27 | 30 |
|
28 | 31 | // Listen for updates from other users
|
29 |
| - useActorEvent({ actor, event: "textUpdated" }, ({ text: newText, userId: senderId }) => { |
30 |
| - if (senderId !== connectionId) setText(newText); |
| 32 | + useActorEvent({ actor, event: "textUpdated" }, (event) => { |
| 33 | + const { text: newText, userId: _senderId } = event as TextUpdatedEvent; |
| 34 | + |
| 35 | + setText(newText); |
31 | 36 | });
|
32 | 37 |
|
33 |
| - useActorEvent({ actor, event: "cursorUpdated" }, ({ userId: cursorUserId, x, y }) => { |
34 |
| - if (cursorUserId !== connectionId) { |
35 |
| - setOtherCursors(prev => ({ |
36 |
| - ...prev, |
37 |
| - [cursorUserId]: { x, y, userId: cursorUserId } |
38 |
| - })); |
39 |
| - } |
| 38 | + useActorEvent({ actor, event: "cursorUpdated" }, (event) => { |
| 39 | + const { userId: cursorUserId, x, y } = event as CursorUpdateEvent; |
| 40 | + |
| 41 | + setOtherCursors(prev => ({ |
| 42 | + ...prev, |
| 43 | + [cursorUserId]: { x, y, userId: cursorUserId } |
| 44 | + })); |
40 | 45 | });
|
41 |
| - |
42 |
| - // Update cursor position |
43 |
| - const updateCursor = (e) => { |
44 |
| - if (!actor) return; |
45 |
| - const rect = e.currentTarget.getBoundingClientRect(); |
46 |
| - const x = e.clientX - rect.left; |
47 |
| - const y = e.clientY - rect.top; |
| 46 | + |
| 47 | + useActorEvent({ actor, event: "userDisconnected" }, (event) => { |
| 48 | + const { userId } = event as UserDisconnectedEvent; |
48 | 49 |
|
49 |
| - if (x !== cursorPos.x || y !== cursorPos.y) { |
50 |
| - setCursorPos({ x, y }); |
51 |
| - actor.updateCursor(x, y); |
52 |
| - } |
53 |
| - }; |
| 50 | + setOtherCursors(prev => { |
| 51 | + const newCursors = { ...prev }; |
| 52 | + delete newCursors[userId]; |
| 53 | + return newCursors; |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!actor || state !== "created") return; |
| 60 | + |
| 61 | + const updateCursor = ({ x, y }: { x: number, y: number }) => { |
| 62 | + |
| 63 | + if (x !== cursorPos.x || y !== cursorPos.y) { |
| 64 | + setCursorPos({ x, y }); |
| 65 | + actor.updateCursor(x, y); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + window.addEventListener("mousemove", (e) => { |
| 70 | + const x = e.clientX; |
| 71 | + const y = e.clientY; |
| 72 | + |
| 73 | + updateCursor({ x, y }); |
| 74 | + }); |
| 75 | + }, [actor, state]); |
54 | 76 |
|
55 | 77 | return (
|
56 | 78 | <div className="document-editor">
|
57 | 79 | <h2>Document: {documentId}</h2>
|
58 | 80 |
|
59 |
| - <div onMouseMove={updateCursor}> |
| 81 | + <div> |
60 | 82 | <textarea
|
61 | 83 | value={text}
|
62 | 84 | onChange={(e) => {
|
63 | 85 | const newText = e.target.value;
|
64 | 86 | setText(newText);
|
65 |
| - actor?.setText(newText); |
| 87 | + if (actor && state === "created") { |
| 88 | + actor.setText(newText); |
| 89 | + } |
66 | 90 | }}
|
67 | 91 | placeholder="Start typing..."
|
68 | 92 | />
|
69 | 93 |
|
70 | 94 | {/* Other users' cursors */}
|
71 |
| - {Object.values(otherCursors).map((cursor: any) => ( |
| 95 | + {Object.values(otherCursors).map((cursor) => ( |
72 | 96 | <div
|
73 | 97 | key={cursor.userId}
|
74 | 98 | style={{
|
|
0 commit comments