|
| 1 | +/** |
| 2 | + * MeshCoreMessageRouteModal — shown when the relay-hash chain on a received |
| 3 | + * MeshCore message is clicked. Presents the message's reception details and |
| 4 | + * expands each relay hash to the matching repeater / room-server name from |
| 5 | + * the contact list (mirroring the {ROUTE_NAMES} automation token): only |
| 6 | + * relay infrastructure ever appears in a path, unknown hashes stay raw, and |
| 7 | + * hash collisions resolve to the candidate nearest the neighbouring hops. |
| 8 | + * Reuses the packet-monitor modal styling (mcpm-*). |
| 9 | + */ |
| 10 | +import React, { useMemo } from 'react'; |
| 11 | +import { useTranslation } from 'react-i18next'; |
| 12 | +import type { MeshCoreMessage } from './hooks/useMeshCore'; |
| 13 | +import type { MeshCoreContact } from '../../utils/meshcoreHelpers'; |
| 14 | +import { |
| 15 | + parsePathHops, |
| 16 | + pathHashBytesOf, |
| 17 | + repeaterHopOptions, |
| 18 | + resolveRouteNames, |
| 19 | +} from '../../utils/meshcorePath'; |
| 20 | +import { UiIcon } from '../icons'; |
| 21 | +import './MeshCorePacketMonitor.css'; |
| 22 | + |
| 23 | +interface Props { |
| 24 | + message: MeshCoreMessage; |
| 25 | + /** Sender label already resolved by the stream (name or key prefix). */ |
| 26 | + fromLabel: string; |
| 27 | + contacts: MeshCoreContact[]; |
| 28 | + onClose: () => void; |
| 29 | +} |
| 30 | + |
| 31 | +const Row: React.FC<{ label: string; children: React.ReactNode; mono?: boolean; wrap?: boolean }> = ({ label, children, mono, wrap }) => ( |
| 32 | + <div className="mcpm-dl-row"> |
| 33 | + <span className="mcpm-dl-label">{label}</span> |
| 34 | + <span className={`mcpm-dl-value${mono ? ' mcpm-mono' : ''}${wrap ? ' mcpm-dl-wrap' : ''}`}>{children}</span> |
| 35 | + </div> |
| 36 | +); |
| 37 | + |
| 38 | +const MeshCoreMessageRouteModal: React.FC<Props> = ({ message, fromLabel, contacts, onClose }) => { |
| 39 | + const { t } = useTranslation(); |
| 40 | + |
| 41 | + const hops = useMemo(() => parsePathHops(message.routePath), [message.routePath]); |
| 42 | + const width = pathHashBytesOf(hops); |
| 43 | + const rows = useMemo(() => { |
| 44 | + const names = resolveRouteNames(hops, contacts); |
| 45 | + const options = repeaterHopOptions(contacts, width); |
| 46 | + return hops.map((hash, i) => ({ |
| 47 | + hash, |
| 48 | + name: names[i], |
| 49 | + matchCount: options.filter((o) => o.hopByte === hash).length, |
| 50 | + })); |
| 51 | + }, [hops, contacts, width]); |
| 52 | + |
| 53 | + const scopeStr = message.scopeName |
| 54 | + ? message.scopeName |
| 55 | + : typeof message.scopeCode === 'number' && message.scopeCode !== 0 |
| 56 | + ? `#${message.scopeCode.toString(16).padStart(4, '0')}` |
| 57 | + : '—'; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className="mcpm-modal" onClick={onClose}> |
| 61 | + <div className="mcpm-modal-content" onClick={(e) => e.stopPropagation()}> |
| 62 | + <div className="mcpm-modal-header"> |
| 63 | + <h4>{t('meshcore.route_detail_title', 'Message Route')}</h4> |
| 64 | + <button className="mcpm-modal-close" onClick={onClose} aria-label={t('common.close', 'Close')}>×</button> |
| 65 | + </div> |
| 66 | + |
| 67 | + <div className="mcpm-modal-body"> |
| 68 | + <section className="mcpm-dl-section"> |
| 69 | + <h5>{t('meshcore.route_detail_message', 'Message')}</h5> |
| 70 | + <Row label={t('meshcore.route_detail_from', 'From')}>{fromLabel}</Row> |
| 71 | + <Row label={t('meshcore.route_detail_time', 'Time')} mono>{new Date(message.timestamp).toLocaleString()}</Row> |
| 72 | + <Row label={t('meshcore.route_detail_hops', 'Hops')} mono>{typeof message.hopCount === 'number' ? message.hopCount : '—'}</Row> |
| 73 | + <Row label={t('meshcore.route_detail_scope', 'Scope')}>{scopeStr}</Row> |
| 74 | + {message.text && ( |
| 75 | + <Row label={t('meshcore.route_detail_text', 'Text')} wrap>{message.text}</Row> |
| 76 | + )} |
| 77 | + </section> |
| 78 | + |
| 79 | + <section className="mcpm-dl-section"> |
| 80 | + <h5>{t('meshcore.route_detail_route', 'Route')}</h5> |
| 81 | + <Row label={t('meshcore.packets.hashWidth', 'Hash width')} mono>{width} B</Row> |
| 82 | + {rows.length === 0 ? ( |
| 83 | + <Row label={t('meshcore.packets.routing', 'Routing')}>{t('meshcore.packets.directNoRelay', 'Direct (no relays)')}</Row> |
| 84 | + ) : ( |
| 85 | + <> |
| 86 | + {rows.map((r, i) => ( |
| 87 | + <Row key={`${r.hash}-${i}`} label={`#${i + 1}`} mono> |
| 88 | + {r.hash} |
| 89 | + {' '}<UiIcon name="forward" size={12} />{' '} |
| 90 | + {r.matchCount === 0 |
| 91 | + ? t('meshcore.route_detail_unknown', 'unknown repeater') |
| 92 | + : r.name} |
| 93 | + {r.matchCount > 1 && ( |
| 94 | + <span className="mc-route-best-guess"> |
| 95 | + {' '}({t('meshcore.route_detail_best_guess', 'best guess of')} {r.matchCount} {t('meshcore.route_detail_matches', 'matches')}) |
| 96 | + </span> |
| 97 | + )} |
| 98 | + </Row> |
| 99 | + ))} |
| 100 | + <Row label={t('meshcore.route_detail_resolved', 'Resolved')} wrap> |
| 101 | + <span className="mc-route-resolved"> |
| 102 | + {rows.map((r, i) => ( |
| 103 | + <React.Fragment key={`${r.hash}-${i}`}> |
| 104 | + {r.matchCount === 0 ? r.hash : r.name} |
| 105 | + {i < rows.length - 1 && <> <UiIcon name="forward" size={12} /> </>} |
| 106 | + </React.Fragment> |
| 107 | + ))} |
| 108 | + </span> |
| 109 | + </Row> |
| 110 | + </> |
| 111 | + )} |
| 112 | + </section> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +export default MeshCoreMessageRouteModal; |
0 commit comments