-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathshared.tsx
More file actions
373 lines (336 loc) · 13.2 KB
/
shared.tsx
File metadata and controls
373 lines (336 loc) · 13.2 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"use client";
import { Prompt } from "../prompts";
import React from "react";
import * as ContextMenu from "@radix-ui/react-context-menu";
import { useRouter } from "next/navigation";
import { SelectionArea, SelectionEvent } from "@viselect/react";
import copy from "copy-to-clipboard";
import { isTouchDevice } from "../utils/isTouchDevice";
import {
ChevronDownIcon,
CopyClipboardIcon,
DownloadIcon,
Icons,
LinkIcon,
MinusCircleIcon,
PlusCircleIcon,
StarsIcon,
StarsSquareIcon,
} from "@raycast/icons";
import { extractPrompts } from "../utils/extractPrompts";
import { addToRaycast, copyData, downloadData, makeUrl } from "../utils/actions";
import styles from "../[[...slug]]/prompts.module.css";
import { ScrollArea } from "@/components/scroll-area";
import CreativityIcon from "../components/CreativityIcon";
import { ButtonGroup } from "@/components/button-group";
import { Button } from "@/components/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/dropdown-menu";
import { Toast, ToastTitle } from "../components/Toast";
import { Metadata } from "next";
import { NavigationActions } from "@/components/navigation";
import { InfoDialog } from "../components/InfoDialog";
import { Kbd, Kbds } from "@/components/kbd";
import { Extension } from "@/api/store";
import { AIExtension } from "@/components/ai-extension";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/tooltip";
import { renderSafePromptContent } from "@/utils/sanitizePromptContent";
import { FloatingActionBar } from "@/components/floating-action-bar";
export function Shared({ prompts, extensions }: { prompts: Prompt[]; extensions: Extension[] }) {
const router = useRouter();
const [copied, setCopied] = React.useState(false);
const [actionsOpen, setActionsOpen] = React.useState(false);
const [selectedPrompts, setSelectedPrompts] = React.useState([...prompts]);
const isTouch = React.useMemo(() => (typeof window !== "undefined" ? isTouchDevice() : false), []);
const [isClient, setIsClient] = React.useState(false);
React.useEffect(() => {
setIsClient(true);
}, []);
const categories = [
{
name: `${prompts.length} ${prompts.length > 1 ? "prompts" : "prompt"} shared with you`,
isTemplate: true,
isShared: true,
prompts: prompts,
slug: "/shared",
icon: StarsIcon,
},
];
const onStart = ({ event, selection }: SelectionEvent) => {
if (!event?.ctrlKey && !event?.metaKey) {
selection.clearSelection();
setSelectedPrompts([]);
}
};
const onMove = ({
store: {
changed: { added, removed },
},
}: SelectionEvent) => {
const addedPrompts = extractPrompts(added, categories);
const removedPrompts = extractPrompts(removed, categories);
setSelectedPrompts((prevPrompts) => {
const prompts = [...prevPrompts];
addedPrompts.forEach((prompt) => {
if (!prompt) {
return;
}
if (prompts.find((p) => p.id === prompt.id)) {
return;
}
prompts.push(prompt);
});
removedPrompts.forEach((prompt) => {
return prompts.filter((s) => s?.id !== prompt?.id);
});
return prompts;
});
};
const handleDownload = React.useCallback(() => {
downloadData(selectedPrompts);
}, [selectedPrompts]);
const handleCopyData = React.useCallback(() => {
copyData(selectedPrompts);
setCopied(true);
}, [selectedPrompts]);
const handleCopyUrl = React.useCallback(async () => {
const url = makeUrl(selectedPrompts);
let urlToCopy = url;
const encodedUrl = encodeURIComponent(urlToCopy);
const response = await fetch(`https://ray.so/api/shorten-url?url=${encodedUrl}&ref=prompts`).then((res) =>
res.json(),
);
if (response.link) {
urlToCopy = response.link;
}
copy(urlToCopy);
setCopied(true);
}, [selectedPrompts]);
const handleAddToRaycast = React.useCallback(() => addToRaycast(router, selectedPrompts), [router, selectedPrompts]);
const handleCopyText = React.useCallback((prompt: Prompt) => {
copy(prompt.prompt);
setCopied(true);
}, []);
React.useEffect(() => {
const down = (event: KeyboardEvent) => {
const { key, keyCode, metaKey, altKey } = event;
if (key === "k" && metaKey) {
if (selectedPrompts.length === 0) return;
setActionsOpen((prevOpen) => {
return !prevOpen;
});
}
if (key === "d" && metaKey) {
if (selectedPrompts.length === 0) return;
event.preventDefault();
handleDownload();
}
if (key === "Enter" && metaKey) {
if (selectedPrompts.length === 0) return;
event.preventDefault();
handleAddToRaycast();
}
// key === "c" doesn't work when using alt key, so we use keCode instead (67)
if (keyCode === 67 && metaKey && altKey) {
if (selectedPrompts.length === 0) return;
event.preventDefault();
handleCopyData();
setActionsOpen(false);
}
if (key === "a" && metaKey) {
event.preventDefault();
setSelectedPrompts([...prompts]);
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, [prompts, setActionsOpen, selectedPrompts, handleCopyData, handleDownload, handleAddToRaycast]);
React.useEffect(() => {
if (copied) {
setTimeout(() => {
setCopied(false);
}, 2000);
}
}, [copied]);
if (prompts.length === 0) {
return;
}
return (
<div>
<NavigationActions>
<div className="flex gap-2 sm:hidden">
<Button variant="primary" disabled={selectedPrompts.length === 0} onClick={() => handleCopyUrl()}>
<LinkIcon /> Copy URL to Share
</Button>
</div>
<div className="sm:flex gap-2 hidden">
<InfoDialog />
<ButtonGroup>
<Button variant="primary" disabled={selectedPrompts.length === 0} onClick={() => handleAddToRaycast()}>
<PlusCircleIcon /> Add to Raycast
</Button>
<DropdownMenu open={actionsOpen} onOpenChange={setActionsOpen}>
<DropdownMenuTrigger asChild>
<Button variant="primary" disabled={selectedPrompts.length === 0} aria-label="Export options">
<ChevronDownIcon />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem disabled={selectedPrompts.length === 0} onSelect={() => handleDownload()}>
<DownloadIcon /> Download JSON
<Kbds>
<Kbd>⌘</Kbd>
<Kbd>D</Kbd>
</Kbds>
</DropdownMenuItem>
<DropdownMenuItem disabled={selectedPrompts.length === 0} onSelect={() => handleCopyData()}>
<CopyClipboardIcon /> Copy JSON{" "}
<Kbds>
<Kbd>⌘</Kbd>
<Kbd>⌥</Kbd>
<Kbd>C</Kbd>
</Kbds>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</ButtonGroup>
</div>
</NavigationActions>
<Toast open={copied} onOpenChange={setCopied}>
<ToastTitle className={styles.toastTitle}>
<CopyClipboardIcon /> Copied to clipboard
</ToastTitle>
</Toast>
<div className={styles.container}>
{isTouch !== null && (
<SelectionArea
className="container pt-8"
onStart={onStart}
onMove={onMove}
selectables=".selectable"
features={{
// Disable support for touch devices
touch: isTouch ? false : true,
range: true,
singleTap: {
allow: true,
intersect: "native",
},
}}
>
{categories.map((promptGroup) => {
return (
<div key={promptGroup.name} data-section-slug={promptGroup.slug} style={{ outline: "none" }}>
<h2 className={styles.subtitle}>
<promptGroup.icon /> {promptGroup.name}
</h2>
<div className={styles.prompts}>
{promptGroup.prompts.map((prompt, index) => {
const Icon = prompt.icon in Icons ? Icons[prompt.icon] : StarsIcon;
const isSelected = selectedPrompts.some((selectedPrompt) => selectedPrompt.id === prompt.id);
const hasAIExtensions = prompt.prompt.includes("{id=") && prompt.prompt.includes("@");
return (
<ContextMenu.Root key={prompt.id}>
<ContextMenu.Trigger>
<div
className={`${styles.item} selectable`}
key={prompt.id}
data-selected={isSelected}
data-key={`${promptGroup.slug}-${index}`}
>
<div className={styles.promptTemplate}>
<ScrollArea>
<pre className={styles.template}>
{prompt.prompt.split(/(@[a-zA-Z0-9-]+\{id=[^}]+\})/).map((part, index) => {
const match = part.match(/@([a-zA-Z0-9-]+)\{id=([^}]+)\}/);
if (match) {
const extension = extensions.find((e) => e.id === match[2]);
return <AIExtension key={index} extension={extension} fallback={match[1]} />;
}
return (
<span key={index}>{renderSafePromptContent(part, styles.placeholder)}</span>
);
})}
</pre>
</ScrollArea>
</div>
<div className={styles.prompt}>
<span className={styles.name}>
<Icon />
{prompt.title}
</span>
{prompt.creativity ? <CreativityIcon creativity={prompt.creativity} /> : null}
{hasAIExtensions ? (
<Tooltip>
<TooltipTrigger>
<StarsSquareIcon />
</TooltipTrigger>
<TooltipContent>Includes AI Extensions</TooltipContent>
</Tooltip>
) : null}
</div>
</div>
</ContextMenu.Trigger>
<ContextMenu.Portal>
<ContextMenu.Content className={styles.contextMenuContent}>
<ContextMenu.Item
className={styles.contextMenuItem}
onSelect={() => {
if (isSelected) {
return setSelectedPrompts((prevPrompts) =>
prevPrompts.filter((prevPrompt) => prevPrompt.id !== prompt.id),
);
}
setSelectedPrompts((prevPrompts) => [...prevPrompts, prompt]);
}}
>
{isSelected ? <MinusCircleIcon /> : <PlusCircleIcon />}
{isSelected ? "Deselect Prompt" : "Select Prompt"}
</ContextMenu.Item>
<ContextMenu.Item
className={styles.contextMenuItem}
onSelect={() => handleCopyText(prompt)}
>
<CopyClipboardIcon /> Copy Prompt Text{" "}
</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Portal>
</ContextMenu.Root>
);
})}
</div>
</div>
);
})}
</SelectionArea>
)}
</div>
<FloatingActionBar
isVisible={isTouch === true && selectedPrompts.length > 0}
actions={[
{
icon: <PlusCircleIcon />,
label: "Add to Raycast",
onClick: handleAddToRaycast,
variant: "primary",
},
{
icon: <CopyClipboardIcon />,
label: "Copy JSON",
onClick: handleCopyData,
},
{
icon: <LinkIcon />,
label: "Share URL",
onClick: handleCopyUrl,
},
]}
/>
</div>
);
}