-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathShareHeader.tsx
More file actions
295 lines (278 loc) · 8.36 KB
/
ShareHeader.tsx
File metadata and controls
295 lines (278 loc) · 8.36 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
"use client";
import type { userSelectProps } from "@cap/database/auth/session";
import type { videos } from "@cap/database/schema";
import { buildEnv, NODE_ENV } from "@cap/env";
import { Button } from "@cap/ui";
import { userIsPro } from "@cap/utils";
import { faChevronDown, faLock } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import clsx from "clsx";
import { Check, Copy, Globe2 } from "lucide-react";
import moment from "moment";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { editTitle } from "@/actions/videos/edit-title";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { SharingDialog } from "@/app/(org)/dashboard/caps/components/SharingDialog";
import type { Spaces } from "@/app/(org)/dashboard/dashboard-data";
import { UpgradeModal } from "@/components/UpgradeModal";
import { usePublicEnv } from "@/utils/public-env";
export const ShareHeader = ({
data,
user,
customDomain,
domainVerified,
sharedOrganizations = [],
sharedSpaces = [],
spacesData = null,
}: {
data: typeof videos.$inferSelect;
user: typeof userSelectProps | null;
customDomain?: string | null;
domainVerified?: boolean;
sharedOrganizations?: { id: string; name: string }[];
userOrganizations?: { id: string; name: string }[];
sharedSpaces?: {
id: string;
name: string;
iconUrl?: string;
organizationId: string;
}[];
userSpaces?: {
id: string;
name: string;
iconUrl?: string;
organizationId: string;
}[];
spacesData?: Spaces[] | null;
}) => {
const { push, refresh } = useRouter();
const [isEditing, setIsEditing] = useState(false);
const [title, setTitle] = useState(data.name);
const [upgradeModalOpen, setUpgradeModalOpen] = useState(false);
const [isSharingDialogOpen, setIsSharingDialogOpen] = useState(false);
const [linkCopied, setLinkCopied] = useState(false);
const contextData = useDashboardContext();
const contextSharedSpaces = contextData?.sharedSpaces || null;
const effectiveSharedSpaces = contextSharedSpaces || sharedSpaces;
const isOwner = user && user.id.toString() === data.ownerId;
const { webUrl } = usePublicEnv();
useEffect(() => {
setTitle(data.name);
}, [data.name]);
const handleBlur = async () => {
setIsEditing(false);
try {
await editTitle(data.id, title);
toast.success("Video title updated");
refresh();
} catch (error) {
if (error instanceof Error) {
toast.error(error.message);
} else {
toast.error("Failed to update title - please try again.");
}
}
};
const handleKeyDown = async (event: { key: string }) => {
if (event.key === "Enter") {
handleBlur();
}
};
const getVideoLink = () => {
if (NODE_ENV === "development" && customDomain && domainVerified) {
return `https://${customDomain}/s/${data.id}`;
} else if (NODE_ENV === "development" && !customDomain && !domainVerified) {
return `${webUrl}/s/${data.id}`;
} else if (buildEnv.NEXT_PUBLIC_IS_CAP && customDomain && domainVerified) {
return `https://${customDomain}/s/${data.id}`;
} else if (
buildEnv.NEXT_PUBLIC_IS_CAP &&
!customDomain &&
!domainVerified
) {
return `https://cap.link/${data.id}`;
} else {
return `${webUrl}/s/${data.id}`;
}
};
const getDisplayLink = () => {
if (NODE_ENV === "development" && customDomain && domainVerified) {
return `${customDomain}/s/${data.id}`;
} else if (NODE_ENV === "development" && !customDomain && !domainVerified) {
return `${webUrl}/s/${data.id}`;
} else if (buildEnv.NEXT_PUBLIC_IS_CAP && customDomain && domainVerified) {
return `${customDomain}/s/${data.id}`;
} else if (
buildEnv.NEXT_PUBLIC_IS_CAP &&
!customDomain &&
!domainVerified
) {
return `cap.link/${data.id}`;
} else {
return `${webUrl}/s/${data.id}`;
}
};
const isUserPro = userIsPro(user);
const showUpgradeBanner =
user && data.ownerId === user.id && !userIsPro(user);
const handleSharingUpdated = () => {
refresh();
};
const renderSharedStatus = () => {
const baseClassName =
"text-sm text-gray-10 transition-colors duration-200 flex items-center";
if (isOwner) {
const hasSpaceSharing =
sharedOrganizations?.length > 0 || effectiveSharedSpaces?.length > 0;
const isPublic = data.public;
if (!hasSpaceSharing && !isPublic) {
return (
<p
className={clsx(baseClassName, "cursor-pointer hover:text-gray-12")}
onClick={() => setIsSharingDialogOpen(true)}
>
Not shared{" "}
<FontAwesomeIcon className="ml-2 size-2.5" icon={faChevronDown} />
</p>
);
} else {
return (
<p
className={clsx(baseClassName, "cursor-pointer hover:text-gray-12")}
onClick={() => setIsSharingDialogOpen(true)}
>
Shared{" "}
<FontAwesomeIcon className="ml-1 size-2.5" icon={faChevronDown} />
</p>
);
}
} else {
return <p className={baseClassName}>Shared with you</p>;
}
};
return (
<>
{showUpgradeBanner && (
<div className="flex sticky flex-col sm:flex-row inset-x-0 top-0 z-10 gap-4 justify-center items-center px-3 py-2 mx-auto w-[calc(100%-20px)] max-w-fit rounded-b-xl border bg-gray-4 border-gray-6">
<p className="text-center text-gray-12">
Shareable links are limited to 5 mins on the free plan.
</p>
<Button
type="button"
onClick={() => setUpgradeModalOpen(true)}
size="sm"
variant="blue"
>
Upgrade To Cap Pro
</Button>
</div>
)}
<SharingDialog
isOpen={isSharingDialogOpen}
onClose={() => setIsSharingDialogOpen(false)}
capId={data.id}
capName={data.name}
sharedSpaces={effectiveSharedSpaces || []}
onSharingUpdated={handleSharingUpdated}
isPublic={data.public}
spacesData={spacesData}
/>
<div className="mt-8">
<div className="flex flex-col gap-6 lg:flex-row lg:items-center lg:justify-between lg:gap-0">
<div className="items-center md:flex md:justify-between md:space-x-6">
<div className="mb-3 md:mb-0">
<div className="flex items-center space-x-3 lg:min-w-[400px]">
{isEditing ? (
<input
value={title}
onChange={(e) => setTitle(e.target.value)}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
autoFocus
className="w-full text-xl font-semibold sm:text-2xl"
/>
) : (
<h1
className="text-xl sm:text-2xl"
onClick={() => {
if (user && user.id.toString() === data.ownerId) {
setIsEditing(true);
}
}}
>
{title}
</h1>
)}
</div>
{user && renderSharedStatus()}
<p className="mt-1 text-sm text-gray-10">
{moment(data.createdAt).fromNow()}
</p>
</div>
</div>
{user !== null && (
<div className="flex space-x-2">
<div>
<div className="flex gap-2 items-center">
{data.password && (
<FontAwesomeIcon
className="text-amber-600 size-4"
icon={faLock}
/>
)}
<Button
variant="white"
onClick={() => {
navigator.clipboard.writeText(getVideoLink());
setLinkCopied(true);
setTimeout(() => {
setLinkCopied(false);
}, 2000);
}}
>
{getDisplayLink()}
{linkCopied ? (
<Check className="ml-2 w-4 h-4 svgpathanimation" />
) : (
<Copy className="ml-2 w-4 h-4" />
)}
</Button>
</div>
{user !== null && !isUserPro && (
<button
type="button"
className="flex items-center mt-2 mb-3 text-sm text-gray-400 duration-200 cursor-pointer hover:text-blue-500"
onClick={() => setUpgradeModalOpen(true)}
>
<Globe2 className="mr-1 w-4 h-4" />
Connect a custom domain
</button>
)}
</div>
{user !== null && (
<div className="hidden md:flex">
<Button
onClick={() => {
push("/dashboard/caps?page=1");
}}
>
<span className="hidden text-sm text-white lg:block">
Go to
</span>{" "}
Dashboard
</Button>
</div>
)}
</div>
)}
</div>
</div>
<UpgradeModal
open={upgradeModalOpen}
onOpenChange={setUpgradeModalOpen}
/>
</>
);
};