From 731d2dfbea39aa140ccb6d2d2cabd49186320299 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 27 Oct 2024 00:12:11 +0000 Subject: feature: Add a summarize with AI button for links --- .../dashboard/bookmarks/SummarizeBookmarkArea.tsx | 151 +++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 apps/web/components/dashboard/bookmarks/SummarizeBookmarkArea.tsx (limited to 'apps/web/components/dashboard/bookmarks') diff --git a/apps/web/components/dashboard/bookmarks/SummarizeBookmarkArea.tsx b/apps/web/components/dashboard/bookmarks/SummarizeBookmarkArea.tsx new file mode 100644 index 00000000..c1f3a18a --- /dev/null +++ b/apps/web/components/dashboard/bookmarks/SummarizeBookmarkArea.tsx @@ -0,0 +1,151 @@ +import React from "react"; +import { ActionButton } from "@/components/ui/action-button"; +import { Button } from "@/components/ui/button"; +import LoadingSpinner from "@/components/ui/spinner"; +import { toast } from "@/components/ui/use-toast"; +import { cn } from "@/lib/utils"; +import { + ChevronDown, + ChevronUp, + Loader2, + RefreshCw, + Trash2, +} from "lucide-react"; + +import { + useSummarizeBookmark, + useUpdateBookmark, +} from "@hoarder/shared-react/hooks/bookmarks"; +import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks"; + +function AISummary({ + bookmarkId, + summary, +}: { + bookmarkId: string; + summary: string; +}) { + const [isExpanded, setIsExpanded] = React.useState(false); + const { mutate: resummarize, isPending: isResummarizing } = + useSummarizeBookmark({ + onError: () => { + toast({ + description: "Something went wrong", + variant: "destructive", + }); + }, + }); + const { mutate: updateBookmark, isPending: isUpdatingBookmark } = + useUpdateBookmark({ + onError: () => { + toast({ + description: "Something went wrong", + variant: "destructive", + }); + }, + }); + return ( +
+ {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */} +
!isExpanded && setIsExpanded(true)} + > +
+

+ {summary} +

+ {!isExpanded && ( +
+ )} + + {isExpanded && ( + <> + } + className="rounded-full bg-gray-200 p-1 text-gray-600 dark:bg-gray-700 dark:text-gray-400" + aria-label={isExpanded ? "Collapse" : "Expand"} + loading={isResummarizing} + onClick={() => resummarize({ bookmarkId })} + > + + + } + className="rounded-full bg-gray-200 p-1 text-gray-600 dark:bg-gray-700 dark:text-gray-400" + aria-label={isExpanded ? "Collapse" : "Expand"} + loading={isUpdatingBookmark} + onClick={() => updateBookmark({ bookmarkId, summary: null })} + > + + + + )} + + +
+
+
+ ); +} + +export default function SummarizeBookmarkArea({ + bookmark, +}: { + bookmark: ZBookmark; +}) { + const { mutate, isPending } = useSummarizeBookmark({ + onError: () => { + toast({ + description: "Something went wrong", + variant: "destructive", + }); + }, + }); + + if (bookmark.content.type !== BookmarkTypes.LINK) { + return null; + } + + if (bookmark.summary) { + return ; + } else { + return ( +
+ +
+ ); + } +} -- cgit v1.2.3-70-g09d2