| 
									
										
										
										
											2023-01-03 11:55:46 -08:00
										 |  |  | // Copyright 2019 Signal Messenger, LLC
 | 
					
						
							| 
									
										
										
										
											2020-10-30 15:34:04 -05:00
										 |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  | import React from 'react'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type PropsType = { | 
					
						
							|  |  |  |   id: string; | 
					
						
							|  |  |  |   conversationId: string; | 
					
						
							|  |  |  |   isSelected: boolean; | 
					
						
							| 
									
										
										
										
											2019-11-21 11:16:06 -08:00
										 |  |  |   selectMessage?: (messageId: string, conversationId: string) => unknown; | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class InlineNotificationWrapper extends React.Component<PropsType> { | 
					
						
							|  |  |  |   public focusRef: React.RefObject<HTMLDivElement> = React.createRef(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 12:51:27 -07:00
										 |  |  |   public setFocus = (): void => { | 
					
						
							| 
									
										
										
										
											2019-11-14 15:54:50 -08:00
										 |  |  |     const container = this.focusRef.current; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (container && !container.contains(document.activeElement)) { | 
					
						
							|  |  |  |       container.focus(); | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 12:51:27 -07:00
										 |  |  |   public handleFocus = (): void => { | 
					
						
							| 
									
										
										
										
											2019-11-21 11:16:06 -08:00
										 |  |  |     if (window.getInteractionMode() === 'keyboard') { | 
					
						
							|  |  |  |       this.setSelected(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 12:51:27 -07:00
										 |  |  |   public setSelected = (): void => { | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |     const { id, conversationId, selectMessage } = this.props; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 11:16:06 -08:00
										 |  |  |     if (selectMessage) { | 
					
						
							|  |  |  |       selectMessage(id, conversationId); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 17:44:20 -06:00
										 |  |  |   public override componentDidMount(): void { | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |     const { isSelected } = this.props; | 
					
						
							|  |  |  |     if (isSelected) { | 
					
						
							|  |  |  |       this.setFocus(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 17:44:20 -06:00
										 |  |  |   public override componentDidUpdate(prevProps: PropsType): void { | 
					
						
							| 
									
										
										
										
											2020-09-14 12:51:27 -07:00
										 |  |  |     const { isSelected } = this.props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!prevProps.isSelected && isSelected) { | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |       this.setFocus(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 17:44:20 -06:00
										 |  |  |   public override render(): JSX.Element { | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |     const { children } = this.props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <div | 
					
						
							|  |  |  |         className="module-inline-notification-wrapper" | 
					
						
							| 
									
										
										
										
											2020-09-14 12:51:27 -07:00
										 |  |  |         // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
 | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |         tabIndex={0} | 
					
						
							|  |  |  |         ref={this.focusRef} | 
					
						
							| 
									
										
										
										
											2019-11-21 11:16:06 -08:00
										 |  |  |         onFocus={this.handleFocus} | 
					
						
							| 
									
										
										
										
											2019-11-07 13:36:16 -08:00
										 |  |  |       > | 
					
						
							|  |  |  |         {children} | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |