mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-25 07:38:17 +03:00
71 lines
2.5 KiB
TypeScript
71 lines
2.5 KiB
TypeScript
import * as Clipboard from 'expo-clipboard';
|
|
import {FlatList, View} from "react-native";
|
|
import React, {useCallback, useMemo} from "react";
|
|
|
|
import {AMCard} from "../models/AMCard";
|
|
import {AimeCard} from "../components/AimeCard";
|
|
import RealmContext from "../models/RealmContext";
|
|
import ContextMenu from "react-native-context-menu-view";
|
|
import {EmptyStatePlaceholder} from "../components/EmptyStatePlaceholder";
|
|
import {CreditCard, Plus} from "lucide-react-native";
|
|
import {Button, useTheme} from "react-native-paper";
|
|
|
|
const {useQuery, useRealm} = RealmContext;
|
|
|
|
function CardEntry(props: { card: AMCard }) {
|
|
const realm = useRealm();
|
|
const callback = useCallback(async (index: number) => {
|
|
if (!props.card.isValid()) {
|
|
return;
|
|
}
|
|
|
|
switch (index) {
|
|
// copy card no
|
|
case 0:
|
|
await Clipboard.setStringAsync(props.card.code);
|
|
break;
|
|
|
|
case 1:
|
|
realm.write(() => {
|
|
realm.delete(props.card);
|
|
});
|
|
break;
|
|
}
|
|
}, [realm, props.card]);
|
|
|
|
return (
|
|
<ContextMenu
|
|
onPress={e => callback(e.nativeEvent?.index)}
|
|
actions={[
|
|
{title: "Copy Card Number", systemIcon: "doc.on.doc"},
|
|
{title: "Delete Card", destructive: true, systemIcon: "trash"}]}>
|
|
<AimeCard card={props.card} showFullCardNumber/>
|
|
</ContextMenu>
|
|
);
|
|
}
|
|
|
|
export default function CardManager({navigation}) {
|
|
const query = useQuery(AMCard);
|
|
const cards = useMemo(() => query.sorted("added", true), [query]);
|
|
|
|
const theme = useTheme();
|
|
|
|
if (!cards.length) {
|
|
return (
|
|
<View style={{flex: 1, gap: 10, justifyContent: "center", alignItems: "center"}}>
|
|
<EmptyStatePlaceholder message="No cards added yet" icon={<CreditCard color="gray"/>}/>
|
|
<Button mode="text" onPress={() => navigation.navigate('add-card')} icon={() => <Plus size={18} color={theme.colors.primary}/>}>Add Card</Button>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<FlatList contentInsetAdjustmentBehavior="always"
|
|
style={{padding: 16, paddingTop: 24}}
|
|
data={cards}
|
|
keyExtractor={i => i._id.toString()}
|
|
contentContainerStyle={{paddingBottom: 40}}
|
|
ItemSeparatorComponent={() => <View style={{height: 15}}/>}
|
|
renderItem={i => <CardEntry card={i.item}/>}/>
|
|
)
|
|
} |