|
| 1 | +import classNames from 'classnames'; |
| 2 | +import { |
| 3 | + Avatar, |
| 4 | + Box, |
| 5 | + Header, |
| 6 | + Icon, |
| 7 | + IconButton, |
| 8 | + Icons, |
| 9 | + MenuItem, |
| 10 | + Scroll, |
| 11 | + Text, |
| 12 | + as, |
| 13 | + color, |
| 14 | + config, |
| 15 | +} from 'folds'; |
| 16 | +import { MatrixEvent, Room } from '$types/matrix-sdk'; |
| 17 | +import { getMemberDisplayName } from '$utils/room'; |
| 18 | +import { getMxIdLocalPart } from '$utils/matrix'; |
| 19 | +import { useMatrixClient } from '$hooks/useMatrixClient'; |
| 20 | +import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; |
| 21 | +import { useOpenUserRoomProfile } from '$state/hooks/userRoomProfile'; |
| 22 | +import { useSpaceOptionally } from '$hooks/useSpace'; |
| 23 | +import { getMouseEventCords } from '$utils/dom'; |
| 24 | +import { useAtomValue } from 'jotai'; |
| 25 | +import { nicknamesAtom } from '$state/nicknames'; |
| 26 | +import { UserAvatar } from '$components/user-avatar'; |
| 27 | +import { RenderBody, Time } from '$components/message'; |
| 28 | +import { useSetting } from '$state/hooks/settings'; |
| 29 | +import { settingsAtom } from '$state/settings'; |
| 30 | +import { useMemo } from 'react'; |
| 31 | +import { getReactCustomHtmlParser, LINKIFY_OPTS } from '$plugins/react-custom-html-parser'; |
| 32 | +import { Opts as LinkifyOpts } from 'linkifyjs'; |
| 33 | +import { HTMLReactParserOptions } from 'html-react-parser'; |
| 34 | +import { useSpoilerClickHandler } from '$hooks/useSpoilerClickHandler'; |
| 35 | +import * as css from './EventHistory.css'; |
| 36 | + |
| 37 | +export type EventHistoryProps = { |
| 38 | + room: Room; |
| 39 | + mEvents: MatrixEvent[]; |
| 40 | + requestClose: () => void; |
| 41 | +}; |
| 42 | +export const EventHistory = as<'div', EventHistoryProps>( |
| 43 | + ({ className, room, mEvents, requestClose, ...props }, ref) => { |
| 44 | + const mx = useMatrixClient(); |
| 45 | + const useAuthentication = useMediaAuthentication(); |
| 46 | + const openProfile = useOpenUserRoomProfile(); |
| 47 | + const space = useSpaceOptionally(); |
| 48 | + const nicknames = useAtomValue(nicknamesAtom); |
| 49 | + |
| 50 | + const getName = (userId: string) => |
| 51 | + getMemberDisplayName(room, userId, nicknames) ?? getMxIdLocalPart(userId) ?? userId; |
| 52 | + |
| 53 | + const readerId = mEvents[0].event.sender ?? ''; |
| 54 | + const name = getName(readerId ?? ''); |
| 55 | + const avatarMxcUrl = room.getMember(readerId ?? '')?.getMxcAvatarUrl(); |
| 56 | + const avatarUrl = avatarMxcUrl |
| 57 | + ? mx.mxcUrlToHttp(avatarMxcUrl, 100, 100, 'crop', undefined, false, useAuthentication) |
| 58 | + : undefined; |
| 59 | + |
| 60 | + const [hour24Clock] = useSetting(settingsAtom, 'hour24Clock'); |
| 61 | + const [dateFormatString] = useSetting(settingsAtom, 'dateFormatString'); |
| 62 | + |
| 63 | + const linkifyOpts = useMemo<LinkifyOpts>(() => ({ ...LINKIFY_OPTS }), []); |
| 64 | + const spoilerClickHandler = useSpoilerClickHandler(); |
| 65 | + const htmlReactParserOptions = useMemo<HTMLReactParserOptions>( |
| 66 | + () => |
| 67 | + getReactCustomHtmlParser(mx, mEvents[0].getRoomId(), { |
| 68 | + linkifyOpts, |
| 69 | + useAuthentication, |
| 70 | + handleSpoilerClick: spoilerClickHandler, |
| 71 | + }), |
| 72 | + [linkifyOpts, mEvents, mx, spoilerClickHandler, useAuthentication] |
| 73 | + ); |
| 74 | + return ( |
| 75 | + <Box |
| 76 | + className={classNames(css.EventHistory, className)} |
| 77 | + direction="Column" |
| 78 | + {...props} |
| 79 | + ref={ref} |
| 80 | + > |
| 81 | + <Header className={css.Header} variant="Surface" size="600"> |
| 82 | + <Box grow="Yes"> |
| 83 | + <Text size="H3">Message version history</Text> |
| 84 | + </Box> |
| 85 | + <IconButton size="300" onClick={requestClose}> |
| 86 | + <Icon src={Icons.Cross} /> |
| 87 | + </IconButton> |
| 88 | + </Header> |
| 89 | + <Header> |
| 90 | + <MenuItem |
| 91 | + key={readerId} |
| 92 | + style={{ |
| 93 | + display: 'flex', |
| 94 | + justifyContent: 'center', |
| 95 | + padding: `${config.space.S200} ${config.space.S200}`, |
| 96 | + height: 'unset', |
| 97 | + }} |
| 98 | + radii="400" |
| 99 | + onClick={(event) => { |
| 100 | + openProfile( |
| 101 | + room.roomId, |
| 102 | + space?.roomId, |
| 103 | + readerId, |
| 104 | + getMouseEventCords(event.nativeEvent), |
| 105 | + 'Bottom' |
| 106 | + ); |
| 107 | + }} |
| 108 | + before={ |
| 109 | + <Avatar size="300"> |
| 110 | + <UserAvatar |
| 111 | + userId={readerId ?? ''} |
| 112 | + src={avatarUrl ?? undefined} |
| 113 | + alt={name} |
| 114 | + renderFallback={() => <Icon size="50" src={Icons.User} filled />} |
| 115 | + /> |
| 116 | + </Avatar> |
| 117 | + } |
| 118 | + > |
| 119 | + <Text size="T400">{name}</Text> |
| 120 | + </MenuItem> |
| 121 | + </Header> |
| 122 | + <Box grow="Yes" style={{ overflow: 'scroll' }}> |
| 123 | + <Scroll visibility="Hover"> |
| 124 | + <Box className={css.Content} direction="Column"> |
| 125 | + {mEvents.map((mEvent) => { |
| 126 | + if (!mEvent.event.sender) return <div />; |
| 127 | + const EventContent = mEvent.getOriginalContent(); |
| 128 | + return ( |
| 129 | + <> |
| 130 | + <hr style={{ width: '100%', color: color.Surface.ContainerLine }} /> |
| 131 | + <Box className={css.EventItem}> |
| 132 | + <Time |
| 133 | + ts={mEvent.getTs()} |
| 134 | + hour24Clock={hour24Clock} |
| 135 | + dateFormatString={dateFormatString} |
| 136 | + /> |
| 137 | + <Text size="T400" style={{ paddingLeft: '10px', wordBreak: 'break-word' }}> |
| 138 | + <RenderBody |
| 139 | + body={EventContent?.['m.new_content']?.body ?? EventContent?.body ?? ''} |
| 140 | + customBody={ |
| 141 | + EventContent?.['m.new_content']?.formatted_body ?? |
| 142 | + EventContent?.formatted_body ?? |
| 143 | + '' |
| 144 | + } |
| 145 | + htmlReactParserOptions={htmlReactParserOptions} |
| 146 | + linkifyOpts={linkifyOpts} |
| 147 | + /> |
| 148 | + </Text> |
| 149 | + </Box> |
| 150 | + </> |
| 151 | + ); |
| 152 | + })} |
| 153 | + </Box> |
| 154 | + </Scroll> |
| 155 | + </Box> |
| 156 | + </Box> |
| 157 | + ); |
| 158 | + } |
| 159 | +); |
0 commit comments