|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { useDispatch, useSelector } from "react-redux"; |
| 4 | +import { Navigate, useLocation } from "react-router-dom"; |
| 5 | +import { Icon, Notification } from "@stellar/design-system"; |
| 6 | + |
| 7 | +import { |
| 8 | + AutoLockTimeoutMinutes, |
| 9 | + VALID_AUTO_LOCK_TIMEOUT_MINUTES, |
| 10 | + coerceAutoLockTimeoutMinutes, |
| 11 | + formatTimeoutLabel, |
| 12 | +} from "@shared/constants/autoLock"; |
| 13 | +import { AppDispatch } from "popup/App"; |
| 14 | +import { SubviewHeader } from "popup/components/SubviewHeader"; |
| 15 | +import { View } from "popup/basics/layout/View"; |
| 16 | +import { Loading } from "popup/components/Loading"; |
| 17 | +import { AppDataType, useGetAppData } from "helpers/hooks/useGetAppData"; |
| 18 | +import { RequestState } from "constants/request"; |
| 19 | +import { openTab } from "popup/helpers/navigate"; |
| 20 | +import { newTabHref } from "helpers/urls"; |
| 21 | +import { reRouteOnboarding } from "popup/helpers/route"; |
| 22 | +import { |
| 23 | + autoLockTimeoutMinutesSelector, |
| 24 | + saveSettings, |
| 25 | + settingsSelector, |
| 26 | +} from "popup/ducks/settings"; |
| 27 | + |
| 28 | +import "./styles.scss"; |
| 29 | + |
| 30 | +export const AutoLockTimer = () => { |
| 31 | + const { t } = useTranslation(); |
| 32 | + const location = useLocation(); |
| 33 | + const dispatch = useDispatch<AppDispatch>(); |
| 34 | + const { state, fetchData } = useGetAppData(); |
| 35 | + const currentTimeout = useSelector(autoLockTimeoutMinutesSelector); |
| 36 | + const settings = useSelector(settingsSelector); |
| 37 | + |
| 38 | + const [isSaving, setIsSaving] = useState(false); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + const getData = async () => { |
| 42 | + await fetchData(); |
| 43 | + }; |
| 44 | + getData(); |
| 45 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 46 | + }, []); |
| 47 | + |
| 48 | + if ( |
| 49 | + state.state === RequestState.IDLE || |
| 50 | + state.state === RequestState.LOADING |
| 51 | + ) { |
| 52 | + return <Loading />; |
| 53 | + } |
| 54 | + |
| 55 | + if (state.state === RequestState.ERROR) { |
| 56 | + return ( |
| 57 | + <div className="AddAsset__fetch-fail"> |
| 58 | + <Notification |
| 59 | + variant="error" |
| 60 | + title={t("Failed to fetch your account data.")} |
| 61 | + > |
| 62 | + {t("Your account data could not be fetched at this time.")} |
| 63 | + </Notification> |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (state.data?.type === AppDataType.REROUTE) { |
| 69 | + if (state.data.shouldOpenTab) { |
| 70 | + openTab(newTabHref(state.data.routeTarget)); |
| 71 | + window.close(); |
| 72 | + } |
| 73 | + return ( |
| 74 | + <Navigate |
| 75 | + to={`${state.data.routeTarget}${location.search}`} |
| 76 | + state={{ from: location }} |
| 77 | + replace |
| 78 | + /> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + reRouteOnboarding({ |
| 83 | + type: state.data.type, |
| 84 | + applicationState: state.data.account.applicationState, |
| 85 | + state: state.state, |
| 86 | + }); |
| 87 | + |
| 88 | + const handleSelect = async (minutes: AutoLockTimeoutMinutes) => { |
| 89 | + if (isSaving || minutes === currentTimeout) { |
| 90 | + return; |
| 91 | + } |
| 92 | + setIsSaving(true); |
| 93 | + await dispatch( |
| 94 | + saveSettings({ |
| 95 | + isDataSharingAllowed: |
| 96 | + settings.isDataSharingAllowed ?? false, |
| 97 | + isMemoValidationEnabled: |
| 98 | + settings.isMemoValidationEnabled ?? true, |
| 99 | + isHideDustEnabled: settings.isHideDustEnabled ?? true, |
| 100 | + isOpenSidebarByDefault: settings.isOpenSidebarByDefault ?? false, |
| 101 | + autoLockTimeoutMinutes: minutes, |
| 102 | + }), |
| 103 | + ); |
| 104 | + setIsSaving(false); |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <React.Fragment> |
| 109 | + <SubviewHeader title={t("Auto-Lock Timer")} /> |
| 110 | + <View.Content hasNoTopPadding> |
| 111 | + <div className="AutoLockTimer"> |
| 112 | + {VALID_AUTO_LOCK_TIMEOUT_MINUTES.map((minutes) => { |
| 113 | + const isSelected = |
| 114 | + coerceAutoLockTimeoutMinutes(currentTimeout) === minutes; |
| 115 | + return ( |
| 116 | + <React.Fragment key={minutes}> |
| 117 | + <button |
| 118 | + className={`AutoLockTimer__option${isSaving ? " AutoLockTimer__option--disabled" : ""}`} |
| 119 | + onClick={() => handleSelect(minutes)} |
| 120 | + disabled={isSaving} |
| 121 | + aria-pressed={isSelected} |
| 122 | + data-testid={`autoLockOption-${minutes}`} |
| 123 | + > |
| 124 | + <span className="AutoLockTimer__option__label"> |
| 125 | + {formatTimeoutLabel(minutes, t)} |
| 126 | + </span> |
| 127 | + {isSelected && ( |
| 128 | + <Icon.Check className="AutoLockTimer__option__check" /> |
| 129 | + )} |
| 130 | + </button> |
| 131 | + </React.Fragment> |
| 132 | + ); |
| 133 | + })} |
| 134 | + </div> |
| 135 | + </View.Content> |
| 136 | + </React.Fragment> |
| 137 | + ); |
| 138 | +}; |
0 commit comments