|
| 1 | +import { compareDesc, parseISO } from 'date-fns'; |
| 2 | +import { ArrowRight } from 'lucide-react'; |
| 3 | +import { FC } from 'react'; |
| 4 | + |
| 5 | +import { Timeline, TimelineDescription, TimelineItem, TimelineTime } from '@/components/timeline/timeline'; |
| 6 | +import { |
| 7 | + ChangeItemType, |
| 8 | + ChangeSetItemType, |
| 9 | + SocialAccountChangeItem, |
| 10 | + TimelineItemType, |
| 11 | +} from '@/types/profile-timeline.types'; |
| 12 | +import { isObject } from '@/utils/is-object'; |
| 13 | +import { splitCamelCase } from '@/utils/split-camelcase'; |
| 14 | + |
| 15 | +type ProfileTimelineProps = { |
| 16 | + timeline: TimelineItemType[] | null | undefined; |
| 17 | + firstSeenAt?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +type ProfileTimelineDescriptionProps = { |
| 21 | + type: string; |
| 22 | + changeset: ChangeSetItemType; |
| 23 | +}; |
| 24 | + |
| 25 | +const parseChangesetItem = (changesetItem: ChangeItemType) => { |
| 26 | + if (isObject<NonNullable<SocialAccountChangeItem>>(changesetItem)) { |
| 27 | + if (!changesetItem.totalCount) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + return changesetItem.nodes |
| 32 | + ?.map((account) => `${account.provider?.toLowerCase()}: ${account.displayName}`) |
| 33 | + .join('; '); |
| 34 | + } |
| 35 | + |
| 36 | + if (typeof changesetItem === 'number') { |
| 37 | + return changesetItem.toLocaleString('en-US'); |
| 38 | + } |
| 39 | + |
| 40 | + if (!changesetItem) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + return String(changesetItem); |
| 45 | +}; |
| 46 | + |
| 47 | +const ProfileTimelineDescription: FC<ProfileTimelineDescriptionProps> = ({ type, changeset }) => { |
| 48 | + const before = parseChangesetItem(changeset.b); |
| 49 | + const after = parseChangesetItem(changeset.a); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="flex items-center gap-2"> |
| 53 | + {splitCamelCase(type)}:{' '} |
| 54 | + {!!before && ( |
| 55 | + <span className="opacity-50"> |
| 56 | + {before} {!after && '(removed)'} |
| 57 | + </span> |
| 58 | + )}{' '} |
| 59 | + {!!before && !!after && <ArrowRight size={12} />} {after} |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +export const ProfileTimeline: FC<ProfileTimelineProps> = ({ timeline, firstSeenAt }) => { |
| 65 | + if (!timeline?.length) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + const sortedTimeline = timeline.sort((a, b) => compareDesc(parseISO(a.createdAt), parseISO(b.createdAt))); |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="flex flex-col gap-6"> |
| 73 | + <h2 className="text-xl font-semibold">Timeline</h2> |
| 74 | + <Timeline> |
| 75 | + {sortedTimeline.map((item) => ( |
| 76 | + <TimelineItem key={item.createdAt}> |
| 77 | + <TimelineTime isoDate={item.createdAt} /> |
| 78 | + <TimelineDescription> |
| 79 | + {Object.entries(item.changes) |
| 80 | + .filter(([, changeset]) => !!changeset.a || !!changeset.b) |
| 81 | + .map(([type, changeset]) => ( |
| 82 | + <ProfileTimelineDescription key={type} type={type} changeset={changeset} /> |
| 83 | + ))} |
| 84 | + </TimelineDescription> |
| 85 | + </TimelineItem> |
| 86 | + ))} |
| 87 | + {!!firstSeenAt && ( |
| 88 | + <TimelineItem> |
| 89 | + <TimelineTime isoDate={firstSeenAt} /> |
| 90 | + <TimelineDescription>First seen</TimelineDescription> |
| 91 | + </TimelineItem> |
| 92 | + )} |
| 93 | + </Timeline> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +}; |
0 commit comments