Skip to content

Commit

Permalink
(FE) Add Presence to Editor Header (#90)
Browse files Browse the repository at this point in the history
* Add presence to header

* Fix lint
  • Loading branch information
devleejb authored Jan 23, 2024
1 parent 7b8a387 commit 1b085a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 13 additions & 0 deletions frontend/src/components/headers/EditorHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
AppBar,
Avatar,
AvatarGroup,
Paper,
Stack,
ToggleButton,
Expand Down Expand Up @@ -63,6 +65,17 @@ function EditorHeader() {
</Paper>
</Stack>
<Stack direction="row" alignItems="center" gap={1}>
<AvatarGroup max={4}>
{editorState.doc?.getPresences()?.map((presence) => (
<Avatar
key={presence.clientID}
alt={presence.presence.name}
sx={{ bgcolor: presence.presence.color }}
>
{presence.presence.name[0]}
</Avatar>
))}
</AvatarGroup>
{!editorState.shareRole && <ShareButton />}
<ThemeButton />
</Stack>
Expand Down
15 changes: 12 additions & 3 deletions frontend/src/pages/document/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import Preview from "../../components/editor/Preview";
import { Navigate, useParams, useSearchParams } from "react-router-dom";
import { useGetDocumentBySharingTokenQuery, useGetDocumentQuery } from "../../hooks/api/document";
import { AuthContext } from "../../contexts/AuthContext";
import { selectUser } from "../../store/userSlice";

function EditorIndex() {
const dispatch = useDispatch();
const params = useParams();
const userStore = useSelector(selectUser);
const { isLoggedIn } = useContext(AuthContext);
const [searchParams] = useSearchParams();
const windowWidth = useWindowWidth();
Expand All @@ -34,8 +36,9 @@ function EditorIndex() {
let client: yorkie.Client;
let doc: yorkie.Document<YorkieCodeMirrorDocType, YorkieCodeMirrorPresenceType>;
const yorkieDocuentId = document?.yorkieDocumentId || sharedDocument?.yorkieDocumentId;
const name = searchParams.get("token") ? "Anonymous" : userStore.data?.nickname;

if (!yorkieDocuentId) return;
if (!yorkieDocuentId || !name) return;

const initializeYorkie = async () => {
client = new yorkie.Client(import.meta.env.VITE_YORKIE_API_ADDR, {
Expand All @@ -47,7 +50,7 @@ function EditorIndex() {

await client.attach(doc, {
initialPresence: {
name: "Yorkie",
name,
color: Color(randomColor()).fade(0.15).toString(),
selection: null,
},
Expand All @@ -66,7 +69,13 @@ function EditorIndex() {

cleanUp();
};
}, [dispatch, document?.yorkieDocumentId, sharedDocument?.yorkieDocumentId]);
}, [
dispatch,
document?.yorkieDocumentId,
sharedDocument?.yorkieDocumentId,
userStore.data?.nickname,
searchParams,
]);

useEffect(() => {
if (!sharedDocument) return;
Expand Down

0 comments on commit 1b085a9

Please sign in to comment.