import { Ionicons } from "@expo/vector-icons"; import { BottomSheetBackdrop, type BottomSheetBackdropProps, BottomSheetModal, BottomSheetView, } from "@gorhom/bottom-sheet"; import React, { useCallback, useEffect, useMemo, useRef } from "react"; import { useTranslation } from "react-i18next"; import { StyleSheet, TouchableOpacity, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { Text } from "@/components/common/Text"; export type PlaylistSortOption = "SortName" | "DateCreated"; export type PlaylistSortOrder = "Ascending" | "Descending"; interface Props { open: boolean; setOpen: (open: boolean) => void; sortBy: PlaylistSortOption; sortOrder: PlaylistSortOrder; onSortChange: ( sortBy: PlaylistSortOption, sortOrder: PlaylistSortOrder, ) => void; } const SORT_OPTIONS: { key: PlaylistSortOption; label: string; icon: string }[] = [ { key: "SortName", label: "music.sort.alphabetical", icon: "text-outline" }, { key: "DateCreated", label: "music.sort.date_created", icon: "time-outline", }, ]; export const PlaylistSortSheet: React.FC = ({ open, setOpen, sortBy, sortOrder, onSortChange, }) => { const bottomSheetModalRef = useRef(null); const insets = useSafeAreaInsets(); const { t } = useTranslation(); const snapPoints = useMemo(() => ["40%"], []); useEffect(() => { if (open) bottomSheetModalRef.current?.present(); else bottomSheetModalRef.current?.dismiss(); }, [open]); const handleSheetChanges = useCallback( (index: number) => { if (index === -1) { setOpen(false); } }, [setOpen], ); const renderBackdrop = useCallback( (props: BottomSheetBackdropProps) => ( ), [], ); const handleSortSelect = useCallback( (option: PlaylistSortOption) => { // If selecting same option, toggle order; otherwise use sensible default if (option === sortBy) { onSortChange( option, sortOrder === "Ascending" ? "Descending" : "Ascending", ); } else { // Default order based on sort type const defaultOrder: PlaylistSortOrder = option === "SortName" ? "Ascending" : "Descending"; onSortChange(option, defaultOrder); } setOpen(false); }, [sortBy, sortOrder, onSortChange, setOpen], ); return ( {t("music.sort.title")} {SORT_OPTIONS.map((option, index) => { const isSelected = sortBy === option.key; return ( {index > 0 && } handleSortSelect(option.key)} className='flex-row items-center px-4 py-3.5' > {t(option.label)} {isSelected && ( )} ); })} ); }; const styles = StyleSheet.create({ separator: { height: StyleSheet.hairlineWidth, backgroundColor: "#404040", }, });