|
| 1 | +import Clipboard from '@react-native-clipboard/clipboard' |
| 2 | +import { useQuery } from '@tanstack/react-query' |
| 3 | +import * as React from 'react' |
| 4 | +import { View } from 'react-native' |
| 5 | + |
| 6 | +import { ENV } from '../../env' |
| 7 | +import { useGiftCardProvider } from '../../hooks/useGiftCardProvider' |
| 8 | +import { useHandler } from '../../hooks/useHandler' |
| 9 | +import { lstrings } from '../../locales/strings' |
| 10 | +import { useSelector } from '../../types/reactRedux' |
| 11 | +import type { EdgeAppSceneProps } from '../../types/routerTypes' |
| 12 | +import { SceneButtons } from '../buttons/SceneButtons' |
| 13 | +import { EdgeCard } from '../cards/EdgeCard' |
| 14 | +import { SceneWrapper } from '../common/SceneWrapper' |
| 15 | +import { ConfirmContinueModal } from '../modals/ConfirmContinueModal' |
| 16 | +import { EdgeRow } from '../rows/EdgeRow' |
| 17 | +import { Airship, showError, showToast } from '../services/AirshipInstance' |
| 18 | +import { cacheStyles, type Theme, useTheme } from '../services/ThemeContext' |
| 19 | +import { Paragraph } from '../themed/EdgeText' |
| 20 | + |
| 21 | +export interface GiftCardAccountInfoParams { |
| 22 | + quoteId?: string |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Displays Phaze gift card account credentials behind a confirmation wall. |
| 27 | + * Accessible from the kebab menu (with quoteId context) or developer settings. |
| 28 | + */ |
| 29 | +export const GiftCardAccountInfoScene: React.FC< |
| 30 | + EdgeAppSceneProps<'giftCardAccountInfo'> |
| 31 | +> = props => { |
| 32 | + const { route } = props |
| 33 | + const { quoteId } = route.params |
| 34 | + const theme = useTheme() |
| 35 | + const styles = getStyles(theme) |
| 36 | + |
| 37 | + const account = useSelector(state => state.core.account) |
| 38 | + |
| 39 | + // Provider for identity lookup |
| 40 | + const phazeConfig = (ENV.PLUGIN_API_KEYS as Record<string, unknown>) |
| 41 | + ?.phaze as { apiKey?: string; baseUrl?: string } | undefined |
| 42 | + const { provider } = useGiftCardProvider({ |
| 43 | + account, |
| 44 | + apiKey: phazeConfig?.apiKey ?? '', |
| 45 | + baseUrl: phazeConfig?.baseUrl ?? '' |
| 46 | + }) |
| 47 | + |
| 48 | + const [isRevealed, setIsRevealed] = React.useState(false) |
| 49 | + |
| 50 | + const { data: identities = [], error } = useQuery({ |
| 51 | + queryKey: ['phazeIdentities', account.id], |
| 52 | + queryFn: async () => { |
| 53 | + if (provider == null) throw new Error('Provider not ready') |
| 54 | + return await provider.listIdentities(account) |
| 55 | + }, |
| 56 | + enabled: isRevealed && provider != null |
| 57 | + }) |
| 58 | + |
| 59 | + React.useEffect(() => { |
| 60 | + if (error != null) showError(error) |
| 61 | + }, [error]) |
| 62 | + |
| 63 | + const handleReveal = useHandler(async () => { |
| 64 | + const confirmed = await Airship.show<boolean>(bridge => ( |
| 65 | + <ConfirmContinueModal |
| 66 | + bridge={bridge} |
| 67 | + title={lstrings.gift_card_account_info_title} |
| 68 | + body={lstrings.gift_card_account_info_warning} |
| 69 | + warning |
| 70 | + onPress={async () => true} |
| 71 | + /> |
| 72 | + )) |
| 73 | + if (confirmed) { |
| 74 | + setIsRevealed(true) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + const handleCopyAll = useHandler(async () => { |
| 79 | + const lines: string[] = [] |
| 80 | + |
| 81 | + if (quoteId != null) { |
| 82 | + lines.push(`${lstrings.gift_card_quote_id_label}: ${quoteId}`) |
| 83 | + } |
| 84 | + |
| 85 | + identities.forEach(identity => { |
| 86 | + lines.push(`${lstrings.gift_card_account_info_email}: ${identity.email}`) |
| 87 | + }) |
| 88 | + |
| 89 | + const text = lines.join('\n') |
| 90 | + Clipboard.setString(text) |
| 91 | + showToast(lstrings.fragment_copied) |
| 92 | + }) |
| 93 | + |
| 94 | + return ( |
| 95 | + <SceneWrapper scroll={isRevealed}> |
| 96 | + <View style={styles.container}> |
| 97 | + <Paragraph>{lstrings.gift_card_account_info_body}</Paragraph> |
| 98 | + |
| 99 | + {isRevealed && ( |
| 100 | + <EdgeCard sections> |
| 101 | + {quoteId != null && ( |
| 102 | + <EdgeRow |
| 103 | + title={lstrings.gift_card_quote_id_label} |
| 104 | + body={quoteId} |
| 105 | + /> |
| 106 | + )} |
| 107 | + {identities.map(identity => ( |
| 108 | + <EdgeRow |
| 109 | + key={String(identity.id)} |
| 110 | + title={lstrings.gift_card_account_info_email} |
| 111 | + body={identity.email} |
| 112 | + /> |
| 113 | + ))} |
| 114 | + </EdgeCard> |
| 115 | + )} |
| 116 | + |
| 117 | + <SceneButtons |
| 118 | + primary={ |
| 119 | + isRevealed |
| 120 | + ? { |
| 121 | + label: lstrings.fragment_request_copy_title, |
| 122 | + onPress: handleCopyAll |
| 123 | + } |
| 124 | + : { |
| 125 | + label: lstrings.gift_card_account_info_reveal_button, |
| 126 | + onPress: handleReveal |
| 127 | + } |
| 128 | + } |
| 129 | + /> |
| 130 | + </View> |
| 131 | + </SceneWrapper> |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +const getStyles = cacheStyles((theme: Theme) => ({ |
| 136 | + container: { |
| 137 | + padding: theme.rem(0.5) |
| 138 | + } |
| 139 | +})) |
0 commit comments