Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message reaction display. #156

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 49 additions & 32 deletions www/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ const URL = process.env.NEXT_PUBLIC_API_URL;
export default function Home() {
const [userId, setUserId] = useState<string>();

const [isThoughtsOpen, setIsThoughtsOpen] = useState<boolean>(false);
const [isThoughtsOpenState, setIsThoughtsOpenState] =
useState<boolean>(false);
const [openThoughtMessageId, setOpenThoughtMessageId] = useState<
string | null
>(null);
const [isSidebarOpen, setIsSidebarOpen] = useState<boolean>(false);

const [thought, setThought] = useState<string>('');
Expand All @@ -57,6 +61,14 @@ export default function Home() {

const [isSubscribed, setIsSubscribed] = useState(false);

const setIsThoughtsOpen = (
isOpen: boolean,
messageId: string | null = null
) => {
setIsThoughtsOpenState(isOpen);
setOpenThoughtMessageId(isOpen ? messageId : null);
};

useEffect(() => {
(async () => {
const {
Expand Down Expand Up @@ -112,17 +124,17 @@ export default function Home() {
return api.getConversations();
};

const {
data: conversations,
mutate: mutateConversations,
error,
} = useSWR(userId, conversationsFetcher, {
onSuccess: (conversations) => {
setConversationId(conversations[0].conversationId);
setCanSend(true);
},
revalidateOnFocus: false,
});
const { data: conversations, mutate: mutateConversations } = useSWR(
userId,
conversationsFetcher,
{
onSuccess: (conversations) => {
setConversationId(conversations[0].conversationId);
setCanSend(true);
},
revalidateOnFocus: false,
}
);

const messagesFetcher = async (conversationId: string) => {
if (!userId) return Promise.resolve([]);
Expand All @@ -136,7 +148,6 @@ export default function Home() {
data: messages,
mutate: mutateMessages,
isLoading: messagesLoading,
error: _,
} = useSWR(conversationId, messagesFetcher, { revalidateOnFocus: false });

const handleReactionAdded = async (messageId: string, reaction: Reaction) => {
Expand All @@ -148,21 +159,24 @@ export default function Home() {
await api.addOrRemoveReaction(conversationId, messageId, reaction);

// Optimistically update the local data
mutateMessages((currentMessages) => {
if (!currentMessages) return currentMessages;
return currentMessages.map((msg) => {
if (msg.id === messageId) {
return {
...msg,
metadata: {
...msg.metadata,
reaction,
},
};
}
return msg;
});
}, true);
mutateMessages(
(currentMessages) => {
if (!currentMessages) return currentMessages;
return currentMessages.map((msg) => {
if (msg.id === messageId) {
return {
...msg,
metadata: {
...msg.metadata,
reaction,
},
};
}
return msg;
});
},
{ revalidate: false }
);
} catch (error) {
console.error('Failed to update reaction:', error);
}
Expand Down Expand Up @@ -248,7 +262,7 @@ export default function Home() {

mutateMessages(
[
...newMessages?.slice(0, -1)!,
...(newMessages?.slice(0, -1) || []),
{
text: currentModelOutput,
isUser: false,
Expand Down Expand Up @@ -336,7 +350,10 @@ export default function Home() {
loading={messagesLoading}
conversationId={conversationId}
setThought={setThought}
setIsThoughtsOpen={setIsThoughtsOpen}
isThoughtOpen={openThoughtMessageId === message.id}
setIsThoughtsOpen={(isOpen) =>
setIsThoughtsOpen(isOpen, message.id)
}
onReactionAdded={handleReactionAdded}
/>
)) || (
Expand Down Expand Up @@ -403,8 +420,8 @@ export default function Home() {
</div>
<Thoughts
thought={thought}
setIsThoughtsOpen={setIsThoughtsOpen}
isThoughtsOpen={isThoughtsOpen}
setIsThoughtsOpen={(isOpen: boolean) => setIsThoughtsOpen(isOpen, null)}
isThoughtsOpen={isThoughtsOpenState}
/>
</main>
);
Expand Down
20 changes: 13 additions & 7 deletions www/components/messagebox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface MessageBoxProps {
conversationId?: string;
message: Message;
loading?: boolean;
isThoughtsOpen?: boolean;
isThoughtOpen?: boolean;
setIsThoughtsOpen: (isOpen: boolean) => void;
setThought: (thought: string) => void;
onReactionAdded: (messageId: string, reaction: Reaction) => Promise<void>;
Expand All @@ -29,6 +29,7 @@ export default function MessageBox({
URL,
message,
loading = false,
isThoughtOpen,
setIsThoughtsOpen,
conversationId,
onReactionAdded,
Expand All @@ -49,10 +50,7 @@ export default function MessageBox({

try {
const reactionToSend = reaction === newReaction ? null : newReaction;
await onReactionAdded(
messageId,
reactionToSend as Exclude<Reaction, null>
);
await onReactionAdded(messageId, reactionToSend as Reaction);
} catch (err) {
console.error(err);
setError('Failed to update reaction.');
Expand All @@ -63,7 +61,11 @@ export default function MessageBox({

const handleFetchThought = async () => {
if (!messageId || !conversationId || !userId || !URL) return;

if (isThoughtOpen) {
// If thought is already open, close it
setIsThoughtsOpen(false);
return;
}
setIsThoughtLoading(true);
setError(null);

Expand Down Expand Up @@ -140,7 +142,11 @@ export default function MessageBox({
</div>
</button>
<button
className="p-2 rounded-full bg-gray-200 dark:bg-gray-700"
className={`p-2 rounded-full ${
isThoughtOpen
? 'bg-neon-green text-gray-800'
: 'bg-gray-200 dark:bg-gray-700'
}`}
onClick={handleFetchThought}
disabled={isThoughtLoading}
>
Expand Down
2 changes: 2 additions & 0 deletions www/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ export class API {
if (!rawMessages) return [];
const messages: Message[] = rawMessages.map((rawMessage: any) => {
return {
...rawMessage,
text: rawMessage.content,
isUser: rawMessage.isUser,
id: rawMessage.id,
metadata: rawMessage.metadata,
};
});

Expand Down
Loading