import React, { useCallback, useEffect } from 'react'; import FocusTrap from 'focus-trap-react'; import { Box, Header, Icon, IconButton, Icons, Modal, Overlay, OverlayBackdrop, OverlayCenter, Scroll, Spinner, Text, config, } from 'folds'; import { MatrixEvent, Room } from 'matrix-js-sdk'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { stopPropagation } from '../../../utils/keyboard'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { timeDayMonYear, timeHourMinute } from '../../../utils/time'; import { useSetting } from '../../../state/hooks/settings'; import { settingsAtom } from '../../../state/settings'; type RawEditEvent = { type: string; content: Record; origin_server_ts: number; event_id: string; }; type EditHistoryResponse = { chunk: Array>; next_batch?: string; }; type EditHistoryData = { events: MatrixEvent[]; hasMore: boolean; }; type EditHistoryModalProps = { room: Room; mEvent: MatrixEvent; onClose: () => void; }; function isRawEditEvent(raw: unknown): raw is RawEditEvent { if (typeof raw !== 'object' || raw === null) return false; const r = raw as Record; return typeof r.event_id === 'string' && typeof r.origin_server_ts === 'number'; } function getVersionBody(evt: MatrixEvent): string { const content = evt.getContent(); const newContent = content['m.new_content'] as Record | undefined; const source = newContent ?? content; const formattedBody = source.formatted_body; if (typeof formattedBody === 'string') { return formattedBody.replace(/<[^>]+>/g, '').trim() || '(no text)'; } const body = source.body; return typeof body === 'string' ? body : '(no text)'; } export function EditHistoryModal({ room, mEvent, onClose }: EditHistoryModalProps) { const mx = useMatrixClient(); const [hour24Clock] = useSetting(settingsAtom, 'hour24Clock'); const [dateFormatString] = useSetting(settingsAtom, 'dateFormatString'); const eventId = mEvent.getId(); const roomId = room.roomId; const [historyState, fetchHistory] = useAsyncCallback( useCallback(async () => { if (!eventId) return { events: [], hasMore: false }; // Relations API lives at /_matrix/client/v1/ (not v3); use raw fetch to avoid SDK prefix const token = mx.getAccessToken(); const baseUrl = mx.getHomeserverUrl(); const url = `${baseUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(eventId)}/m.replace?limit=50`; const fetchRes = await fetch(url, { headers: { Authorization: `Bearer ${token}` } }); if (!fetchRes.ok) throw new Error(`HTTP ${fetchRes.status}`); const res = (await fetchRes.json()) as EditHistoryResponse; const rawEvents = res.chunk ?? []; const events = rawEvents .filter(isRawEditEvent) .sort((a, b) => a.origin_server_ts - b.origin_server_ts) .map((raw) => { const existing = room.findEventById(raw.event_id); if (existing) return existing; return new MatrixEvent({ type: raw.type, content: raw.content, origin_server_ts: raw.origin_server_ts, event_id: raw.event_id, room_id: roomId, sender: mEvent.getSender() ?? '', }); }); return { events, hasMore: !!res.next_batch }; }, [mx, roomId, eventId, room, mEvent]), ); useEffect(() => { fetchHistory(); }, [fetchHistory]); const formatTs = (ts: number): string => { const time = timeHourMinute(ts, hour24Clock); const date = timeDayMonYear(ts, dateFormatString); return `${date} at ${time}`; }; const originalBody = (() => { const content = mEvent.getContent(); const body = content.body; return typeof body === 'string' ? body : '(no text)'; })(); const originalTs = mEvent.getTs(); return ( }>
Edit History
{historyState.status === AsyncStatus.Loading && ( )} {historyState.status === AsyncStatus.Error && ( Failed to load edit history. )} {historyState.status === AsyncStatus.Success && ( Original {formatTs(originalTs)} {originalBody} {historyState.data.events.map((editEvt, index) => ( {index === historyState.data.events.length - 1 ? `Edit ${index + 1} (current)` : `Edit ${index + 1}`} {formatTs(editEvt.getTs())} {getVersionBody(editEvt)} ))} {historyState.data.events.length === 0 && ( No edit history found. )} {historyState.data.hasMore && ( Showing the 50 most recent edits )} )}
); }