Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: optimistically show reaction

Marc Seitza3a86eed387b
diff --git a/components/view/toolbar.tsx b/components/view/toolbar.tsx
index 1c4f4833..374a7385 100644
--- a/components/view/toolbar.tsx
+++ b/components/view/toolbar.tsx
@@ -1,7 +1,8 @@
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { Avatar, AvatarFallback } from "../ui/avatar";
import UserRound from "../shared/icons/user-round";
import { REACTIONS } from "@/lib/constants";
+import { set } from "date-fns";
function getKeyByValue(object: { [x: string]: any }, value: any) {
return Object.keys(object).find((key) => object[key] === value);
@@ -14,11 +15,37 @@ export default function Toolbar({
viewId: string;
pageNumber: number;
}) {
- const [flyEmojis, setFlyEmojis] = useState<string>("");
+ const [currentEmoji, setCurrentEmoji] = useState<{
+ emoji: string;
+ id: number;
+ } | null>(null);
+ const clearEmojiTimeout = useRef<any>(null);
+
+ useEffect(() => {
+ setCurrentEmoji(null);
+ }, [pageNumber]);
+
+ useEffect(() => {
+ return () => {
+ if (clearEmojiTimeout.current) {
+ clearTimeout(clearEmojiTimeout.current);
+ }
+ };
+ }, []);
const handleEmojiClick = async (emoji: string) => {
- // First, reset the state to ensure the same emoji can reappear
- setFlyEmojis("");
+ // Clear any existing timeout
+ if (clearEmojiTimeout.current) {
+ clearTimeout(clearEmojiTimeout.current);
+ }
+
+ // Set the current emoji with a unique identifier
+ setCurrentEmoji({ emoji, id: Date.now() });
+
+ // Remove the emoji after the animation duration
+ clearEmojiTimeout.current = setTimeout(() => {
+ setCurrentEmoji(null);
+ }, 3000); // Adjust this duration to match your animation
await fetch("/api/record_reaction", {
method: "POST",
@@ -31,11 +58,6 @@ export default function Toolbar({
pageNumber: pageNumber,
}),
});
-
- // Use setTimeout to allow the state reset to take effect before setting the new emoji
- setTimeout(() => {
- setFlyEmojis(emoji);
- }, 10); // Short delay to allow the state reset to take effect
};
const Emoji = (props: any) => (
@@ -46,7 +68,7 @@ export default function Toolbar({
aria-hidden={props.label ? "false" : "true"}
onClick={() => handleEmojiClick(props.symbol)}
>
- {(props.symbol === "πŸ‘" ||
+ {/* {(props.symbol === "πŸ‘" ||
props.symbol === "πŸ‘Ž" ||
props.symbol === "❀️" ||
props.symbol === "πŸ’Έ") &&
@@ -56,14 +78,22 @@ export default function Toolbar({
>
{flyEmojis}
</span>
- )}
+ )} */}
{props.symbol}
+ {currentEmoji && currentEmoji.emoji === props.symbol && (
+ <span
+ key={currentEmoji.id}
+ className="absolute -top-10 left-0 right-0 mx-auto animate-flyEmoji duration-3000"
+ >
+ {currentEmoji.emoji}
+ </span>
+ )}
</button>
);
return (
<>
- <div className="fixed bottom-10 left-1/2 transform -translate-x-1/2 bg-[#333] rounded-full py-1 px-4 flex items-center justify-start space-x-2 shadow-lg z-10">
+ <div className="fixed bottom-10 left-1/2 transform -translate-x-1/2 bg-gray-950/50 rounded-full py-1 px-4 flex items-center justify-start space-x-2 shadow-lg z-10">
<div className="p-1 rounded-full bg-emerald-500 -ml-2">
<Avatar className="h-6 w-6">
{/* <AvatarImage src= /> */}