|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMemo } from "react"; |
| 4 | +import { useUser, UserButton } from "@clerk/nextjs"; |
| 5 | +import { useOthers } from "@liveblocks/react/suspense"; |
| 6 | +import { ViewportPortal } from "@xyflow/react"; |
| 7 | + |
| 8 | +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; |
| 9 | + |
| 10 | +interface PresenceCursor { |
| 11 | + x: number; |
| 12 | + y: number; |
| 13 | +} |
| 14 | + |
| 15 | +function getInitials(name: string): string { |
| 16 | + return name |
| 17 | + .split(" ") |
| 18 | + .filter(Boolean) |
| 19 | + .slice(0, 2) |
| 20 | + .map((part) => part[0]?.toUpperCase() ?? "") |
| 21 | + .join(""); |
| 22 | +} |
| 23 | + |
| 24 | +export function CanvasPresenceOverlay() { |
| 25 | + const { user } = useUser(); |
| 26 | + const others = useOthers(); |
| 27 | + |
| 28 | + const collaborators = useMemo(() => { |
| 29 | + return others.filter((other) => other.id !== user?.id && other.info.id !== user?.id); |
| 30 | + }, [others, user?.id]); |
| 31 | + |
| 32 | + const visibleCollaborators = collaborators.slice(0, 5); |
| 33 | + const overflowCount = Math.max(0, collaborators.length - visibleCollaborators.length); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="absolute right-3 top-3 z-20 flex items-center rounded-md border border-[var(--border-default)] bg-[var(--bg-base)]/85 px-2 py-1 backdrop-blur-sm"> |
| 37 | + {collaborators.length > 0 ? ( |
| 38 | + <> |
| 39 | + <div className="flex -space-x-2"> |
| 40 | + {visibleCollaborators.map((collaborator) => ( |
| 41 | + <Avatar |
| 42 | + key={collaborator.connectionId} |
| 43 | + className="h-8 w-8 ring-2 ring-background" |
| 44 | + aria-hidden="true" |
| 45 | + > |
| 46 | + {collaborator.info.avatar ? ( |
| 47 | + <AvatarImage src={collaborator.info.avatar} alt={collaborator.info.name} /> |
| 48 | + ) : null} |
| 49 | + <AvatarFallback className="text-xs font-medium"> |
| 50 | + {getInitials(collaborator.info.name)} |
| 51 | + </AvatarFallback> |
| 52 | + </Avatar> |
| 53 | + ))} |
| 54 | + {overflowCount > 0 ? ( |
| 55 | + <div className="flex h-8 w-8 items-center justify-center rounded-full bg-[var(--bg-surface)] text-xs font-semibold text-[var(--text-primary)] ring-2 ring-background"> |
| 56 | + +{overflowCount} |
| 57 | + </div> |
| 58 | + ) : null} |
| 59 | + </div> |
| 60 | + <div className="mx-2 h-6 w-px bg-[var(--border-default)]" /> |
| 61 | + </> |
| 62 | + ) : null} |
| 63 | + <UserButton |
| 64 | + appearance={{ |
| 65 | + elements: { |
| 66 | + userButtonAvatarBox: "h-8 w-8", |
| 67 | + }, |
| 68 | + }} |
| 69 | + /> |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export function LiveCursors() { |
| 75 | + const { user } = useUser(); |
| 76 | + const others = useOthers(); |
| 77 | + |
| 78 | + const participants = useMemo(() => { |
| 79 | + return others.filter((other) => { |
| 80 | + if (other.id === user?.id || other.info.id === user?.id) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + return other.presence.cursor !== null; |
| 84 | + }); |
| 85 | + }, [others, user?.id]); |
| 86 | + |
| 87 | + return ( |
| 88 | + <ViewportPortal> |
| 89 | + {participants.map((participant) => { |
| 90 | + const cursor = participant.presence.cursor as PresenceCursor | null; |
| 91 | + if (!cursor) return null; |
| 92 | + |
| 93 | + const color = participant.info.color; |
| 94 | + return ( |
| 95 | + <div |
| 96 | + key={participant.connectionId} |
| 97 | + className="pointer-events-none absolute select-none" |
| 98 | + style={{ left: cursor.x, top: cursor.y, transform: "translate(-1px, -1px)" }} |
| 99 | + > |
| 100 | + <svg |
| 101 | + width="18" |
| 102 | + height="24" |
| 103 | + viewBox="0 0 18 24" |
| 104 | + fill="none" |
| 105 | + xmlns="http://www.w3.org/2000/svg" |
| 106 | + aria-hidden="true" |
| 107 | + > |
| 108 | + <path |
| 109 | + d="M2 2L2 20L7 15.6L10.2 22L13.1 20.6L9.8 14.1L16 14L2 2Z" |
| 110 | + fill={color} |
| 111 | + stroke="var(--bg-base)" |
| 112 | + strokeWidth="1" |
| 113 | + strokeLinejoin="round" |
| 114 | + /> |
| 115 | + </svg> |
| 116 | + <div |
| 117 | + className="mt-1 inline-flex rounded-sm px-1.5 py-0.5 text-xs font-medium text-[var(--text-primary)]" |
| 118 | + style={{ backgroundColor: color }} |
| 119 | + > |
| 120 | + {participant.info.name} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ); |
| 124 | + })} |
| 125 | + </ViewportPortal> |
| 126 | + ); |
| 127 | +} |
0 commit comments