diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-03 13:51:20 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-03 13:51:20 -0500 |
| commit | 9ad8b004544e9edcf5141f536bb6be99b49dba51 (patch) | |
| tree | 979514b145fb10d087aab7bed3f2eb39a3530156 /src/web | |
| parent | 9a7556a9c9b3c1313f11f369d8b38ad8fe144b58 (diff) | |
| download | slskd-9ad8b004544e9edcf5141f536bb6be99b49dba51.tar.gz | |
memoize history
Co-authored-by: Copilot <copilot@github.com>
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/components/Chat/Chat.jsx | 58 | ||||
| -rw-r--r-- | src/web/src/components/Rooms/Rooms.jsx | 58 |
2 files changed, 73 insertions, 43 deletions
diff --git a/src/web/src/components/Chat/Chat.jsx b/src/web/src/components/Chat/Chat.jsx index 086d2cd8..39dd1a44 100644 --- a/src/web/src/components/Chat/Chat.jsx +++ b/src/web/src/components/Chat/Chat.jsx @@ -23,6 +23,32 @@ const initialState = { message: '', }; +const ChatMessageHistory = React.memo( + ({ formatTimestamp, messages, selfUsername }) => { + return ( + <> + {messages.map((message) => ( + <List.Content + className={`chat-message ${message.direction === 'Out' ? 'chat-message-self' : ''}`} + key={`${message.timestamp}+${message.message}`} + > + <span className="chat-message-time"> + {formatTimestamp(message.timestamp)} + </span> + <span className="chat-message-name"> + {message.direction === 'Out' ? selfUsername : message.username}: + </span> + <span className="chat-message-message">{message.message}</span> + </List.Content> + ))} + <List.Content id="chat-history-scroll-anchor" /> + </> + ); + }, +); + +ChatMessageHistory.displayName = 'ChatMessageHistory'; + class Chat extends Component { constructor(props) { super(props); @@ -63,6 +89,13 @@ class Chat extends Component { // fetch all of the active conversations (but no messages) let conversations = await chat.getAll(); + // the username '..' breaks nearly everything in slskd because it's a path traversal + // sequence and no API endpoints that expect usernames in the path work + // rather than letting this sit broken, i'm making the choice to filter it out + // eslint-disable-next-line no-warning-comments + // todo: remove this filter when the API can handle '..' + conversations = conversations.filter((f) => f.username !== '..'); + // turn into a map, keyed by username // if there are no active conversations, set to an empty map if (conversations.length === 0) { @@ -257,26 +290,11 @@ class Chat extends Component { <Segment className="chat-history"> <Ref innerRef={this.listRef}> <List> - {messages.map((message) => ( - <List.Content - className={`chat-message ${message.direction === 'Out' ? 'chat-message-self' : ''}`} - key={`${message.timestamp}+${message.message}`} - > - <span className="chat-message-time"> - {this.formatTimestamp(message.timestamp)} - </span> - <span className="chat-message-name"> - {message.direction === 'Out' - ? user.username - : message.username} - :{' '} - </span> - <span className="chat-message-message"> - {message.message} - </span> - </List.Content> - ))} - <List.Content id="chat-history-scroll-anchor" /> + <ChatMessageHistory + formatTimestamp={this.formatTimestamp} + messages={messages} + selfUsername={user.username} + /> </List> </Ref> </Segment> diff --git a/src/web/src/components/Rooms/Rooms.jsx b/src/web/src/components/Rooms/Rooms.jsx index 3d29242b..f6bdb685 100644 --- a/src/web/src/components/Rooms/Rooms.jsx +++ b/src/web/src/components/Rooms/Rooms.jsx @@ -39,6 +39,36 @@ const initialState = { }, }; +const RoomMessageHistory = React.memo( + ({ formatTimestamp, messages, onHandleContextMenu }) => { + return ( + <> + {messages.map((message) => ( + <div + key={`${message.timestamp}+${message.message}`} + onContextMenu={(clickEvent) => + onHandleContextMenu(clickEvent, message) + } + > + <List.Content + className={`room-message ${message.self ? 'room-message-self' : ''}`} + > + <span className="room-message-time"> + {formatTimestamp(message.timestamp)} + </span> + <span className="room-message-name">{message.username}: </span> + <span className="room-message-message">{message.message}</span> + </List.Content> + </div> + ))} + <List.Content id="room-history-scroll-anchor" /> + </> + ); + }, +); + +RoomMessageHistory.displayName = 'RoomMessageHistory'; + class Rooms extends Component { constructor(props) { super(props); @@ -325,29 +355,11 @@ class Rooms extends Component { <Segment className="room-history"> <Ref innerRef={this.listRef}> <List> - {room.messages.map((message) => ( - <div - key={`${message.timestamp}+${message.message}`} - onContextMenu={(clickEvent) => - this.handleContextMenu(clickEvent, message) - } - > - <List.Content - className={`room-message ${message.self ? 'room-message-self' : ''}`} - > - <span className="room-message-time"> - {this.formatTimestamp(message.timestamp)} - </span> - <span className="room-message-name"> - {message.username}:{' '} - </span> - <span className="room-message-message"> - {message.message} - </span> - </List.Content> - </div> - ))} - <List.Content id="room-history-scroll-anchor" /> + <RoomMessageHistory + formatTimestamp={this.formatTimestamp} + messages={room.messages} + onHandleContextMenu={this.handleContextMenu} + /> </List> </Ref> </Segment> |
