207 lines
7.8 KiB
TypeScript
207 lines
7.8 KiB
TypeScript
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/<token> deep links (the site's /q page opens these). */
|
|
export default function ScanResultScreen() {
|
|
const { token } = useLocalSearchParams<{ token: string }>();
|
|
const [state, setState] = useState<State>({ 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<ScanResult>('/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 (
|
|
<SafeAreaView style={[styles.fill, { backgroundColor: colors.primary }]}>
|
|
<Image
|
|
source={require('../../../assets/pattern.png')}
|
|
style={[StyleSheet.absoluteFill, { width: '100%', height: '100%', opacity: 0.25 }]}
|
|
resizeMode="cover"
|
|
/>
|
|
<View style={styles.center}>
|
|
<Text style={[styles.kicker, { color: colors.secondary }]}>Mystery investee</Text>
|
|
<Text style={[styles.title, { color: colors.white }]}>You found our next investee</Text>
|
|
{result.checkpoint.organisation && (
|
|
<P style={{ color: colors.secondary, textAlign: 'center', fontSize: 17 }}>
|
|
{who(result.checkpoint)}
|
|
</P>
|
|
)}
|
|
<Animated.View
|
|
style={[styles.badge, { backgroundColor: colors.white, transform: [{ scale: pop }] }]}
|
|
>
|
|
<Text style={[s.num, { fontSize: 56, color: colors.primary }]}>
|
|
+{result.pointsAwarded}
|
|
</Text>
|
|
<Text style={{ fontFamily: fonts.bold, color: colors.primary }}>DOUBLE POINTS</Text>
|
|
</Animated.View>
|
|
<P style={{ color: colors.white }}>New total: {result.total} points</P>
|
|
</View>
|
|
<View style={styles.actions}>
|
|
<Button title="Done" variant="light" onPress={close} />
|
|
</View>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView style={[styles.fill, { backgroundColor: colors.whiteSoft }]}>
|
|
<View style={styles.center}>
|
|
{state.kind === 'loading' && <ActivityIndicator size="large" color={colors.primary} />}
|
|
|
|
{state.kind === 'ok' && (
|
|
<>
|
|
<View style={[styles.circle, { backgroundColor: colors.primary }]}>
|
|
<Icon name="check" color={colors.white} size={44} />
|
|
</View>
|
|
<Text style={styles.kicker}>
|
|
{state.result.checkpoint.kind === 'BOOTH' ? 'Booth checked in' : 'Investee found'}
|
|
</Text>
|
|
<Text style={[styles.title, { fontFamily: fonts.bold }]}>
|
|
{state.result.checkpoint.organisation ?? state.result.checkpoint.label}
|
|
</Text>
|
|
<P muted style={{ textAlign: 'center' }}>
|
|
{who(state.result.checkpoint)}
|
|
</P>
|
|
<Animated.View
|
|
style={[
|
|
styles.badge,
|
|
{ backgroundColor: colors.surfaceMuted, transform: [{ scale: pop }] },
|
|
]}
|
|
>
|
|
<Text style={[s.num, { fontSize: 44, color: colors.primary }]}>
|
|
+{state.result.pointsAwarded}
|
|
</Text>
|
|
<Text style={{ fontFamily: fonts.semibold, color: colors.primary }}>points</Text>
|
|
</Animated.View>
|
|
<P>New total: {state.result.total} points</P>
|
|
</>
|
|
)}
|
|
|
|
{state.kind === 'already' && (
|
|
<>
|
|
<View style={[styles.circle, { backgroundColor: colors.secondary }]}>
|
|
<Icon name="check" color={colors.ink} size={44} />
|
|
</View>
|
|
<Text style={styles.title}>Already scanned</Text>
|
|
<P muted style={{ textAlign: 'center' }}>
|
|
{state.checkpoint
|
|
? `You already collected points for ${who(state.checkpoint)}.`
|
|
: 'You already collected points for this code.'}
|
|
</P>
|
|
</>
|
|
)}
|
|
|
|
{state.kind === 'unknown' && (
|
|
<>
|
|
<View style={[styles.circle, { backgroundColor: colors.grey3 }]}>
|
|
<Icon name="help" color={colors.ink} size={44} />
|
|
</View>
|
|
<Text style={styles.title}>Not a GRV code</Text>
|
|
<P muted style={{ textAlign: 'center' }}>
|
|
This QR isn't one of our booth or investee codes. Ticket QRs are scanned by staff at
|
|
the entrance.
|
|
</P>
|
|
</>
|
|
)}
|
|
|
|
{state.kind === 'error' && (
|
|
<>
|
|
<Text style={styles.title}>Something went wrong</Text>
|
|
<P style={{ color: colors.destructive, textAlign: 'center' }}>{state.message}</P>
|
|
</>
|
|
)}
|
|
</View>
|
|
|
|
{state.kind !== 'loading' && (
|
|
<View style={styles.actions}>
|
|
<Button title="Scan another" onPress={scanAgain} />
|
|
<Button title="Done" variant="outline" onPress={close} />
|
|
</View>
|
|
)}
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
fill: { flex: 1 },
|
|
center: { flex: 1, alignItems: 'center', justifyContent: 'center', padding: 24, gap: 12 },
|
|
actions: { padding: 20, gap: 10 },
|
|
circle: {
|
|
width: 88,
|
|
height: 88,
|
|
borderRadius: 44,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
marginBottom: 8,
|
|
},
|
|
kicker: {
|
|
fontFamily: fonts.semibold,
|
|
fontSize: 13,
|
|
letterSpacing: 1,
|
|
textTransform: 'uppercase',
|
|
color: colors.grey1,
|
|
},
|
|
title: { fontFamily: fonts.display, fontSize: 30, color: colors.ink, textAlign: 'center' },
|
|
badge: {
|
|
alignItems: 'center',
|
|
borderRadius: 24,
|
|
paddingHorizontal: 32,
|
|
paddingVertical: 16,
|
|
marginVertical: 8,
|
|
},
|
|
});
|