-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostSegment.tsx
432 lines (402 loc) · 15.2 KB
/
PostSegment.tsx
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
import { useAutoAnimate } from '@formkit/auto-animate/react'
import type { PostSegmentImagePosition } from '@prisma/client'
import { useMemo, useRef, useState } from 'react'
import { useFormState } from 'react-hook-form'
import { z } from 'zod'
import {
schemaCreatePostSegmentItem,
schemaUpdatePostSegment,
} from '../../../lib/schemas'
import { formatDateTime } from '../../../util/date-time'
import { trpc } from '../../../util/trpc'
import { useOnClickOutside } from '../../../util/use-on-click-outside'
import { useZodForm } from '../../../util/use-zod-form'
import { Box } from '../../Box'
import { ButtonRemove } from '../../Button'
import { ChoiceSelect, useChoiceSelect } from '../../ChoiceSelect'
import { EditOverlay } from '../../EditOverlay'
import { LoadingAnimation } from '../../LoadingAnimation'
import { PostSegmentImage } from '../../PostSegmentImage'
import { Form, FormLabel, Input, useIsSubmitEnabled } from '../../form'
import { IconArrowCircleDown, IconArrowCircleRight } from '../../icons'
import { SegmentPostPage } from './PostPage'
import { PostSegmentItem } from './PostSegmentItem'
/**
* This is a hack to convert Prisma string literals to an enum-like.
* We do this to prevent `@prisma/client` being part of the client bundle.
* See: https://github.com/prisma/prisma/issues/13567#issuecomment-1332030096
*/
const segmentImagePositionMap: { [K in PostSegmentImagePosition]: K } = {
BOTTOM: 'BOTTOM',
RIGHT: 'RIGHT',
}
type SchemaUpdateSegment = z.input<typeof schemaUpdatePostSegment>
const defaultValuesCreate = { content: '' }
export function PostSegment({
postSegmentId,
segment,
sequenceNumber,
isLastInSequence,
postId,
authorId,
isPostEditable = false,
}: {
postSegmentId: string
segment: SegmentPostPage
sequenceNumber: number
isLastInSequence: boolean
postId: string
authorId: string
isPostEditable: boolean
}): JSX.Element {
const [lastSuccessfulEdit, setLastSuccessfulEdit] = useState<Date | null>(
null,
)
const [isItemLoading, setIsItemLoading] = useState(false)
const [isNewSegmentItem] = useState(() => !segment.title)
const utils = trpc.useContext()
function createSuccessfulEditStatus() {
setLastSuccessfulEdit(new Date())
}
async function invalidate() {
await utils.postSegments.byPostId.invalidate({ postId })
await utils.posts.someByUserId.invalidate({ userId: authorId })
createSuccessfulEditStatus()
}
const edit = trpc.postSegments.edit.useMutation({
onSuccess: invalidate,
})
const createItem = trpc.postSegments.createItem.useMutation({
onSuccess: invalidate,
})
const deleteSegment = trpc.postSegments.delete.useMutation({
onSuccess: invalidate,
})
const changePosition = trpc.postSegments.changeImagePosition.useMutation({
onSuccess: () => utils.postSegments.byPostId.invalidate({ postId }),
})
const {
handleSubmit: handleSubmitCreateItem,
register: registerCreateItem,
control: controlCreateItem,
reset: resetCreateItem,
} = useZodForm({
schema: schemaCreatePostSegmentItem.pick({ content: true }),
defaultValues: defaultValuesCreate,
mode: 'onBlur',
reValidateMode: 'onBlur',
shouldFocusError: false,
})
const {
errors: { content: errorContent },
} = useFormState({ control: controlCreateItem, name: 'content' })
const isSubmitCreateItemEnabled = useIsSubmitEnabled({
isInitiallySubmittable: true,
isLoading: createItem.isLoading,
control: controlCreateItem,
})
const [isSegmentEditMode, setIsSegmentEditMode] = useState(
// segments without a title should be considered "new" and are shown in edit mode initially
() => isNewSegmentItem && isPostEditable,
)
const refSegmentEdit = useRef<HTMLDivElement>(null)
useOnClickOutside(refSegmentEdit, () => {
setLastSuccessfulEdit(null)
setIsSegmentEditMode(false)
})
const [animateRef] = useAutoAnimate<HTMLDivElement>()
const choiceControl = useChoiceSelect(
[
{
choiceId: segmentImagePositionMap.BOTTOM,
label: 'Bottom',
icon: <IconArrowCircleDown />,
},
{
choiceId: segmentImagePositionMap.RIGHT,
label: 'Right',
icon: <IconArrowCircleRight />,
},
],
segment.position,
)
const defaultValuesUpdate: SchemaUpdateSegment = useMemo(
() => ({
postSegmentId: segment.id,
title: segment.title,
subtitle: segment.subtitle ?? undefined,
}),
[segment.id, segment.title, segment.subtitle],
)
const {
handleSubmit: handleSubmitUpdate,
register: registerUpdate,
control: controlUpdate,
} = useZodForm({
schema: schemaUpdatePostSegment,
defaultValues: defaultValuesUpdate,
mode: 'onBlur',
reValidateMode: 'onBlur',
})
const { errors: errorsUpdate } = useFormState({ control: controlUpdate })
const isSubmitEnabled = useIsSubmitEnabled({
// new segments should be able to submit initially, as they don't have any data yet
isInitiallySubmittable: !!isNewSegmentItem ?? false,
isLoading: edit.isLoading,
control: controlUpdate,
})
return (
<Box>
<div
ref={refSegmentEdit}
// items-stretch needed for the post image
className="flex w-full flex-col items-stretch rounded-xl bg-white lg:flex-row"
>
<div
className={`px-1 lg:px-4 ${
choiceControl.selected.choiceId === 'RIGHT'
? // keep width in sync with post segment image
'w-full lg:w-4/5'
: 'w-full'
}`}
>
{/* HEADER & ITEMS */}
<EditOverlay
isEnabled={isPostEditable && !isSegmentEditMode}
onClick={() => setIsSegmentEditMode(true)}
>
<div className="rounded-xl">
<Form
className="w-full space-y-4"
onBlur={handleSubmitUpdate((data) => {
// For `onBlur`, RHF does not validate like with `onSubmit`, so we check ourselves.
if (isSubmitEnabled) {
edit.mutate({
postSegmentId,
title: data.title,
subtitle: data.subtitle,
})
}
})}
>
<div className="flex w-full flex-row text-xl">
<div className="h-full w-10 text-left md:w-20">
<span className="text-4xl italic">{sequenceNumber}</span>
</div>
{/* SEGMENT HEADER */}
{/*
* We use conditional CSS instead of conditional rendering so the children are not re-/mounted.
* This is e.g. needed because there is bug in React where unmounting does not trigger `onBlur`.
* See: https://github.com/facebook/react/issues/12363
*/}
<div
className={`grow space-y-6 ${
isSegmentEditMode ? 'block' : 'hidden'
}`}
>
<div>
<FormLabel>Title</FormLabel>
<Input
{...registerUpdate('title')}
hasLabel
blurOnEnterPressed
placeholder="Enter a title.."
autoFocus={
!defaultValuesUpdate.title && isLastInSequence
}
defaultValue={defaultValuesUpdate.title}
validationErrorMessage={errorsUpdate.title?.message}
/>
</div>
<div>
<FormLabel>Subtitle</FormLabel>
<Input
{...registerUpdate('subtitle')}
hasLabel
blurOnEnterPressed
placeholder="Enter a subtitle.."
defaultValue={defaultValuesUpdate.subtitle}
validationErrorMessage={errorsUpdate.subtitle?.message}
/>
</div>
</div>
<div
className={`flex grow ${
isSegmentEditMode ? 'hidden' : 'block'
}`}
>
<div className="ml-2 flex flex-col">
<div className="flex-1 font-serif text-2xl text-dprimary">
<span>{segment.title}</span>
</div>
<div className="flex-1">
<span className="font-serif text-base italic text-dsecondary">
{segment.subtitle}
</span>
</div>
</div>
</div>
</div>
</Form>
{/* SEGMENT ITEMS */}
{/* `relative` here needed for auto-animate. Without it, the edit overlay is shown loosely below the list, instead of overlaying the list. */}
<div
ref={animateRef}
className="relative mt-8 space-y-2 lg:space-y-4"
>
{segment.items.map((item, index) => (
<PostSegmentItem
key={item.id}
index={index}
isEditMode={isSegmentEditMode}
itemContent={item.content}
postId={postId}
postSegmentItemId={item.id}
setIsLoading={(isLoadingNew) =>
setIsItemLoading(isLoadingNew)
}
onSuccessfulSubmit={createSuccessfulEditStatus}
/>
))}
</div>
</div>
</EditOverlay>
{/* EDIT ACTIONS */}
<div className={isSegmentEditMode ? 'block' : 'hidden'}>
<p className="my-6 text-center text-xl text-dprimary">
<span>Add a new item:</span>
</p>
<Form
onBlur={handleSubmitCreateItem((data) => {
// For `onBlur`, RHF does not validate like with `onSubmit`, so we check ourselves.
if (isSubmitCreateItemEnabled) {
createItem.mutate(
{
segmentId: postSegmentId,
content: data.content,
},
{ onSuccess: () => resetCreateItem() },
)
}
})}
className="my-4 flex w-full items-center space-x-4"
>
<div className="grow">
<Input
{...registerCreateItem('content')}
placeholder="Enter some text.."
blurOnEnterPressed
validationErrorMessage={errorContent?.message}
/>
</div>
</Form>
<div className="my-2 text-center italic">
{/* height needed to not make it jump when the loading animation is shown */}
<div className="h-7 tracking-tighter">
<div className="animate-fade-in">
{!lastSuccessfulEdit ? (
<span>No changes yet.</span>
) : (
<div className="grid h-7 grid-cols-8 place-items-center lg:grid-cols-3">
<div className="col-span-1 justify-self-end">
{(edit.isLoading ||
isItemLoading ||
createItem.isLoading) && (
<LoadingAnimation size="small" />
)}
</div>
<div className="col-span-6 lg:col-span-1">
<span>Saved changes</span>
<span className="ml-2 text-gray-400">
{formatDateTime(lastSuccessfulEdit, 'MM-DD hh:mm:ss')}
</span>
</div>
</div>
)}
</div>
</div>
</div>
</div>
{/* POST SEGMENT IMAGE - BOTTOM */}
<div
className={
'mt-6 grid w-full place-items-center' +
// placeholder doesn't need to be as big as an image
` ${
segment.imageId
? 'min-h-[150px] lg:min-h-[250px]'
: 'min-h-[100px]'
}` +
// on mobile, we always show the image at the bottom, so we hide it here on larger screens if necessary
` ${choiceControl.selected.choiceId !== 'BOTTOM' && 'lg:hidden'}`
}
>
<PostSegmentImage
isEditable={isPostEditable}
postId={postId}
postSegmentId={postSegmentId}
imageId={segment.imageId}
imageFileExtension={segment.imageFileExtension}
position="BOTTOM"
/>
</div>
{isPostEditable && (
<div className="mt-4 flex flex-col items-center space-y-4 lg:flex-row-reverse lg:items-end lg:justify-between lg:space-y-0">
<div className="space-y-4">
{!segment.imageId ? (
/*
* Hack to respect the "justify between".
* With e.g. `hidden` the button remove button would take the full width and be wrongly positioned.
*/
<> </>
) : (
<>
<div className="text-center">
<p>Image position:</p>
<p className="text-sm italic">(only on larger screens)</p>
</div>
<ChoiceSelect
control={choiceControl}
onSelect={(selectedChoice) => {
changePosition.mutate({
postSegmentId,
position: selectedChoice.choiceId,
})
}}
/>
</>
)}
</div>
<div>
<ButtonRemove
showLoading={deleteSegment.isLoading}
onClick={() =>
deleteSegment.mutate({ segmentId: postSegmentId })
}
>
Remove segment
</ButtonRemove>
</div>
</div>
)}
</div>
{/* POST SEGMENT IMAGE - RIGHT */}
{/* The parent container uses "items-stretch" so the image can "fill" the height. */}
{/* Also, by not rendering the div, we allow the "BOTTOM" image to take the full width of the segment, */}
{/* that's why the whole div here is rendered conditionally. */}
{choiceControl.selected.choiceId === 'RIGHT' && (
<div className="hidden min-h-[150px] w-full place-items-center lg:grid lg:w-1/5">
<PostSegmentImage
isEditable={isPostEditable}
postId={postId}
postSegmentId={postSegmentId}
imageId={segment.imageId}
imageFileExtension={segment.imageFileExtension}
position="RIGHT"
/>
</div>
)}
</div>
</Box>
)
}