1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
import React from "react";
import { ActionButton } from "@/components/ui/action-button";
import { MarkdownReadonly } from "@/components/ui/markdown/markdown-readonly";
import LoadingSpinner from "@/components/ui/spinner";
import { toast } from "@/components/ui/use-toast";
import { useClientConfig } from "@/lib/clientConfig";
import { useTranslation } from "@/lib/i18n/client";
import { cn } from "@/lib/utils";
import { ChevronUp, RefreshCw, Sparkles, Trash2 } from "lucide-react";
import {
useSummarizeBookmark,
useUpdateBookmark,
} from "@karakeep/shared-react/hooks/bookmarks";
import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";
function AISummary({
bookmarkId,
summary,
readOnly = false,
}: {
bookmarkId: string;
summary: string;
readOnly?: boolean;
}) {
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 (
<div className="w-full p-1">
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div
className={`
relative overflow-hidden rounded-lg p-4
transition-all duration-300 ease-in-out
${isExpanded ? "h-auto" : "cursor-pointer"}
bg-gradient-to-r from-purple-400 via-pink-500 to-red-500 p-[2px]
`}
onClick={() => !isExpanded && setIsExpanded(true)}
>
<div className="h-full rounded-lg bg-accent p-2">
<MarkdownReadonly
className={`text-sm ${!isExpanded && "line-clamp-3"}`}
>
{summary}
</MarkdownReadonly>
{isExpanded && (
<span className="flex justify-end gap-2 pt-2">
{!readOnly && (
<>
<ActionButton
variant="none"
size="none"
spinner={<LoadingSpinner className="size-4" />}
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 })}
>
<RefreshCw size={16} />
</ActionButton>
<ActionButton
size="none"
variant="none"
spinner={<LoadingSpinner className="size-4" />}
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 })
}
>
<Trash2 size={16} />
</ActionButton>
</>
)}
<button
className="rounded-full bg-gray-200 p-1 text-gray-600 dark:bg-gray-700 dark:text-gray-400"
aria-label="Collapse"
onClick={() => setIsExpanded(false)}
>
<ChevronUp size={16} />
</button>
</span>
)}
</div>
</div>
</div>
);
}
export default function SummarizeBookmarkArea({
bookmark,
readOnly = false,
}: {
bookmark: ZBookmark;
readOnly?: boolean;
}) {
const { t } = useTranslation();
const { mutate, isPending } = useSummarizeBookmark({
onError: () => {
toast({
description: "Something went wrong",
variant: "destructive",
});
},
});
const clientConfig = useClientConfig();
if (bookmark.content.type !== BookmarkTypes.LINK) {
return null;
}
if (bookmark.summary) {
return (
<AISummary
bookmarkId={bookmark.id}
summary={bookmark.summary}
readOnly={readOnly}
/>
);
} else if (!clientConfig.inference.isConfigured || readOnly) {
return null;
} else {
return (
<div className="flex w-full items-center gap-4">
<ActionButton
onClick={() => mutate({ bookmarkId: bookmark.id })}
className={cn(
`relative w-full overflow-hidden bg-opacity-30 bg-gradient-to-r from-blue-400 via-purple-500 to-pink-500 text-gray-50 transition-all duration-300`,
)}
loading={isPending}
>
{isPending && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="h-full w-full bg-gradient-to-r from-blue-400 via-purple-500 to-pink-500"></div>
<LoadingSpinner className="absolute h-5 w-5 text-white" />
</div>
)}
<span className="relative z-10 flex items-center gap-1.5">
{t("actions.summarize_with_ai")}
<Sparkles className="size-4" />
</span>
</ActionButton>
</div>
);
}
}
|