Skip to content
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
54 changes: 35 additions & 19 deletions src/components/WishlistSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useStreamClient } from "@/hooks/useStreamClient";
import { useEffect, useState, useRef } from "react";
import ReactMarkdown from "react-markdown";
import ReactMarkdown from 'react-markdown';

export default function WishlistSummary() {
const [fullText, setFullText] = useState<string>("");
Expand All @@ -11,6 +11,7 @@ export default function WishlistSummary() {
const { isStreaming, streamClient } = useStreamClient<string>();
const hasStreamed = useRef(false);
const typewriterInterval = useRef<NodeJS.Timeout | null>(null);
const [isRequested, setIsRequested] = useState(false);

useEffect(() => {
if (!isLoading) return;
Expand Down Expand Up @@ -60,10 +61,11 @@ export default function WishlistSummary() {
}, [fullText, isLoading, displayedText]);

useEffect(() => {
if (hasStreamed.current) return;
hasStreamed.current = true;
if (!isRequested || hasStreamed.current) return;

const startStream = async () => {
hasStreamed.current = true;

try {
await streamClient.stream("/api/wishlists/summary", {
onChunk: (chunk) => {
Expand All @@ -76,12 +78,16 @@ export default function WishlistSummary() {
console.error("Streaming error:", error);
setFullText("Unable to load summary.");
setIsLoading(false);
hasStreamed.current = false;
setIsRequested(false);
},
});
} catch (error) {
console.error("Failed to start stream:", error);
setFullText("Unable to load summary.");
setIsLoading(false);
hasStreamed.current = false;
setIsRequested(false);
}
};

Expand All @@ -90,29 +96,39 @@ export default function WishlistSummary() {
return () => {
streamClient.cancel();
};
}, [streamClient]);

}, [streamClient, isRequested]);

const showContainer = displayedText.length > 0;
const showCursor = isLoading || isStreaming || displayedText.length < fullText.length;

return (
<div
className={`mt-8 mb-5 ${
showContainer
? "min-h-4 p-4 rounded border border-blue-primary bg-blue-light"
: "h-0"
} transition-all duration-200 text-md text-black`}
>
{displayedText.split("\n").map((line, index, array) => (
<div key={index} className={line.trim() === "" ? "h-4" : "mb-2 last:mb-0 inline-block w-full"}>
<ReactMarkdown components={{ p: "span" }}>
{line}
<div className="w-full">
{!isRequested && (
<div className="flex justify-end -mb-12 mt-8 mr-28">
<button
onClick={() => setIsRequested(true)}
className="flex items-center px-4 py-2 bg-blue-primary text-white rounded-md font-medium hover:bg-blue-600 transition-colors shadow-sm"
>
Generate Summary
</button>
</div>
)}
Comment thread
ND68 marked this conversation as resolved.

<div
className={`mt-4 mb-5 ${
isRequested && showContainer
? "min-h-4 p-4 rounded border border-blue-primary bg-blue-light"
: "hidden"
} transition-all duration-200`}
>
<div className="prose inline-block align-top-markdown-content">
Comment thread
ND68 marked this conversation as resolved.
<ReactMarkdown>
{displayedText}
</ReactMarkdown>
{showCursor && index === array.length - 1 && (
<span className="animate-pulse">|</span>
)}
</div>
))}
{showCursor && <span className="animate-pulse">|</span>}
</div>
</div>
);
}
1 change: 0 additions & 1 deletion src/screens/Wishlist/AdminWishlistScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default function AdminWishlistScreen() {
<div className="pb-32">
<h1 className="text-2xl font-bold text-gray-primary">Wishlists</h1>
<WishlistSummary />

<AdvancedBaseTable
columns={[
{
Expand Down
Loading