Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import { useState } from 'react';
import { Dimensions, Image, Alert } from 'react-native';
import { Dialog, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
import { useRouter } from 'expo-router';
import { Heart, Trash2 } from '@tamagui/lucide-icons';
import { Colors } from '@/src/constants/theme';
import { useThemeAwareColorScheme } from '@/src/hooks/useThemeAwareColorScheme';
import type { Pet } from '@/src/schemas/pet.schema';
interface PetDetailModalProps {
pet: Pet | null;
open: boolean;
onOpenChange: (open: boolean) => void;
onDelete?: (petId: number) => Promise<void>;
}
export function PetDetailModal({ pet, open, onOpenChange, onDelete }: PetDetailModalProps) {
const colorScheme = useThemeAwareColorScheme();
const colors = Colors[colorScheme];
const router = useRouter();
const [deleting, setDeleting] = useState(false);
if (!pet) return null;
const handleOpenHealth = () => {
onOpenChange(false);
router.push({
pathname: '/(tabs)/profile/pet-health',
params: {
petId: pet.id.toString(),
petName: pet.name,
},
});
};
const handleDelete = () => {
Alert.alert('确认删除', `确定要删除 ${pet.name} 吗?此操作无法撤销。`, [
{
text: '取消',
style: 'cancel',
},
{
text: '删除',
style: 'destructive',
onPress: async () => {
if (!onDelete) return;
try {
setDeleting(true);
await onDelete(pet.id);
onOpenChange(false);
} catch (error) {
// 错误已在 Hook 中处理
} finally {
setDeleting(false);
}
},
},
]);
};
const screenWidth = Dimensions.get('window').width;
const dialogWidth = Math.min(screenWidth - 48, 400);
return (
<Dialog modal open={open} onOpenChange={onOpenChange}>
<Dialog.Portal>
<Dialog.Overlay
key="overlay"
animation="quick"
opacity={0.5}
enterStyle={{ opacity: 0 }}
exitStyle={{ opacity: 0 }}
/>
<Dialog.Content
bordered
key="content"
animateOnly={['transform', 'opacity']}
animation={[
'quick',
{
opacity: {
overshootClamping: true,
},
},
]}
enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }}
exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }}
gap="$4"
padding="$4"
backgroundColor={colors.background}
width={dialogWidth}
>
<Dialog.Title fontSize={22} fontWeight="700" color={colors.text}>
{pet.name}
</Dialog.Title>
<YStack gap="$3" alignItems="center">
{pet.photo_url ? (
<Image
source={{ uri: pet.photo_url }}
style={{
width: 200,
height: 200,
borderRadius: 16,
backgroundColor: colors.icon + '20',
}}
resizeMode="cover"
/>
) : (
<YStack
width={200}
height={200}
borderRadius={16}
backgroundColor="$orange3"
alignItems="center"
justifyContent="center"
>
<Text fontSize={120}>🐱</Text>
</YStack>
)}
<YStack gap="$2" width="100%">
<XStack gap="$2">
<Text fontSize={16} fontWeight="600" color={colors.text}>
种类:
</Text>
<Text fontSize={16} color={colors.icon}>
{pet.species_display ?? pet.species}
</Text>
</XStack>
{pet.age != null && (
<XStack gap="$2">
<Text fontSize={16} fontWeight="600" color={colors.text}>
年龄:
</Text>
<Text fontSize={16} color={colors.icon}>
{pet.age}岁
</Text>
</XStack>
)}
{pet.breed && (
<XStack gap="$2">
<Text fontSize={16} fontWeight="600" color={colors.text}>
品种:
</Text>
<Text fontSize={16} color={colors.icon}>
{pet.breed}
</Text>
</XStack>
)}
{pet.description && (
<YStack gap="$2">
<Text fontSize={16} fontWeight="600" color={colors.text}>
简介:
</Text>
<Text fontSize={14} color={colors.icon} lineHeight={20}>
{pet.description}
</Text>
</YStack>
)}
</YStack>
</YStack>
{/* 快捷操作按钮 */}
<Button
fullWidth
variant="outline"
leftIcon={<Heart size={18} />}
onPress={handleOpenHealth}
>
健康档案 & 体重记录
</Button>
<XStack justifyContent="flex-end" gap="$2">
<Dialog.Close displayWhenAdapted asChild>
<Button variant="outline" onPress={() => onOpenChange(false)} disabled={deleting}>
关闭
</Button>
</Dialog.Close>
</XStack>
{/* 删除按钮 - 放在最下方,与上面内容分隔 */}
{onDelete && (
<>
<YStack height={1} backgroundColor="$gray5" marginVertical="$2" />
<Button
fullWidth
variant="danger"
leftIcon={<Trash2 size={18} />}
onPress={handleDelete}
loading={deleting}
disabled={deleting}
>
删除宠物
</Button>
</>
)}
</Dialog.Content>
</Dialog.Portal>
</Dialog>
);
}
|