import React, { useState } from "react"; import { View, Text, TouchableOpacity, StyleSheet, Modal, Platform, ScrollView, TextInput, } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import { useThings } from "@/state/use-thing"; import { useI18n } from "@/hooks/use-i18n"; import { useThemeContext } from "@/hooks/use-theme-context"; interface ShipOption { id: string; shipName: string; } interface ShipDropdownProps { value: ShipOption | null; onChange: (value: ShipOption | null) => void; } export default function ShipDropdown({ value, onChange }: ShipDropdownProps) { const { t } = useI18n(); const { colors } = useThemeContext(); const [isOpen, setIsOpen] = useState(false); const [searchText, setSearchText] = useState(""); const { things } = useThings(); // Convert things to ship options, filter out items without id const shipOptions: ShipOption[] = things ?.filter((thing) => thing.id != null) .map((thing) => ({ id: thing.id as string, shipName: thing.metadata?.ship_name || "", })) || []; // Filter ships based on search text const filteredShips = shipOptions.filter((ship) => { const searchLower = searchText.toLowerCase(); return ship.shipName.toLowerCase().includes(searchLower); }); const handleSelect = (ship: ShipOption | null) => { onChange(ship); setIsOpen(false); setSearchText(""); }; const displayValue = value ? value.shipName : t("diary.shipDropdown.placeholder"); const themedStyles = { label: { color: colors.text }, selector: { backgroundColor: colors.card, borderColor: colors.border }, selectorText: { color: colors.text }, placeholder: { color: colors.textSecondary }, modalContent: { backgroundColor: colors.card }, searchContainer: { backgroundColor: colors.backgroundSecondary, borderColor: colors.border }, searchInput: { color: colors.text }, option: { borderBottomColor: colors.separator }, selectedOption: { backgroundColor: colors.backgroundSecondary }, optionText: { color: colors.text }, emptyText: { color: colors.textSecondary }, }; return ( {t("diary.shipDropdown.label")} setIsOpen(true)} activeOpacity={0.7} > {displayValue} setIsOpen(false)} > setIsOpen(false)} > true} > {/* Search Input */} {searchText.length > 0 && ( setSearchText("")}> )} {/* "All Ships" option */} handleSelect(null)} > {t("diary.shipDropdown.allShips")} {!value && ( )} {/* Filtered ship options */} {filteredShips.length > 0 ? ( filteredShips.map((ship) => ( handleSelect(ship)} > {ship.shipName} {value?.id === ship.id && ( )} )) ) : ( {t("diary.shipDropdown.noShipsFound")} )} ); } const styles = StyleSheet.create({ container: { marginBottom: 20, }, label: { fontSize: 16, fontWeight: "600", marginBottom: 8, fontFamily: Platform.select({ ios: "System", android: "Roboto", default: "System", }), }, selector: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", borderWidth: 1, borderRadius: 8, paddingHorizontal: 16, paddingVertical: 12, }, selectorText: { fontSize: 16, flex: 1, fontFamily: Platform.select({ ios: "System", android: "Roboto", default: "System", }), }, modalOverlay: { flex: 1, backgroundColor: "rgba(0, 0, 0, 0.5)", justifyContent: "center", alignItems: "center", }, modalContent: { borderRadius: 12, width: "85%", maxHeight: "70%", overflow: "hidden", shadowColor: "#000", shadowOffset: { width: 0, height: 4, }, shadowOpacity: 0.3, shadowRadius: 8, elevation: 8, }, searchContainer: { flexDirection: "row", alignItems: "center", paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: 1, }, searchIcon: { marginRight: 8, }, searchInput: { flex: 1, fontSize: 16, padding: 0, fontFamily: Platform.select({ ios: "System", android: "Roboto", default: "System", }), }, optionsList: { maxHeight: 350, }, option: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", paddingHorizontal: 20, paddingVertical: 16, borderBottomWidth: 1, }, optionText: { fontSize: 16, fontFamily: Platform.select({ ios: "System", android: "Roboto", default: "System", }), }, emptyContainer: { paddingVertical: 24, alignItems: "center", }, emptyText: { fontSize: 14, fontFamily: Platform.select({ ios: "System", android: "Roboto", default: "System", }), }, });