import * as Haptics from 'expo-haptics'; import { router, useLocalSearchParams } from 'expo-router'; import { useEffect, useRef, useState } from 'react'; import { ActivityIndicator, Animated, Image, StyleSheet, Text, View } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { Button, Icon, P, s } from '../../components/ui'; import { api, ApiError, invalidateAll, type Checkpoint, type ScanResult } from '../../lib/api'; import { colors, fonts } from '../../lib/theme'; type State = | { kind: 'loading' } | { kind: 'ok'; result: ScanResult } | { kind: 'already'; checkpoint: Checkpoint | null } | { kind: 'unknown' } | { kind: 'error'; message: string }; const haptic = (type: Haptics.NotificationFeedbackType) => void Haptics.notificationAsync(type).catch(() => undefined); const who = (c: Checkpoint) => c.kind === 'BOOTH' ? [c.organisation ?? c.label, c.boothNumber && `Booth ${c.boothNumber}`] .filter(Boolean) .join(' · ') : [c.personName, c.label, c.organisation].filter(Boolean).join(' · '); /** Reached from the Scan tab and from grvsummit://q/ deep links (the site's /q page opens these). */ export default function ScanResultScreen() { const { token } = useLocalSearchParams<{ token: string }>(); const [state, setState] = useState({ kind: 'loading' }); const sent = useRef(false); const pop = useRef(new Animated.Value(0.6)).current; useEffect(() => { if (sent.current || !token) return; sent.current = true; // one QR → one request, even under StrictMode double effects api('/scan', { body: { token } }) .then((result) => { setState({ kind: 'ok', result }); haptic(Haptics.NotificationFeedbackType.Success); if (result.mystery) void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy).catch(() => undefined); Animated.spring(pop, { toValue: 1, friction: 4, useNativeDriver: true }).start(); void invalidateAll(); }) .catch((e: unknown) => { if (e instanceof ApiError && e.status === 409) { setState({ kind: 'already', checkpoint: (e.body?.checkpoint as Checkpoint | undefined) ?? null, }); haptic(Haptics.NotificationFeedbackType.Warning); } else if (e instanceof ApiError && e.status === 404) { setState({ kind: 'unknown' }); haptic(Haptics.NotificationFeedbackType.Error); } else { setState({ kind: 'error', message: (e as Error).message }); haptic(Haptics.NotificationFeedbackType.Error); } }); }, [token, pop]); const close = () => (router.canGoBack() ? router.back() : router.replace('/')); const scanAgain = () => (router.canGoBack() ? router.back() : router.replace('/scan')); if (state.kind === 'ok' && state.result.mystery) { const { result } = state; return ( Mystery investee You found our next investee {result.checkpoint.organisation && (

{who(result.checkpoint)}

)} +{result.pointsAwarded} DOUBLE POINTS

New total: {result.total} points