| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  | // Copyright 2023 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import React, { useCallback, useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | import type { Meta, StoryFn } from '@storybook/react'; | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | import { action } from '@storybook/addon-actions'; | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  | import enMessages from '../../_locales/en/messages.json'; | 
					
						
							|  |  |  | import { UsernameLinkState } from '../state/ducks/usernameEnums'; | 
					
						
							|  |  |  | import { setupI18n } from '../util/setupI18n'; | 
					
						
							|  |  |  | import { SignalService as Proto } from '../protobuf'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import type { PropsType } from './UsernameLinkModalBody'; | 
					
						
							| 
									
										
										
										
											2024-01-25 15:48:44 -08:00
										 |  |  | import { | 
					
						
							|  |  |  |   UsernameLinkModalBody, | 
					
						
							|  |  |  |   PRINT_WIDTH, | 
					
						
							|  |  |  |   PRINT_HEIGHT, | 
					
						
							|  |  |  | } from './UsernameLinkModalBody'; | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  | import { Modal } from './Modal'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ColorEnum = Proto.AccountRecord.UsernameLink.Color; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const i18n = setupI18n('en', enMessages); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   component: UsernameLinkModalBody, | 
					
						
							|  |  |  |   title: 'Components/UsernameLinkModalBody', | 
					
						
							|  |  |  |   argTypes: { | 
					
						
							|  |  |  |     link: { | 
					
						
							|  |  |  |       control: { type: 'text' }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     username: { | 
					
						
							|  |  |  |       control: { type: 'text' }, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-11-03 23:05:11 +01:00
										 |  |  |     usernameLinkCorrupted: { | 
					
						
							|  |  |  |       control: 'boolean', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-02-06 10:35:59 -08:00
										 |  |  |     usernameLinkRecovered: { | 
					
						
							|  |  |  |       control: 'boolean', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  |     usernameLinkState: { | 
					
						
							|  |  |  |       control: { type: 'select' }, | 
					
						
							| 
									
										
										
										
											2023-11-03 23:05:11 +01:00
										 |  |  |       options: [ | 
					
						
							|  |  |  |         UsernameLinkState.Ready, | 
					
						
							|  |  |  |         UsernameLinkState.Updating, | 
					
						
							|  |  |  |         UsernameLinkState.Error, | 
					
						
							|  |  |  |       ], | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     colorId: { | 
					
						
							|  |  |  |       control: { type: 'select' }, | 
					
						
							|  |  |  |       mapping: { | 
					
						
							|  |  |  |         blue: ColorEnum.BLUE, | 
					
						
							|  |  |  |         white: ColorEnum.WHITE, | 
					
						
							|  |  |  |         grey: ColorEnum.GREY, | 
					
						
							|  |  |  |         olive: ColorEnum.OLIVE, | 
					
						
							|  |  |  |         green: ColorEnum.GREEN, | 
					
						
							|  |  |  |         orange: ColorEnum.ORANGE, | 
					
						
							|  |  |  |         pink: ColorEnum.PINK, | 
					
						
							|  |  |  |         purple: ColorEnum.PURPLE, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  |   args: { | 
					
						
							|  |  |  |     i18n, | 
					
						
							|  |  |  |     link: 'https://signal.me/#eu/n-AJkmmykrFB7j6UODGndSycxcMdp_v6ppRp9rFu5Ad39q_9Ngi_k9-TARWfT43t', | 
					
						
							|  |  |  |     username: 'alice.12', | 
					
						
							|  |  |  |     usernameLinkState: UsernameLinkState.Ready, | 
					
						
							|  |  |  |     colorId: ColorEnum.BLUE, | 
					
						
							|  |  |  |     showToast: action('showToast'), | 
					
						
							|  |  |  |     resetUsernameLink: action('resetUsernameLink'), | 
					
						
							|  |  |  |     setUsernameLinkColor: action('setUsernameLinkColor'), | 
					
						
							| 
									
										
										
										
											2024-02-06 10:35:59 -08:00
										 |  |  |     clearUsernameLinkRecovered: action('clearUsernameLinkRecovered'), | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  |     onBack: action('onBack'), | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | } satisfies Meta<PropsType>; | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // eslint-disable-next-line react/function-component-definition
 | 
					
						
							| 
									
										
										
										
											2023-10-11 12:06:43 -07:00
										 |  |  | const Template: StoryFn<PropsType> = args => { | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  |   const [attachment, setAttachment] = useState<string | undefined>(); | 
					
						
							|  |  |  |   const saveAttachment = useCallback(({ data }: { data?: Uint8Array }) => { | 
					
						
							|  |  |  |     if (!data) { | 
					
						
							|  |  |  |       setAttachment(undefined); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const blob = new Blob([data], { | 
					
						
							|  |  |  |       type: 'image/png', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setAttachment(oldURL => { | 
					
						
							|  |  |  |       if (oldURL) { | 
					
						
							|  |  |  |         URL.revokeObjectURL(oldURL); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return URL.createObjectURL(blob); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							|  |  |  |       <Modal modalName="story" i18n={i18n} hasXButton> | 
					
						
							|  |  |  |         <UsernameLinkModalBody {...args} saveAttachment={saveAttachment} /> | 
					
						
							|  |  |  |       </Modal> | 
					
						
							| 
									
										
										
										
											2024-01-25 15:48:44 -08:00
										 |  |  |       {attachment && ( | 
					
						
							|  |  |  |         <img | 
					
						
							|  |  |  |           src={attachment} | 
					
						
							|  |  |  |           width={PRINT_WIDTH} | 
					
						
							|  |  |  |           height={PRINT_HEIGHT} | 
					
						
							|  |  |  |           alt="printable qr code" | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       )} | 
					
						
							| 
									
										
										
										
											2023-07-20 05:14:08 +02:00
										 |  |  |     </> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const Normal = Template.bind({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const NoLink = Template.bind({}); | 
					
						
							|  |  |  | NoLink.args = { link: '' }; |