All files / apps/host/src/screens/Profile Settings.tsx

92.85% Statements 13/14
100% Branches 9/9
66.66% Functions 2/3
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                            2x 17x 17x           17x     17x 17x 17x 17x   17x 2x     15x                                                                                                                                                                     17x                                                                                                            
import { ActivityIndicator, Modal, Switch, Text, TouchableOpacity, View } from 'react-native';
 
import { LogoutIcon } from '@repo/ui/icons/Logout';
import { NotificationIcon } from '@repo/ui/icons/Notification';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
 
import { useSettingsLogout, useSettingsNotification } from './hooks';
 
interface SettingsProps {
  isActive?: boolean;
}
 
export const Settings = ({ isActive = true }: SettingsProps) => {
  const { theme } = useTheme();
  const styles = useStyles();
  const {
    isNotificationEnabled,
    isNotificationStateHydrating,
    isNotificationToggleLoading,
    handleNotificationToggle,
  } = useSettingsNotification({
    isActive,
  });
  const { isLogoutProcessing, handleLogout } = useSettingsLogout();
  const switchScaleX = theme.metrics.spacing[13] / 51;
  const switchScaleY = theme.metrics.spacing[7] / 31;
  const isSwitchDisabled = isNotificationToggleLoading || isNotificationStateHydrating;
 
  if (!isActive) {
    return <View style={styles.container} />;
  }
 
  return (
    <>
      <View style={styles.container}>
        <View style={styles.menuSection}>
          <View style={styles.menuItem}>
            <View style={styles.menuLeft}>
              <NotificationIcon
                width={theme.metrics.spacing[6.5]}
                height={theme.metrics.spacing[6.5]}
                color={theme.colors.text.heading}
              />
              <Text style={styles.menuLabel}>Notification</Text>
            </View>
 
            <View style={styles.switchWrap}>
              {isNotificationStateHydrating ? (
                <View
                  style={styles.switchLoading}
                  accessibilityRole="progressbar"
                  accessibilityLabel="Loading notification setting"
                >
                  <ActivityIndicator size="small" color={theme.colors.icon.active} />
                </View>
              ) : (
                <Switch
                  style={[
                    styles.switch,
                    { transform: [{ scaleX: switchScaleX }, { scaleY: switchScaleY }] },
                  ]}
                  value={isNotificationEnabled}
                  onValueChange={handleNotificationToggle}
                  disabled={isSwitchDisabled}
                  trackColor={{
                    false: theme.colors.badge.light,
                    true: theme.colors.background.selectedStrong,
                  }}
                  thumbColor={theme.colors.text.white}
                  ios_backgroundColor={theme.colors.badge.light}
                  accessibilityLabel="Notification"
                  accessibilityHint="Toggles notifications"
                />
              )}
            </View>
          </View>
 
          <TouchableOpacity
            style={styles.menuItem}
            onPress={handleLogout}
            accessibilityRole="button"
            accessibilityLabel="Log out"
            accessibilityHint="Logs you out of the application"
          >
            <View style={styles.menuLeft}>
              <LogoutIcon
                width={theme.metrics.spacing[6.5]}
                height={theme.metrics.spacing[6.5]}
                color={theme.colors.text.heading}
              />
              <Text style={styles.menuLabel}>Log out</Text>
            </View>
          </TouchableOpacity>
        </View>
      </View>
 
      {isLogoutProcessing ? (
        <Modal transparent visible statusBarTranslucent onRequestClose={() => undefined}>
          <View
            style={styles.logoutOverlay}
            accessibilityRole="progressbar"
            accessibilityLabel="Logging out"
          >
            <ActivityIndicator
              testID="logout-fullscreen-loading-indicator"
              size="large"
              color={theme.colors.icon.active}
            />
          </View>
        </Modal>
      ) : null}
    </>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    width: theme.metrics.sizing.full,
    backgroundColor: theme.colors.background.pageMuted,
    borderRadius: 0,
    paddingTop: theme.metrics.spacing[1],
    paddingHorizontal: theme.metrics.spacing[2.5],
    paddingBottom: theme.metrics.spacing[5],
  },
  menuSection: {
    marginTop: theme.metrics.spacing[8],
    gap: theme.metrics.spacing[2.5],
  },
  menuItem: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    padding: theme.metrics.spacing[4],
  },
  menuLeft: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: theme.metrics.spacing[2],
  },
  menuLabel: {
    ...textStyles.content.semiBold,
    fontSize: theme.metrics.textSize[20],
    color: theme.colors.text.heading,
  },
  switchWrap: {
    width: theme.metrics.spacing[13],
    height: theme.metrics.spacing[7],
    alignItems: 'center',
    justifyContent: 'center',
  },
  switchLoading: {
    width: theme.metrics.sizing.full,
    height: theme.metrics.sizing.full,
    borderRadius: theme.metrics.borderRadius.circle,
    backgroundColor: theme.colors.badge.light,
    alignItems: 'center',
    justifyContent: 'center',
  },
  switch: {
    margin: theme.metrics.spacing[0],
  },
  logoutOverlay: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: `${theme.colors.gray70}59`,
  },
}));