Skip to content

Colota - Android GPS Location tracker

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

import React, { useCallback, useRef, useState } from "react"
import { ScrollView, StyleSheet, Text, View } from "react-native"
import { useFocusEffect } from "@react-navigation/native"
import { Archive, ArchiveX, Download, FileLock, Upload } from "lucide-react-native"
import {
  Button,
  Card,
  Container,
  Divider,
  FieldMessage,
  ListItem,
  LoadingOverlay,
  SectionTitle,
  StateLine,
  TextField
} from "../components"
import { useTheme } from "../hooks/useTheme"
import BackupService, { type BackupManifest, type PasswordStrengthResult } from "../services/BackupService"
import NativeLocationService from "../services/NativeLocationService"
import { showChoice, showConfirm } from "../services/modalService"
import { logger } from "../utils/logger"
import {
  archiveLine,
  backupErrorMessage,
  backupExcludesLine,
  backupScopeLine,
  backupState,
  backupWrittenLine,
  noRecoveryLine,
  openFileLine,
  passwordLine,
  pickedNotOpenedCaption,
  restoreCaveat,
  restoreConfirm,
  restoreErrorMessage,
  restoreIdleLine,
  restoreOutcome,
  submitBlockedReason
} from "../utils/backupState"
import { fonts, fontSizes, lineHeights } from "../styles/typography"
import { space } from "../constants"
import type { ScreenProps } from "../types/global"
import { useTranslation } from "../i18n/useTranslation"
// Handlers use the non-hook t, so no callback list carries it.
import { t as translate } from "../i18n/t"

type Busy = "backup" | "open" | "restore" | null
type Picked = { uri: string; displayName: string }
type Message = { text: string; tone?: "warning" | "error" }

const EMPTY_STRENGTH: PasswordStrengthResult = { score: 0, label: "", bits: 0 }
const EMPTY_STATS = { total: 0, databaseSizeMB: 0 }

export function BackupRestoreScreen({}: ScreenProps) {
  const { colors } = useTheme()
  const { t } = useTranslation()

  const [stats, setStats] = useState(EMPTY_STATS)
  const [lastBackupAt, setLastBackupAt] = useState<number | null>(null)
  const [hasClientCert, setHasClientCert] = useState(false)
  const [loaded, setLoaded] = useState(false)

  const [password, setPassword] = useState("")
  const [confirm, setConfirm] = useState("")
  const [strength, setStrength] = useState<PasswordStrengthResult>(EMPTY_STRENGTH)
  const [strengthFailed, setStrengthFailed] = useState(false)
  const [backupMessage, setBackupMessage] = useState<Message | null>(null)

  const [picked, setPicked] = useState<Picked | null>(null)
  const [restorePassword, setRestorePassword] = useState("")
  const [manifest, setManifest] = useState<BackupManifest | null>(null)
  const [restoreMessage, setRestoreMessage] = useState<Message | null>(null)
  const [busy, setBusy] = useState<Busy>(null)

  // A ref, not the state, because two presses inside one render both read the same state value.
  const busyRef = useRef(false)

  const read = useCallback(async () => {
    const [statsResult, lastResult, certResult] = await Promise.allSettled([
      NativeLocationService.getStats(),
      NativeLocationService.getSetting("last_backup_at"),
      NativeLocationService.getClientCertInfo()
    ])
    if (statsResult.status === "fulfilled") {
      setStats({ total: statsResult.value.total, databaseSizeMB: statsResult.value.databaseSizeMB })
      setLoaded(true)
    }
    if (lastResult.status === "fulfilled") setLastBackupAt(Number(lastResult.value ?? 0) || null)
    if (certResult.status === "fulfilled") setHasClientCert(certResult.value !== null)
  }, [])

  useFocusEffect(
    useCallback(() => {
      read()
    }, [read])
  )

  const scoreDraft = useCallback(async (next: string) => {
    setPassword(next)
    try {
      setStrength(await BackupService.passwordStrength(next))
      setStrengthFailed(false)
    } catch (err) {
      logger.warn("[BackupRestoreScreen] passwordStrength failed", err)
      setStrengthFailed(true)
    }
  }, [])

  const blocked = submitBlockedReason(password, confirm, strength)

  const onCreateBackup = useCallback(async () => {
    if (busyRef.current || blocked) return
    if (
      !(await showConfirm({
        title: translate("backup.noReset.title"),
        message: `${noRecoveryLine()} ${translate("backup.noReset.store")}`,
        confirmText: translate("backup.noReset.confirm"),
        destructive: false
      }))
    ) {
      return
    }

    const uri = await BackupService.pickBackupDestination()
    if (!uri) return

    busyRef.current = true
    setBusy("backup")
    setBackupMessage(null)
    try {
      await BackupService.createBackup(uri, password)
      const now = Date.now()
      await NativeLocationService.saveSetting("last_backup_at", String(now)).catch(() => {})
      setLastBackupAt(now)
      setPassword("")
      setConfirm("")
      setStrength(EMPTY_STRENGTH)
      setBackupMessage({ text: backupWrittenLine() })
    } catch (e) {
      logger.error("[BackupRestoreScreen] backup failed", e)
      const err = e as { code?: string; message?: string }
      setBackupMessage({ text: backupErrorMessage(err.code, err.message), tone: "error" })
    } finally {
      busyRef.current = false
      setBusy(null)
    }
  }, [blocked, password])

  const onChooseFile = useCallback(async () => {
    if (busyRef.current) return
    const source = await BackupService.pickBackupSource()
    if (!source) return
    setPicked({ uri: source.uri, displayName: source.displayName ?? translate("backup.pickedFallback") })
    setManifest(null)
    setRestorePassword("")
    setRestoreMessage(null)
  }, [])

  const onOpenFile = useCallback(async () => {
    if (busyRef.current || !picked) return
    busyRef.current = true
    setBusy("open")
    setRestoreMessage(null)
    try {
      setManifest(await BackupService.describeBackup(picked.uri, restorePassword))
    } catch (e) {
      logger.error("[BackupRestoreScreen] describeBackup failed", e)
      const err = e as { code?: string; message?: string }
      setRestoreMessage({ text: restoreErrorMessage(err.code, err.message), tone: "error" })
    } finally {
      busyRef.current = false
      setBusy(null)
    }
  }, [picked, restorePassword])

  const onRestore = useCallback(async () => {
    if (busyRef.current || !picked || !manifest) return
    busyRef.current = true
    setBusy("restore")
    let outcome
    try {
      const fresh = await NativeLocationService.getStats()
      if (!(await showConfirm({ ...restoreConfirm(fresh.total, manifest), destructive: true }))) return
      await BackupService.restoreBackup(picked.uri, restorePassword)
      outcome = restoreOutcome(undefined)
    } catch (e) {
      logger.error("[BackupRestoreScreen] restore failed", e)
      const err = e as { code?: string; message?: string }
      outcome = restoreOutcome(err.code, err.message)
    } finally {
      busyRef.current = false
      setBusy(null)
    }

    if (!outcome) return
    if (outcome.kind === "failed") {
      setRestoreMessage({ text: outcome.message, tone: "error" })
      return
    }
    // Blocking: applyRestore reloads the bridge and would tear down an unawaited dialog.
    await showChoice({
      title: outcome.title,
      message: outcome.message,
      variant: outcome.variant === "warning" ? "warning" : "success",
      buttons: [{ text: translate("backup.restart"), style: "primary" }]
    })
    // The row lives in the settings table, so it arrived from inside the archive and is one backup stale.
    const madeAt = Date.parse(manifest.createdAt)
    if (Number.isFinite(madeAt)) {
      await NativeLocationService.saveSetting("last_backup_at", String(madeAt)).catch(() => {})
    }
    await BackupService.applyRestore()
  }, [picked, manifest, restorePassword])

  const opening = backupState(lastBackupAt, stats)
  const caveat = manifest ? restoreCaveat(hasClientCert) : null
  const excludes = backupExcludesLine(hasClientCert)
  const pwLine = passwordLine(strength, strengthFailed)

  return (
    <Container>
      <ScrollView contentContainerStyle={styles.scrollContent} keyboardShouldPersistTaps="handled">
        {loaded && (
          <Card rows style={styles.section}>
            <StateLine
              icon={opening.tone === "ok" ? Archive : ArchiveX}
              iconColor={
                opening.tone === "ok" ? colors.primary : opening.tone === "none" ? colors.warning : colors.textSecondary
              }
              label={opening.label}
              caption={opening.caption}
              testID="backup-state"
            />
          </Card>
        )}

        <View style={styles.section}>
          <SectionTitle>{t("backup.section.backup")}</SectionTitle>
          <Card testID="backup-form">
            <View style={styles.fields}>
              {loaded ? (
                <View>
                  <Text style={[styles.description, { color: colors.textSecondary }]}>
                    {backupScopeLine(stats.total)}
                  </Text>
                  {excludes ? (
                    <Text style={[styles.description, styles.descriptionNext, { color: colors.textSecondary }]}>
                      {excludes}
                    </Text>
                  ) : null}
                </View>
              ) : null}
              <View>
                <TextField
                  label={t("backup.password")}
                  secure
                  testID="backup-password"
                  value={password}
                  onChangeText={scoreDraft}
                  autoCapitalize="none"
                  autoCorrect={false}
                  disabled={busy !== null}
                />
                {password.length > 0 ? <FieldMessage variant={pwLine.variant}>{pwLine.text}</FieldMessage> : null}
              </View>
              <View>
                <TextField
                  label={t("backup.confirmPassword")}
                  secure
                  testID="backup-password-confirm"
                  value={confirm}
                  onChangeText={setConfirm}
                  autoCapitalize="none"
                  autoCorrect={false}
                  error={confirm.length > 0 && confirm !== password ? t("backup.blocked.mismatch") : undefined}
                  disabled={busy !== null}
                />
                {!(confirm.length > 0 && confirm !== password) && <FieldMessage>{noRecoveryLine()}</FieldMessage>}
              </View>
            </View>
          </Card>
          <Button
            variant="primary"
            icon={Upload}
            title={t("backup.create")}
            testID="create-backup-btn"
            loading={busy === "backup"}
            disabled={busy !== null || blocked !== null}
            onPress={onCreateBackup}
          />
          {backupMessage ? (
            <FieldMessage variant={backupMessage.tone === "error" ? "error" : "info"}>
              {backupMessage.text}
            </FieldMessage>
          ) : blocked ? (
            <FieldMessage>{blocked}</FieldMessage>
          ) : null}
        </View>

        <View style={styles.section}>
          <SectionTitle>{t("backup.section.restore")}</SectionTitle>

          {!picked && (
            <Card rows>
              <ListItem
                testID="choose-backup-row"
                icon={FileLock}
                trailingIcon={Download}
                label={t("backup.choose")}
                sub={restoreIdleLine()}
                subLines={2}
                accessibilityHint={t("backup.choose.hint")}
                disabled={busy !== null}
                onPress={onChooseFile}
              />
            </Card>
          )}

          {picked && !manifest && (
            <>
              <Card>
                <StateLine
                  icon={FileLock}
                  iconColor={colors.textSecondary}
                  label={picked.displayName}
                  caption={pickedNotOpenedCaption()}
                  testID="picked-file"
                />
                <Divider tight />
                <View style={styles.pickedField}>
                  <TextField
                    label={t("backup.restorePassword")}
                    secure
                    testID="restore-password"
                    value={restorePassword}
                    onChangeText={setRestorePassword}
                    autoCapitalize="none"
                    autoCorrect={false}
                    error={restoreMessage?.tone === "error" ? restoreMessage.text : undefined}
                    disabled={busy !== null}
                  />
                  {restoreMessage?.tone !== "error" && <FieldMessage>{openFileLine()}</FieldMessage>}
                </View>
              </Card>
              <Button
                variant="secondary"
                title={t("backup.open")}
                testID="open-backup-btn"
                loading={busy === "open"}
                disabled={busy !== null || restorePassword.length === 0}
                onPress={onOpenFile}
              />
              <Button
                variant="ghost"
                title={t("backup.chooseDifferent")}
                testID="repick-backup-btn"
                onPress={onChooseFile}
              />
            </>
          )}

          {picked && manifest && (
            <>
              <Card rows>
                <StateLine
                  icon={Archive}
                  iconColor={colors.primary}
                  label={archiveLine(manifest).label}
                  caption={archiveLine(manifest).caption}
                  testID="archive-state"
                />
                {caveat ? (
                  <View style={styles.caveat}>
                    <FieldMessage variant="warning">{caveat}</FieldMessage>
                  </View>
                ) : null}
              </Card>
              <Button
                variant="danger"
                title={t("backup.replaceAll")}
                testID="restore-btn"
                loading={busy === "restore"}
                disabled={busy !== null}
                onPress={onRestore}
              />
              <Button
                variant="ghost"
                title={t("backup.chooseDifferent")}
                testID="repick-backup-btn"
                onPress={onChooseFile}
              />
              {restoreMessage?.tone === "error" && <FieldMessage variant="error">{restoreMessage.text}</FieldMessage>}
            </>
          )}
        </View>
      </ScrollView>

      <LoadingOverlay
        visible={busy === "backup" || busy === "open" || busy === "restore"}
        title={busy === "backup" ? t("backup.writing") : busy === "open" ? t("backup.opening") : t("backup.restoring")}
        message={busy === "restore" ? t("backup.keepOpen") : ""}
      />
    </Container>
  )
}

const styles = StyleSheet.create({
  scrollContent: {
    paddingHorizontal: space.lg,
    paddingTop: space.lg,
    paddingBottom: space.xxl
  },
  section: {
    marginBottom: space.xl
  },
  fields: {
    gap: space.lg
  },
  pickedField: {
    paddingTop: space.lg
  },
  description: {
    fontSize: fontSizes.description,
    ...fonts.regular,
    lineHeight: lineHeights.description
  },
  descriptionNext: {
    marginTop: space.sm
  },
  caveat: {
    paddingBottom: space.lg
  }
})