import { Stack, useLocalSearchParams, router } from 'expo-router';
import { useMemo, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { fetchRecipeById } from '@/src/api/recipes.api';
import { Button } from '@/src/components/Button';
import { ErrorView } from '@/src/components/ErrorView';
import { Screen } from '@/src/components/Screen';
import { useAppearance } from '@/src/context/AppearanceContext';
import { categoryLabel } from '@/src/utils/recipe';
import { nextSort, sortBy, sortIndicator, type SortDirection } from '@/src/utils/sort';
type IngredientSortColumn = 'name' | 'amount';
export default function RecipeDetailScreen() {
const { id } = useLocalSearchParams<{ id: string }>();
const recipeId = Number(id);
const { t } = useTranslation();
const { colors } = useAppearance();
const [ingredientSort, setIngredientSort] = useState<{
column: IngredientSortColumn;
direction: SortDirection;
}>({ column: 'name', direction: 'asc' });
const recipeQuery = useQuery({
queryKey: ['recipe', recipeId],
queryFn: () => fetchRecipeById(recipeId),
enabled: Number.isFinite(recipeId),
});
const sortedIngredients = useMemo(() => {
if (!recipeQuery.data) return [];
const { column, direction } = ingredientSort;
return sortBy(recipeQuery.data.ingredients, direction, (ing) =>
column === 'name' ? ing.name : ing.amountGrams,
);
}, [recipeQuery.data, ingredientSort]);
if (!Number.isFinite(recipeId)) {
return (
);
}
if (recipeQuery.isLoading) {
return ;
}
if (recipeQuery.isError || !recipeQuery.data) {
return (
recipeQuery.refetch()}
/>
);
}
const recipe = recipeQuery.data;
return (
<>
>
);
}
function SortChip({ label, active, onPress }: { label: string; active: boolean; onPress: () => void }) {
const { colors } = useAppearance();
return (
{label}
);
}
const styles = StyleSheet.create({
metaCard: {
borderWidth: 1,
borderRadius: 14,
padding: 14,
gap: 8,
},
category: {
fontSize: 14,
fontWeight: '700',
textTransform: 'uppercase',
},
metaRow: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 12,
},
macros: {
gap: 4,
},
section: {
fontSize: 18,
fontWeight: '700',
marginTop: 8,
},
sortRow: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 8,
marginBottom: 8,
},
sortChip: {
borderWidth: 1,
borderRadius: 999,
paddingHorizontal: 12,
paddingVertical: 6,
},
line: {
fontSize: 15,
lineHeight: 22,
},
step: {
fontSize: 15,
lineHeight: 22,
marginBottom: 6,
},
});