-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathsubmission-fields.tsx
More file actions
464 lines (435 loc) · 14.4 KB
/
Copy pathsubmission-fields.tsx
File metadata and controls
464 lines (435 loc) · 14.4 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
"use client";
import {
BOUNTY_MAX_SUBMISSION_DESCRIPTION_LENGTH,
BOUNTY_MAX_SUBMISSION_FILES,
BOUNTY_MAX_SUBMISSION_URLS,
} from "@/lib/bounty/constants";
import { resolveBountyDetails } from "@/lib/bounty/utils";
import { PartnerBountyProps, PartnerPlatformProps } from "@/lib/types";
import { evaluateSocialContentRequirements } from "@/ui/partners/bounties/evaluate-social-content-requirements";
import { X } from "@/ui/shared/icons";
import {
Button,
CircleCheck,
FileUpload,
Label,
LoadingSpinner,
Trash,
} from "@dub/ui";
import { cn, formatDate } from "@dub/utils";
import { AlertTriangle } from "lucide-react";
import { useEffect, useId, useState } from "react";
import ReactTextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
import { v4 as uuid } from "uuid";
import { useEmbedSocialContent } from "./use-embed-social-content";
const inputClassName =
"block h-10 w-full rounded-md px-3 py-2 sm:text-sm " +
"border-border-default bg-bg-default text-content-emphasis " +
"placeholder:text-content-muted " +
"focus:border-border-emphasis focus:outline-none focus:ring-border-emphasis";
export interface FileInput {
id: string;
file?: File;
url?: string;
uploading: boolean;
originalFileName?: string;
originalFileSize?: number;
}
export function EmbedImagesField({
bounty,
files,
setFiles,
onUploadingChange,
token,
}: {
bounty: PartnerBountyProps;
files: FileInput[];
setFiles: React.Dispatch<React.SetStateAction<FileInput[]>>;
onUploadingChange: (uploading: boolean) => void;
token: string;
}) {
const imageMax = bounty.submissionRequirements?.image?.max;
const maxFiles = imageMax ?? BOUNTY_MAX_SUBMISSION_FILES;
const formatRequirementText = (max?: number | null) =>
max != null && max > 1 ? ` (1 required, max of ${max})` : " (1 required)";
const handleUpload = async (file: File) => {
const newFile: FileInput = { id: uuid(), file, uploading: true };
setFiles((prev) => {
const updated = [...prev, newFile];
onUploadingChange(true);
return updated;
});
try {
const uploadUrlRes = await fetch(
`/api/embed/referrals/bounties/${bounty.id}/upload`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
fileName: file.name,
contentType: file.type,
contentLength: file.size,
}),
},
);
if (!uploadUrlRes.ok) {
toast.error("Failed to get signed upload URL.");
setFiles((prev) => {
const updated = prev.filter((f) => f.id !== newFile.id);
onUploadingChange(updated.some((f) => f.uploading));
return updated;
});
return;
}
const { signedUrl, destinationUrl } = await uploadUrlRes.json();
const uploadResponse = await fetch(signedUrl, {
method: "PUT",
body: file,
headers: {
"Content-Type": file.type,
"Content-Length": file.size.toString(),
},
});
if (!uploadResponse.ok) {
toast.error("Failed to upload screenshot.");
setFiles((prev) => {
const updated = prev.filter((f) => f.id !== newFile.id);
onUploadingChange(updated.some((f) => f.uploading));
return updated;
});
return;
}
toast.success(`${file.name} uploaded!`);
setFiles((prev) => {
const updated = prev.map((f) =>
f.id === newFile.id
? { ...f, uploading: false, url: destinationUrl }
: f,
);
onUploadingChange(updated.some((f) => f.uploading));
return updated;
});
} catch {
toast.error(
"An unexpected error occurred while uploading. Please try again.",
);
setFiles((prev) => {
const updated = prev.filter((f) => f.id !== newFile.id);
onUploadingChange(updated.some((f) => f.uploading));
return updated;
});
}
};
return (
<div>
<Label>Images{formatRequirementText(imageMax)}</Label>
<div
className={cn(
"mt-2 flex h-12 items-center gap-2 transition-[height]",
files.length === 0 && "h-[104px]",
)}
>
{files.map((file, idx) => (
<div
key={file.id}
className="border-border-subtle bg-bg-default group relative flex aspect-square h-full items-center justify-center rounded-md border"
>
{file.uploading ? (
<LoadingSpinner className="size-4" />
) : (
<div className="relative size-full overflow-hidden rounded-md">
<img
src={file.url}
alt={file.file?.name || `Bounty attachment ${idx + 1}`}
/>
</div>
)}
<span className="sr-only">
{file.file?.name || `File ${idx + 1}`}
</span>
<button
type="button"
aria-label={`Remove ${file.file?.name ?? file.originalFileName ?? "file"}`}
className={cn(
"absolute right-0 top-0 flex size-[1.125rem] -translate-y-1/2 translate-x-1/2 items-center justify-center",
"border-border-subtle bg-bg-default hover:bg-bg-muted rounded-full border shadow-sm active:scale-95",
"scale-50 opacity-0 transition-[background-color,transform,opacity] group-hover:scale-100 group-hover:opacity-100",
)}
onClick={() => {
setFiles((prev) => {
const next = prev.filter((f) => f.id !== file.id);
onUploadingChange(next.some((f) => f.uploading));
return next;
});
}}
>
<X className="text-content-muted size-2.5" />
</button>
</div>
))}
<FileUpload
accept="images"
className={cn(
"border-border-subtle h-full w-auto rounded-md border",
files.length > 0 ? "aspect-square" : "aspect-[unset] w-full",
)}
iconClassName="size-5 shrink-0"
variant="plain"
content={
files.length > 0 ? null : "SVG, JPG, PNG or WEBP\nMax size 5MB"
}
onChange={async ({ file }) => await handleUpload(file)}
disabled={files.length >= maxFiles}
maxFileSizeMB={5}
/>
</div>
</div>
);
}
export function EmbedSocialUrlField({
bounty,
value,
onChange,
partnerPlatform,
onVerifyingChange,
onRequirementsMetChange,
}: {
bounty: PartnerBountyProps;
value: string;
onChange: (v: string) => void;
partnerPlatform?: Pick<PartnerPlatformProps, "identifier" | "verifiedAt">;
onVerifyingChange: (value: boolean) => void;
onRequirementsMetChange: (value: boolean) => void;
}) {
const bountyInfo = resolveBountyDetails(bounty);
const socialPlatform = bountyInfo?.socialPlatform;
const inputId = useId();
const [urlToCheck, setUrlToCheck] = useState("");
useEffect(() => {
setUrlToCheck("");
onRequirementsMetChange(false);
}, [value, onRequirementsMetChange]);
const { data, error, isValidating } = useEmbedSocialContent({
bountyId: bounty.id,
url: urlToCheck,
});
useEffect(() => {
onVerifyingChange(isValidating);
return () => onVerifyingChange(false);
}, [isValidating, onVerifyingChange]);
if (!socialPlatform) return null;
const { isPostedFromYourAccount, isAfterStartDate } =
evaluateSocialContentRequirements({
content: data,
bounty: {
startsAt: new Date(bounty.startsAt),
},
partnerPlatform: {
identifier: partnerPlatform?.identifier ?? "",
verifiedAt: partnerPlatform?.verifiedAt
? new Date(partnerPlatform.verifiedAt)
: null,
},
});
useEffect(() => {
onRequirementsMetChange(isPostedFromYourAccount && isAfterStartDate);
return () => onRequirementsMetChange(true);
}, [isAfterStartDate, isPostedFromYourAccount, onRequirementsMetChange]);
const showIcon = isValidating || (!!error && !!urlToCheck);
return (
<div>
<label htmlFor={inputId} className="block">
<span className="text-content-emphasis text-sm font-medium">
{`${socialPlatform.label} URL`}
</span>
</label>
<div className="relative mt-2">
<input
id={inputId}
type="text"
inputMode="url"
autoComplete="url"
placeholder={socialPlatform.placeholder}
value={value}
onChange={(e) => onChange(e.target.value)}
onBlur={(e) => {
const trimmed = e.target.value.trim();
onChange(trimmed);
setUrlToCheck(trimmed);
}}
className={cn(
`${inputClassName} pr-10`,
error &&
urlToCheck &&
"border-border-error focus:border-border-error focus:ring-border-error",
)}
/>
{showIcon && (
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
{isValidating ? (
<LoadingSpinner className="text-content-muted size-4 shrink-0" />
) : error && urlToCheck ? (
<AlertTriangle
className="text-content-error size-4 shrink-0"
fill="rgb(var(--content-error))"
/>
) : null}
</div>
)}
</div>
<ul className="mt-2 flex flex-wrap items-center gap-3">
<li
className={cn(
"flex items-center gap-1 text-xs font-medium transition-colors",
isPostedFromYourAccount
? "text-content-success"
: "text-content-muted",
)}
>
<CircleCheck
variant="fill"
className={cn(
"size-2.5 transition-opacity",
isPostedFromYourAccount
? "text-content-success"
: "text-content-muted",
)}
/>
<span>Posted from your account</span>
</li>
{bounty.startsAt && (
<li
className={cn(
"flex items-center gap-1 text-xs font-medium transition-colors",
isAfterStartDate ? "text-content-success" : "text-content-muted",
)}
>
<CircleCheck
variant="fill"
className={cn(
"size-2.5 transition-opacity",
isAfterStartDate
? "text-content-success"
: "text-content-muted",
)}
/>
<span>{`Posted after ${formatDate(bounty.startsAt, { month: "short", day: "numeric", year: "numeric" })}`}</span>
</li>
)}
</ul>
</div>
);
}
export function EmbedUrlsField({
bounty,
urls,
setUrls,
}: {
bounty: PartnerBountyProps;
urls: string[];
setUrls: React.Dispatch<React.SetStateAction<string[]>>;
}) {
const urlMax = bounty.submissionRequirements?.url?.max;
const maxUrls = urlMax ?? BOUNTY_MAX_SUBMISSION_URLS;
const formatRequirementText = (max?: number | null) =>
max != null && max > 1 ? ` (1 required, max of ${max})` : " (1 required)";
const firstDomain = bounty.submissionRequirements?.url?.domains?.[0];
const placeholderUrl = firstDomain ? `https://${firstDomain}` : "https://";
const rows = urls.length > 0 ? urls : [""];
return (
<div>
<div className="flex items-center justify-between">
<Label>URL{formatRequirementText(urlMax)}</Label>
<span className="text-content-subtle text-xs font-medium">
{urls.filter(Boolean).length} / {maxUrls}
</span>
</div>
<div className="mt-2 flex flex-col gap-2">
{rows.map((url, i) => (
<div key={i} className="flex items-center gap-2">
<input
type="url"
placeholder={placeholderUrl}
value={url}
onChange={(e) => {
setUrls((prev) => {
const next =
prev.length > i
? [...prev]
: [...prev, ...Array(i - prev.length + 1).fill("")];
next[i] = e.target.value;
return next;
});
}}
className={inputClassName}
/>
<Button
variant="outline"
icon={<Trash className="size-4" />}
aria-label={`Remove URL ${i + 1}`}
className="bg-bg-error text-content-error hover:bg-bg-error/80 w-10 shrink-0 p-0"
onClick={() => setUrls((prev) => prev.filter((_, j) => j !== i))}
/>
</div>
))}
{urls.length < maxUrls && (
<Button
variant="secondary"
text="Add URL"
className="h-8 rounded-lg"
onClick={() => setUrls((prev) => [...prev, ""])}
/>
)}
{bounty.submissionRequirements?.url?.domains &&
bounty.submissionRequirements.url.domains.length > 0 && (
<p className="text-content-muted text-xs">
Allowed domains:{" "}
{bounty.submissionRequirements.url.domains.join(", ")}
</p>
)}
</div>
</div>
);
}
export function EmbedDescriptionField({
value,
onChange,
}: {
value: string;
onChange: (v: string) => void;
}) {
return (
<div>
<Label htmlFor="embed-submission-description">
Provide any additional details (optional)
</Label>
<ReactTextareaAutosize
id="embed-submission-description"
className={cn(
"mt-2 block w-full resize-none rounded-md focus:outline-none sm:text-sm",
"border-border-default bg-bg-default text-content-emphasis",
"placeholder:text-content-muted focus:border-border-emphasis focus:ring-border-emphasis",
)}
minRows={3}
maxLength={BOUNTY_MAX_SUBMISSION_DESCRIPTION_LENGTH}
value={value}
onChange={(e) => {
if (
e.target.value.length <= BOUNTY_MAX_SUBMISSION_DESCRIPTION_LENGTH
) {
onChange(e.target.value);
}
}}
/>
<div className="mt-1 text-left">
<span className="text-content-subtle text-xs">
{value.length} / {BOUNTY_MAX_SUBMISSION_DESCRIPTION_LENGTH}
</span>
</div>
</div>
);
}