import * as React from "react" ; import { Alert , Text , TextStyle } from "react-native" ; import Share from "react-native-share" ; import { Divider , Icon , ListItem } from "react-native-elements" ; import { StackNavigationProp } from "@react-navigation/stack" ; import { gql , OperationVariables , QueryLazyOptions , useLazyQuery } from "@apollo/client" ; import type { ViewStyleProp } from "react-native/Libraries/StyleSheet/StyleSheet" ; import { Screen } from "../../components/screen" ; import { VersionComponent } from "../../components/version" ; import { palette } from "../../theme/palette" ; import { LN_PAGE_DOMAIN , WHATSAPP_CONTACT_NUMBER } from "../../constants/support" ; import { translate } from "../../i18n" ; import { openWhatsApp } from "../../utils/external" ; import KeyStoreWrapper from "../../utils/storage/secureStorage" ; import type { ScreenType } from "../../types/jsx" ; import type { RootStackParamList } from "../../navigation/stack-param-lists" ; import Clipboard from "@react-native-community/clipboard" ; import { toastShow } from "../../utils/toast" ; import useToken from "../../utils/use-token" ; import useLogout from "../../hooks/use-logout" ; import useMainQuery from "@app/hooks/use-main-query" ; type Props = { navigation : StackNavigationProp < RootStackParamList , "settings" > ; } ; export const SettingsScreen = ( { navigation } ) => { const { hasToken } = useToken ( ) ; const { logout } = useLogout ( ) ; const { btcWalletId , csvEncoded , username , phoneNumber , userPreferredLanguage } = useMainQuery ( ) ; const securityAction = async ( ) => { const isBiometricsEnabled = await KeyStoreWrapper . getIsBiometricsEnabled ( ) ; const isPinEnabled = await KeyStoreWrapper . getIsPinEnabled ( ) ; navigation . navigate ( "security" , { mIsBiometricsEnabled : isBiometricsEnabled , mIsPinEnabled : isPinEnabled , } ) ; } ; const logoutAction = async ( ) => { try { await logout ( ) ; Alert . alert ( translate ( "common.loggedOut" ) , "" , [ { text : translate ( "common.ok" ) , onPress : ( ) => { navigation . goBack ( ) ; } , } , ] ) ; } catch ( err ) { console . error ( err ) ; } } ; const onGetCsvCallback = async ( ) => { try { await Share . open ( { url : `data:text/csv;base64,${ csvEncoded }` , type : "text/csv" , filename : "export.csv" , } ) ; } catch ( err ) { console . error ( err ) ; } } ; const [ getCsv ] = useLazyQuery ( gql `
      query getWalletCSVTransactions($defaultWalletId: WalletId!) {
        me {
          id
          defaultAccount {
            id
            csvTransactions(walletIds: [$defaultWalletId])
          }
        }
      }
    ` , { onCompleted : onGetCsvCallback } ) ; return ( < SettingsScreenJSX hasToken = { hasToken } navigation = { navigation } username = { username } phone = { phoneNumber } language = { translate ( `Languages.${ userPreferredLanguage || "DEFAULT" } `)} csvAction={() => getCsv({ variables: { btcWalletId } })} securityAction={securityAction} logoutAction={logoutAction}/>);
};
type SettingsScreenProps = {
    hasToken: boolean;
    navigation: StackNavigationProp<RootStackParamList, "settings">;
    username: string;
    phone: string;
    language: string;
    notificationsEnabled: boolean;
    csvAction: (options?: QueryLazyOptions<OperationVariables>) => void;
    securityAction: () => void;
    logoutAction: () => Promise<void>;
};
type SettingRow = {
    id: string;
    icon: string;
    category: string;
    hidden?: boolean;
    enabled?: boolean;
    subTitleText?: string;
    subTitleDefaultValue?: string;
    action?: () => void;
    greyed?: boolean;
    styleDivider?: ViewStyleProp;
};
export const SettingsScreenJSX = (params) => {
    const { hasToken, navigation, username, phone, language, csvAction, securityAction, logoutAction, } = params;
    const copyToClipBoard = (username) => {
        Clipboard.setString(LN_PAGE_DOMAIN + username);
        Clipboard.getString().then((data) => toastShow(translate("tippingLink.copied", { data }), {
            backgroundColor: palette.lightBlue,
        }));
    };
    const openWhatsAppAction = () => openWhatsApp(WHATSAPP_CONTACT_NUMBER, translate("whatsapp.defaultSupportMessage"));
    const settingList = [
        {
            category: translate("common.phoneNumber"),
            icon: "call",
            id: "phone",
            subTitleDefaultValue: translate("SettingsScreen.tapLogIn"),
            subTitleText: phone,
            action: () => navigation.navigate("phoneValidation"),
            enabled: !hasToken,
            greyed: hasToken,
        },
        {
            category: translate("common.username"),
            icon: "ios-person-circle",
            id: "username",
            subTitleDefaultValue: translate("SettingsScreen.tapUserName"),
            subTitleText: username,
            action: () => navigation.navigate("setUsername"),
            enabled: hasToken && !username,
            greyed: !hasToken,
        },
        {
            category: translate("common.language"),
            icon: "ios-language",
            id: "language",
            subTitleText: language,
            action: () => navigation.navigate("language"),
            enabled: hasToken,
            greyed: !hasToken,
        },
        {
            category: translate("common.security"),
            icon: "lock-closed-outline",
            id: "security",
            action: securityAction,
            enabled: hasToken,
            greyed: !hasToken,
        },
        {
            category: translate("common.csvExport"),
            icon: "ios-download",
            id: "csv",
            action: () => csvAction(),
            enabled: hasToken,
            greyed: !hasToken,
        },
        {
            category: translate("tippingLink.title"),
            icon: "cash-outline",
            id: "tippingLink",
            action: () => copyToClipBoard(username),
            enabled: hasToken && username !== null,
            greyed: !hasToken || username === null,
        },
        {
            category: translate("whatsapp.contactUs"),
            icon: "ios-logo-whatsapp",
            id: "contact-us",
            action: openWhatsAppAction,
            enabled: true,
            greyed: false,
            styleDivider: { backgroundColor: palette.lighterGrey, height: 18 },
        },
        {
            category: translate("common.logout"),
            id: "logout",
            icon: "ios-log-out",
            action: () => logoutAction(),
            enabled: hasToken,
            greyed: !hasToken,
            hidden: !hasToken,
        },
    ];
    return (<Screen preset="scroll">
      {settingList.map((setting, i) => {
            if (setting.hidden) {
                return null;
            }
            const settingColor = setting.greyed ? palette.midGrey : palette.darkGrey;
            const settingStyle = { color: settingColor };
            return (<React.Fragment key={` setting - option - $ { i } `}>
            <ListItem onPress={setting.action} disabled={!setting.enabled}>
              <Icon name={setting.icon} type="ionicon" color={settingColor}/>
              <ListItem.Content>
                <ListItem.Title style={settingStyle}>
                  <Text>{setting.category}</Text>
                </ListItem.Title>
                {setting.subTitleText && (<ListItem.Subtitle style={settingStyle}>
                    <Text>{setting.subTitleText}</Text>
                  </ListItem.Subtitle>)}
              </ListItem.Content>
              {setting.enabled && <ListItem.Chevron />}
            </ListItem>
            <Divider style={setting.styleDivider}/>
          </React.Fragment>);
        })}
      <VersionComponent />
    </Screen>);
};
