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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
"use client";
import Image from "next/image";
import type { CSSProperties } from "react";
import { useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
import { PanelView } from "@/components/editor/panels/assets/views/base-view";
import { DraggableItem } from "@/components/editor/panels/assets/draggable-item";
import { Button } from "@/components/ui/button";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import {
resolveStickerId,
type StickerItem as StickerData,
} from "@/lib/stickers";
import { useStickersStore } from "@/stores/stickers-store";
import { cn } from "@/utils/ui";
import {
HappyIcon,
ClockIcon,
MultiplicationSignIcon,
Search01Icon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { Spinner } from "@/components/ui/spinner";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { OcSlidersVerticalIcon } from "@opencut/ui/icons";
import type { StickerCategory } from "@/types/stickers";
import { STICKER_CATEGORIES } from "@/constants/sticker-constants";
import { parseStickerId } from "@/lib/stickers/sticker-id";
export function StickersView() {
const { selectedCategory, setSelectedCategory } = useStickersStore();
return (
<PanelView
title="Stickers"
actions={
<div className="flex items-center">
<Select
value={selectedCategory}
onValueChange={(value: StickerCategory) =>
setSelectedCategory({ category: value })
}
>
<SelectTrigger variant="outline" size="sm" className="mr-1.5">
<SelectValue placeholder="All" />
</SelectTrigger>
<SelectContent>
{Object.entries(STICKER_CATEGORIES).map(([category, label]) => (
<SelectItem key={category} value={category}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
<Button variant="ghost" size="icon">
<HugeiconsIcon icon={Search01Icon} className="!size-3.5" />
</Button>
<Button variant="ghost" size="icon">
<OcSlidersVerticalIcon className="!size-3.5" />
</Button>
</div>
}
>
<StickersContentView />
</PanelView>
);
}
function StickerGrid({
items,
shouldCapSize = false,
}: {
items: StickerData[];
shouldCapSize?: boolean;
}) {
const gridStyle: CSSProperties & {
"--sticker-min": string;
"--sticker-max"?: string;
} = {
gridTemplateColumns: shouldCapSize
? "repeat(auto-fill, minmax(var(--sticker-min, 96px), var(--sticker-max, 160px)))"
: "repeat(auto-fit, minmax(var(--sticker-min, 96px), 1fr))",
"--sticker-min": "96px",
...(shouldCapSize ? { "--sticker-max": "160px" } : {}),
};
return (
<div className="grid gap-2" style={gridStyle}>
{items.map((item) => (
<StickerItem key={item.id} item={item} shouldCapSize={shouldCapSize} />
))}
</div>
);
}
function EmptyView({ message }: { message: string }) {
return (
<div className="bg-background flex h-full flex-col items-center justify-center gap-3 p-4">
<HugeiconsIcon
icon={HappyIcon}
className="text-muted-foreground size-10"
/>
<div className="flex flex-col gap-2 text-center">
<p className="text-lg font-medium">No stickers found</p>
<p className="text-muted-foreground text-sm text-balance">{message}</p>
</div>
</div>
);
}
function StickersContentView() {
const {
searchQuery,
viewMode,
searchResults,
recentStickers,
isSearching,
clearRecentStickers,
} = useStickersStore();
const itemsToDisplay = useMemo(() => {
if (viewMode === "search" && searchResults) {
return searchResults.items;
}
return [];
}, [viewMode, searchResults]);
const recentStickerItems = useMemo(() => {
const items: StickerData[] = [];
for (const stickerId of recentStickers) {
const recentStickerItem = toRecentStickerItem({ stickerId });
if (recentStickerItem) {
items.push(recentStickerItem);
}
}
return items;
}, [recentStickers]);
return (
<div className="flex h-full flex-col gap-4">
{recentStickerItems.length > 0 && viewMode === "browse" && (
<div className="flex h-full flex-col gap-2">
<div className="flex items-center gap-2">
<HugeiconsIcon
icon={ClockIcon}
className="text-muted-foreground size-4"
/>
<span className="text-sm font-medium">Recent</span>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={clearRecentStickers}
className="hover:bg-accent ml-auto flex size-5 items-center justify-center rounded p-0"
>
<HugeiconsIcon
icon={MultiplicationSignIcon}
className="text-muted-foreground size-3"
/>
</button>
</TooltipTrigger>
<TooltipContent>
<p>Clear recent stickers</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
<StickerGrid items={recentStickerItems.slice(0, 12)} shouldCapSize />
</div>
)}
{viewMode === "search" && (
<div className="h-full">
{isSearching ? (
<div className="flex items-center justify-center py-8">
<Spinner className="text-muted-foreground size-6" />
</div>
) : searchResults?.items.length ? (
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between">
<span className="text-muted-foreground text-sm">
{searchResults.total} results
</span>
</div>
<StickerGrid items={itemsToDisplay} shouldCapSize />
</div>
) : searchQuery ? (
<EmptyView message={`No stickers found for "${searchQuery}"`} />
) : null}
</div>
)}
</div>
);
}
interface StickerItemProps {
item: StickerData;
shouldCapSize?: boolean;
}
function StickerItem({ item, shouldCapSize = false }: StickerItemProps) {
const { addingSticker, addStickerToTimeline } = useStickersStore();
const isAdding = addingSticker === item.id;
const [hasImageError, setHasImageError] = useState(false);
useEffect(() => {
if (!item.id) {
return;
}
setHasImageError(false);
}, [item.id]);
const displayName = item.name;
const handleAdd = async () => {
try {
await addStickerToTimeline({
stickerId: item.id,
name: item.name,
});
} catch (error) {
console.error("Failed to add sticker:", error);
toast.error("Failed to add sticker to timeline");
}
};
const preview = hasImageError ? (
<div className="flex size-full items-center justify-center p-2">
<span className="text-muted-foreground text-center text-xs break-all">
{displayName}
</span>
</div>
) : (
<div className="flex size-full items-center justify-center p-4">
<Image
src={item.previewUrl}
alt={displayName}
width={64}
height={64}
className="size-full object-contain"
style={
shouldCapSize
? {
maxWidth: "var(--sticker-max, 160px)",
maxHeight: "var(--sticker-max, 160px)",
}
: undefined
}
onError={() => {
setHasImageError(true);
}}
loading="lazy"
unoptimized
/>
</div>
);
return (
<div
className={cn("relative", isAdding && "pointer-events-none opacity-50")}
>
<DraggableItem
name={displayName}
preview={preview}
dragData={{
id: item.id,
type: "sticker",
name: displayName,
stickerId: item.id,
}}
onAddToTimeline={handleAdd}
aspectRatio={1}
shouldShowLabel={false}
isRounded
variant="card"
containerClassName="w-full"
/>
{isAdding && (
<div className="absolute inset-0 z-10 flex items-center justify-center rounded-md bg-black/60">
<Spinner className="size-6 text-white" />
</div>
)}
</div>
);
}
function getStickerNameFromId({ stickerId }: { stickerId: string }): string {
const stickerIdParts = stickerId.split(":");
if (stickerIdParts.length <= 1) {
return stickerId;
}
return (
stickerIdParts.slice(1).join(":").split(":").pop()?.replaceAll("-", " ") ??
stickerId
);
}
function toRecentStickerItem({
stickerId,
}: {
stickerId: string;
}): StickerData | null {
try {
const { providerId } = parseStickerId({ stickerId });
return {
id: stickerId,
provider: providerId,
name: getStickerNameFromId({ stickerId }),
previewUrl: resolveStickerId({
stickerId,
options: { width: 64, height: 64 },
}),
metadata: {},
};
} catch {
return null;
}
}
|