-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathShareLink.tsx
More file actions
191 lines (169 loc) · 5.47 KB
/
ShareLink.tsx
File metadata and controls
191 lines (169 loc) · 5.47 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
import type { FormEventHandler, ReactElement } from 'react';
import React, { useState } from 'react';
import z from 'zod';
import classNames from 'classnames';
import { useRouter } from 'next/router';
import type { Squad } from '../../../graphql/sources';
import MarkdownInput from '../../fields/MarkdownInput';
import { WriteFooter } from './WriteFooter';
import { SubmitExternalLink } from './SubmitExternalLink';
import { usePostToSquad, useToastNotification } from '../../../hooks';
import type { Post } from '../../../graphql/posts';
import { PostType } from '../../../graphql/posts';
import { WriteLinkPreview } from './WriteLinkPreview';
import useSourcePostModeration from '../../../hooks/source/useSourcePostModeration';
import type { SourcePostModeration } from '../../../graphql/squads';
import { usePrompt } from '../../../hooks/usePrompt';
import { useWritePostContext } from '../../../contexts';
import { MAX_POST_COMMENTARY_LENGTH } from '../../../constants/post';
interface ShareLinkProps {
squad?: Squad;
post?: Post;
moderated?: SourcePostModeration;
className?: string;
onPostSuccess: (post: Post, url: string) => void;
isPostingOnMySource?: boolean;
}
const confirmSharingAgainPrompt = {
title: 'This link has already been shared.',
description: 'Are you sure you want to repost it?',
okButton: {
title: 'Repost',
className: 'btn-primary-cabbage',
},
};
export function ShareLink({
squad,
className,
onPostSuccess,
post,
moderated,
isPostingOnMySource = false,
}: ShareLinkProps): ReactElement {
const fetchedPost = post || moderated;
const isCreatingPost = !fetchedPost;
const { showPrompt } = usePrompt();
const { push } = useRouter();
const { displayToast } = useToastNotification();
const { onSubmitForm, isPosting: isPendingCreation } = useWritePostContext();
const [commentary, setCommentary] = useState(
fetchedPost?.sharedPost ? fetchedPost?.title : fetchedPost?.content,
);
const {
getLinkPreview,
isLoadingPreview,
preview,
isPosting,
onUpdateSharePost,
onUpdatePreview,
} = usePostToSquad({
onPostSuccess,
initialPreview:
fetchedPost?.sharedPost ||
(moderated && {
url: moderated.externalLink,
title: moderated.title,
image: moderated.image,
}),
});
const { onUpdatePostModeration, isPending: isPostingModeration } =
useSourcePostModeration({
onSuccess: async () => push(squad?.permalink),
});
// Character limit state
const commentaryLength = commentary?.length ?? 0;
const isCommentaryTooLong = commentaryLength > MAX_POST_COMMENTARY_LENGTH;
const onUpdateSubmit: FormEventHandler<HTMLFormElement> = async (e) => {
if (!fetchedPost?.id || !squad) {
return null;
}
const isSameAsBefore = [fetchedPost?.title, moderated?.content].includes(
commentary,
);
if (isSameAsBefore) {
return push(squad.permalink);
}
if (moderated) {
return onUpdatePostModeration({
type: PostType.Share,
sourceId: squad.id,
id: moderated.id,
title: moderated.sharedPost ? commentary : preview.title,
content: moderated.sharedPost ? null : commentary,
imageUrl: moderated.sharedPost ? null : preview.image,
externalLink: moderated.sharedPost ? null : preview.url,
});
}
return onUpdateSharePost(e, fetchedPost.id, commentary, squad);
};
const onSubmit: FormEventHandler<HTMLFormElement> = async (e) => {
e.preventDefault();
if (isPosting || isPostingModeration || isPendingCreation) {
return null;
}
if (!isCreatingPost) {
return onUpdateSubmit(e);
}
const isLinkAlreadyShared = preview.relatedPublicPosts?.length > 0;
const proceedSharingLink =
!isPostingOnMySource ||
!isLinkAlreadyShared ||
(await showPrompt(confirmSharingAgainPrompt));
if (!proceedSharingLink) {
return null;
}
const { title, image } = preview;
const externalLink = preview.finalUrl ?? preview.url;
if (!title) {
displayToast('Invalid link');
return null;
}
const { success: isImageValid } = z.httpUrl().safeParse(image);
const args = {
commentary,
content: '',
externalLink,
title,
...(isImageValid && { imageUrl: image }),
};
return onSubmitForm(e, args, PostType.Share);
};
return (
<form
className={classNames('flex flex-col gap-4', className)}
onSubmit={onSubmit}
id="write-post-link"
>
{fetchedPost?.sharedPost ? (
<WriteLinkPreview
link={fetchedPost.sharedPost.permalink}
preview={fetchedPost.sharedPost}
showPreviewLink={false}
/>
) : (
<SubmitExternalLink
preview={preview || fetchedPost?.sharedPost}
getLinkPreview={getLinkPreview}
isLoadingPreview={isLoadingPreview}
onSelectedHistory={onUpdatePreview}
/>
)}
<MarkdownInput
initialContent={commentary || fetchedPost?.title || ''}
enabledCommand={{ mention: true }}
showMarkdownGuide={false}
onValueUpdate={setCommentary}
maxInputLength={MAX_POST_COMMENTARY_LENGTH}
/>
{isCommentaryTooLong && (
<p className="text-text-tertiary typo-caption">
Maximum length is {MAX_POST_COMMENTARY_LENGTH} characters
</p>
)}
<WriteFooter
isLoading={isPosting || isPostingModeration || isPendingCreation}
disabled={isCommentaryTooLong}
/>
</form>
);
}