| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  | // Copyright 2023 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import React, { useState, useEffect, useMemo } from 'react'; | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | import type { Meta, StoryFn } from '@storybook/react'; | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  | import { noop } from 'lodash'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { Inbox } from './Inbox'; | 
					
						
							|  |  |  | import type { PropsType } from './Inbox'; | 
					
						
							|  |  |  | import { DAY, SECOND } from '../util/durations'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { setupI18n } from '../util/setupI18n'; | 
					
						
							|  |  |  | import enMessages from '../../_locales/en/messages.json'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const i18n = setupI18n('en', enMessages); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   title: 'Components/Inbox', | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  |   args: { | 
					
						
							|  |  |  |     i18n, | 
					
						
							|  |  |  |     hasInitialLoadCompleted: false, | 
					
						
							|  |  |  |     isCustomizingPreferredReactions: false, | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | } satisfies Meta<PropsType>; | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // eslint-disable-next-line react/function-component-definition
 | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | const Template: StoryFn<PropsType & { daysAgo?: number }> = ({ | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  |   daysAgo, | 
					
						
							|  |  |  |   ...args | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const now = useMemo(() => Date.now(), []); | 
					
						
							| 
									
										
										
										
											2023-03-29 09:47:06 -07:00
										 |  |  |   const [dayOffset, setDayOffset] = useState(0); | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (daysAgo === undefined) { | 
					
						
							| 
									
										
										
										
											2023-03-29 09:47:06 -07:00
										 |  |  |       setDayOffset(0); | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  |       return noop; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const interval = setInterval(() => { | 
					
						
							| 
									
										
										
										
											2023-03-29 09:47:06 -07:00
										 |  |  |       // Increment day offset by 1 / 24 of a day (an hour), and wrap it when it
 | 
					
						
							|  |  |  |       // reaches `daysAgo` value.
 | 
					
						
							|  |  |  |       setDayOffset(prevValue => (prevValue + 1 / 24) % daysAgo); | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  |     }, SECOND / 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return () => clearInterval(interval); | 
					
						
							|  |  |  |   }, [now, daysAgo]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const firstEnvelopeTimestamp = | 
					
						
							|  |  |  |     daysAgo === undefined ? undefined : now - daysAgo * DAY; | 
					
						
							|  |  |  |   const envelopeTimestamp = | 
					
						
							|  |  |  |     firstEnvelopeTimestamp === undefined | 
					
						
							|  |  |  |       ? undefined | 
					
						
							| 
									
										
										
										
											2023-03-29 09:47:06 -07:00
										 |  |  |       : firstEnvelopeTimestamp + dayOffset * DAY; | 
					
						
							| 
									
										
										
										
											2023-03-28 13:31:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <Inbox | 
					
						
							|  |  |  |       {...args} | 
					
						
							|  |  |  |       firstEnvelopeTimestamp={firstEnvelopeTimestamp} | 
					
						
							|  |  |  |       envelopeTimestamp={envelopeTimestamp} | 
					
						
							|  |  |  |       renderCustomizingPreferredReactionsModal={() => <div />} | 
					
						
							|  |  |  |     /> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const Default = Template.bind({}); |