Skip to content

Colota - Android GPS Location tracker

4.7 KB
Raw Blame
/**
 * Copyright (C) 2026 Max Dietrich
 * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
 */

import React, { useState, useRef, useEffect, useCallback } from "react"
import { Modal, View, Text, Pressable, StyleSheet, BackHandler } from "react-native"
import { useTheme } from "../../hooks/useTheme"
import { fontSizes, fonts, lineHeights, type } from "../../styles/typography"
import { radius } from "@colota/shared"
import { size, space, STATE_LAYER_ALPHA, elevation } from "../../constants"
import { useTranslation } from "../../i18n/useTranslation"

interface DisclosureModalProps {
  icon: React.ReactNode
  title: string
  paragraphs: string[]
  confirmLabel: string
  registerCallback: (cb: () => Promise<boolean>) => void
}

/**
 * Reusable themed disclosure modal.
 *
 * Renders a centered card with an icon, title, body paragraphs,
 * and "Not Now" / confirm buttons. The caller registers a callback
 * that, when invoked, shows the modal and resolves with the user's choice.
 */
export function DisclosureModal({ icon, title, paragraphs, confirmLabel, registerCallback }: DisclosureModalProps) {
  const { colors } = useTheme()
  const { t } = useTranslation()
  const [visible, setVisible] = useState(false)
  const resolveRef = useRef<((value: boolean) => void) | null>(null)

  useEffect(() => {
    registerCallback(() => {
      return new Promise<boolean>((resolve) => {
        resolveRef.current = resolve
        setVisible(true)
      })
    })
  }, [registerCallback])

  // Block hardware back button while visible
  useEffect(() => {
    if (!visible) return
    const handler = BackHandler.addEventListener("hardwareBackPress", () => true)
    return () => handler.remove()
  }, [visible])

  const handleConfirm = useCallback(() => {
    setVisible(false)
    resolveRef.current?.(true)
    resolveRef.current = null
  }, [])

  const handleNotNow = useCallback(() => {
    setVisible(false)
    resolveRef.current?.(false)
    resolveRef.current = null
  }, [])

  return (
    <Modal visible={visible} transparent animationType="fade" statusBarTranslucent>
      <View style={[styles.overlay, { backgroundColor: colors.overlay }]}>
        <View style={[styles.card, { backgroundColor: colors.card, borderRadius: radius.lg }]}>
          {/* Icon */}
          <View style={[styles.iconContainer, { backgroundColor: colors.primary + "15" }]}>{icon}</View>

          {/* Title */}
          <Text style={[styles.title, { color: colors.text }]}>{title}</Text>

          {/* Body */}
          {paragraphs.map((text, i) => (
            <Text key={i} style={[styles.body, i > 0 && styles.bodySpaced, { color: colors.textSecondary }]}>
              {text}
            </Text>
          ))}

          {/* Buttons */}
          <View style={styles.buttons}>
            <Pressable
              accessibilityRole="button"
              android_ripple={{ color: colors.textSecondary + STATE_LAYER_ALPHA }}
              style={styles.button}
              onPress={handleNotNow}
            >
              <Text style={[styles.buttonText, { color: colors.textSecondary }]}>{t("disclosure.notNow")}</Text>
            </Pressable>

            <Pressable
              accessibilityRole="button"
              android_ripple={{ color: colors.textOnPrimary + STATE_LAYER_ALPHA }}
              style={[styles.button, { backgroundColor: colors.primary }]}
              onPress={handleConfirm}
            >
              <Text style={[styles.buttonText, { color: colors.textOnPrimary }]}>{confirmLabel}</Text>
            </Pressable>
          </View>
        </View>
      </View>
    </Modal>
  )
}

const styles = StyleSheet.create({
  overlay: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    paddingHorizontal: space.xxl
  },
  card: {
    width: "100%",
    padding: space.xl,
    elevation: elevation.overlay
  },
  iconContainer: {
    width: size.emptyIcon,
    height: size.emptyIcon,
    borderRadius: radius.pill,
    justifyContent: "center",
    alignItems: "center",
    alignSelf: "center",
    marginBottom: space.lg
  },
  title: {
    ...type.title,
    textAlign: "center",
    marginBottom: space.lg
  },
  body: {
    fontSize: fontSizes.body,
    ...fonts.regular,
    lineHeight: lineHeights.body
  },
  bodySpaced: {
    marginTop: space.sm
  },
  buttons: {
    flexDirection: "row",
    gap: space.md,
    marginTop: space.xl
  },
  button: {
    flex: 1,
    // 14 was holding the 48 target by itself: on the scale that only works with the minimum stated
    minHeight: size.touch,
    paddingVertical: space.md,
    borderRadius: radius.sm,
    overflow: "hidden",
    alignItems: "center",
    justifyContent: "center"
  },
  buttonText: {
    fontSize: fontSizes.label,
    ...fonts.semiBold
  }
})