|
| 1 | +import type { ReactNode, SessionDataTestId, CSSProperties } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { PubKey } from '../../session/types'; |
| 4 | + |
| 5 | +const StyledSessionIDNotEditable = styled.div` |
| 6 | + display: flex; |
| 7 | + gap: var(--margins-sm); |
| 8 | + user-select: none; |
| 9 | + text-align: center; |
| 10 | + word-break: break-all; |
| 11 | + font-weight: 400; |
| 12 | + font-size: var(--font-size-md); |
| 13 | + color: var(--text-secondary-color); |
| 14 | + flex-shrink: 0; |
| 15 | + font-family: var(--font-mono); |
| 16 | +
|
| 17 | + .session-id-tooltip { |
| 18 | + font-family: var(--font-default); |
| 19 | + font-size: var(--font-size-sm); |
| 20 | + } |
| 21 | +`; |
| 22 | + |
| 23 | +export const SessionIDNotEditable = ({ |
| 24 | + sessionId, |
| 25 | + dataTestId, |
| 26 | + tooltipNode, |
| 27 | + displayType, |
| 28 | + style: providedStyle = {}, |
| 29 | +}: { |
| 30 | + sessionId: string; |
| 31 | + tooltipNode: ReactNode; |
| 32 | + displayType: 'blinded' | '2lines' | '3lines'; |
| 33 | + dataTestId: SessionDataTestId; |
| 34 | + style: CSSProperties; |
| 35 | +}) => { |
| 36 | + if (sessionId.length !== 66) { |
| 37 | + throw new Error('Unsupported case for SessionIDNotEditable: sessionId.length !== 66'); |
| 38 | + } |
| 39 | + if (PubKey.isBlinded(sessionId) && displayType !== 'blinded') { |
| 40 | + throw new Error('Unsupported case for SessionIDNotEditable: sessionId is blinded'); |
| 41 | + } |
| 42 | + |
| 43 | + const style = tooltipNode ? { ...providedStyle, marginLeft: 'var(--margins-lg)' } : providedStyle; |
| 44 | + |
| 45 | + if (displayType === 'blinded') { |
| 46 | + const shortenedSessionId = PubKey.shorten(sessionId, { |
| 47 | + keepCharacters: 12, |
| 48 | + withParenthesis: false, |
| 49 | + }); |
| 50 | + |
| 51 | + return ( |
| 52 | + <StyledSessionIDNotEditable |
| 53 | + data-testid={dataTestId} |
| 54 | + // Note: we want the text centered even if the tooltip is offsetting it |
| 55 | + style={style} |
| 56 | + > |
| 57 | + {shortenedSessionId} |
| 58 | + {tooltipNode} |
| 59 | + </StyledSessionIDNotEditable> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (displayType === '3lines') { |
| 64 | + const firstLine = sessionId.slice(0, 27); |
| 65 | + const secondLine = sessionId.slice(27, 54); |
| 66 | + const thirdLine = sessionId.slice(54); |
| 67 | + return ( |
| 68 | + <StyledSessionIDNotEditable |
| 69 | + data-testid={dataTestId} // Note: we want the text centered even if the tooltip is offsetting it |
| 70 | + style={style} |
| 71 | + > |
| 72 | + {firstLine} |
| 73 | + <br /> |
| 74 | + {secondLine} |
| 75 | + <br /> |
| 76 | + {thirdLine} |
| 77 | + {tooltipNode} |
| 78 | + </StyledSessionIDNotEditable> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <StyledSessionIDNotEditable data-testid={dataTestId} style={style}> |
| 84 | + {sessionId.slice(0, 33)} |
| 85 | + <br /> |
| 86 | + {sessionId.slice(33)} |
| 87 | + </StyledSessionIDNotEditable> |
| 88 | + ); |
| 89 | +}; |
0 commit comments