|
| 1 | +import {View} from 'react-native' |
| 2 | +import {type AppBskyFeedPost} from '@atproto/api' |
| 3 | +import {Trans, useLingui} from '@lingui/react/macro' |
| 4 | + |
| 5 | +import {getPostEditInfo} from '#/lib/edit-post' |
| 6 | +import {atoms as a, useTheme, web} from '#/alf' |
| 7 | +import * as Dialog from '#/components/Dialog' |
| 8 | +import {Text} from '#/components/Typography' |
| 9 | + |
| 10 | +/** |
| 11 | + * The "· Edited" badge next to a post's timestamp. Renders nothing unless the |
| 12 | + * post was edited; tapping it opens the original-vs-current history. |
| 13 | + */ |
| 14 | +export function PostEditedIndicator({ |
| 15 | + record, |
| 16 | + size = 'md', |
| 17 | +}: { |
| 18 | + record: AppBskyFeedPost.Record |
| 19 | + size?: 'sm' | 'md' |
| 20 | +}) { |
| 21 | + const t = useTheme() |
| 22 | + const {t: l} = useLingui() |
| 23 | + const control = Dialog.useDialogControl() |
| 24 | + const {isEdited, originalText, updatedAt} = getPostEditInfo(record) |
| 25 | + |
| 26 | + if (!isEdited) { |
| 27 | + return null |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <> |
| 32 | + <Text |
| 33 | + accessibilityRole="button" |
| 34 | + accessibilityLabel={l`View edit history`} |
| 35 | + accessibilityHint={l`Opens the original and edited versions of this post`} |
| 36 | + onPress={() => control.open()} |
| 37 | + style={[ |
| 38 | + a.pl_xs, |
| 39 | + size === 'sm' ? a.text_sm : a.text_md, |
| 40 | + a.leading_tight, |
| 41 | + t.atoms.text_contrast_medium, |
| 42 | + web({whiteSpace: 'nowrap', cursor: 'pointer'}), |
| 43 | + ]}> |
| 44 | + <Trans context="Indicates a post has been edited">· Edited</Trans> |
| 45 | + </Text> |
| 46 | + <PostEditHistoryDialog |
| 47 | + control={control} |
| 48 | + originalText={originalText} |
| 49 | + currentText={record.text} |
| 50 | + createdAt={record.createdAt} |
| 51 | + updatedAt={updatedAt} |
| 52 | + /> |
| 53 | + </> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +function PostEditHistoryDialog({ |
| 58 | + control, |
| 59 | + originalText, |
| 60 | + currentText, |
| 61 | + createdAt, |
| 62 | + updatedAt, |
| 63 | +}: { |
| 64 | + control: Dialog.DialogControlProps |
| 65 | + originalText: string | undefined |
| 66 | + currentText: string |
| 67 | + createdAt: string |
| 68 | + updatedAt: string | undefined |
| 69 | +}) { |
| 70 | + const {t: l, i18n} = useLingui() |
| 71 | + |
| 72 | + return ( |
| 73 | + <Dialog.Outer control={control}> |
| 74 | + <Dialog.Handle /> |
| 75 | + <Dialog.ScrollableInner label={l`Edit history`}> |
| 76 | + <Dialog.Header> |
| 77 | + <Dialog.HeaderText> |
| 78 | + <Trans>Edit history</Trans> |
| 79 | + </Dialog.HeaderText> |
| 80 | + </Dialog.Header> |
| 81 | + <View style={[a.pt_lg]}> |
| 82 | + <TimelineEntry |
| 83 | + isCurrent |
| 84 | + label={ |
| 85 | + updatedAt |
| 86 | + ? l`Current · ${i18n.date(new Date(updatedAt), { |
| 87 | + dateStyle: 'medium', |
| 88 | + timeStyle: 'short', |
| 89 | + })}` |
| 90 | + : l`Current` |
| 91 | + } |
| 92 | + text={currentText} |
| 93 | + /> |
| 94 | + <TimelineEntry |
| 95 | + isLast |
| 96 | + label={l`Original · ${i18n.date(new Date(createdAt), { |
| 97 | + dateStyle: 'medium', |
| 98 | + timeStyle: 'short', |
| 99 | + })}`} |
| 100 | + text={originalText ?? ''} |
| 101 | + /> |
| 102 | + </View> |
| 103 | + <Dialog.Close /> |
| 104 | + </Dialog.ScrollableInner> |
| 105 | + </Dialog.Outer> |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +function TimelineEntry({ |
| 110 | + label, |
| 111 | + text, |
| 112 | + isCurrent = false, |
| 113 | + isLast = false, |
| 114 | +}: { |
| 115 | + label: string |
| 116 | + text: string |
| 117 | + isCurrent?: boolean |
| 118 | + isLast?: boolean |
| 119 | +}) { |
| 120 | + const t = useTheme() |
| 121 | + return ( |
| 122 | + <View style={[a.flex_row, a.gap_md]}> |
| 123 | + <View style={[a.align_center, {width: 12}]}> |
| 124 | + <View |
| 125 | + style={[ |
| 126 | + {width: 12, height: 12, borderRadius: 999, marginTop: 3}, |
| 127 | + isCurrent |
| 128 | + ? {backgroundColor: t.palette.primary_500} |
| 129 | + : [{borderWidth: 2}, t.atoms.border_contrast_high, t.atoms.bg], |
| 130 | + ]} |
| 131 | + /> |
| 132 | + {!isLast && ( |
| 133 | + <View |
| 134 | + style={[ |
| 135 | + a.flex_1, |
| 136 | + {width: 2, marginTop: 3, backgroundColor: t.palette.contrast_200}, |
| 137 | + ]} |
| 138 | + /> |
| 139 | + )} |
| 140 | + </View> |
| 141 | + <View style={[a.flex_1, a.gap_xs, !isLast && a.pb_2xl]}> |
| 142 | + <Text style={[a.text_sm, a.font_bold, t.atoms.text_contrast_medium]}> |
| 143 | + {label} |
| 144 | + </Text> |
| 145 | + <Text emoji style={[a.text_md, a.leading_normal, t.atoms.text]}> |
| 146 | + {text} |
| 147 | + </Text> |
| 148 | + </View> |
| 149 | + </View> |
| 150 | + ) |
| 151 | +} |
0 commit comments