|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useQuery } from "@tanstack/react-query"; |
| 4 | +import { AnimatePresence, motion } from "framer-motion"; |
| 5 | +import { type ReactNode, useCallback, useEffect, useState } from "react"; |
| 6 | +import { useForm } from "react-hook-form"; |
| 7 | +import { useDebounce } from "react-simplikit"; |
| 8 | + |
| 9 | +import CircleCloseIcon from "@/assets/circle-close.svg"; |
| 10 | +import SearchIcon from "@/assets/search.svg"; |
| 11 | +import { BottomSheet } from "@/components/ui/BottomSheet"; |
| 12 | +import { Button } from "@/components/ui/Button"; |
| 13 | +import { Skeleton } from "@/components/ui/Skeleton"; |
| 14 | +import { Text } from "@/components/ui/Text"; |
| 15 | +import { TextButton } from "@/components/ui/TextButton"; |
| 16 | +import { TextField } from "@/components/ui/TextField"; |
| 17 | + |
| 18 | +import { storeSearchQueryOptions } from "../../_api"; |
| 19 | +import { type SearchStoreFormValues } from "../../_types"; |
| 20 | +import * as styles from "./SearchStoreBottomSheet.css"; |
| 21 | + |
| 22 | +export type SearchStoreBottomSheetProps = { |
| 23 | + /** 바텀시트 열림/닫힘 상태 */ |
| 24 | + open: boolean; |
| 25 | + |
| 26 | + /** 바텀시트 상태 변경 핸들러 */ |
| 27 | + onOpenChange: (open: boolean) => void; |
| 28 | + |
| 29 | + /** 가게 선택 시 호출되는 핸들러 */ |
| 30 | + onSelect: (storeName: string) => void; |
| 31 | +}; |
| 32 | + |
| 33 | +export const SearchStoreBottomSheet = ({ |
| 34 | + open, |
| 35 | + onOpenChange, |
| 36 | + onSelect, |
| 37 | +}: SearchStoreBottomSheetProps) => { |
| 38 | + const { register, reset, watch, setValue } = useForm<SearchStoreFormValues>({ |
| 39 | + defaultValues: { searchTerm: "" }, |
| 40 | + mode: "onChange", |
| 41 | + }); |
| 42 | + |
| 43 | + const searchTermValue = watch("searchTerm"); |
| 44 | + const [debouncedSearchTerm, setDebouncedSearchTerm] = useState(""); |
| 45 | + |
| 46 | + // TODO: 검색 디바운스 시간 의논 |
| 47 | + const debouncedSearch = useDebounce((value: string) => { |
| 48 | + setDebouncedSearchTerm(value); |
| 49 | + }, 2000); |
| 50 | + |
| 51 | + const { data: list, isLoading } = useQuery( |
| 52 | + storeSearchQueryOptions(debouncedSearchTerm) |
| 53 | + ); |
| 54 | + |
| 55 | + const handleSearchChange = useCallback( |
| 56 | + (value: string) => { |
| 57 | + if (!value.trim()) { |
| 58 | + debouncedSearch.cancel(); |
| 59 | + setDebouncedSearchTerm(""); |
| 60 | + return; |
| 61 | + } |
| 62 | + debouncedSearch(value); |
| 63 | + }, |
| 64 | + [debouncedSearch] |
| 65 | + ); |
| 66 | + |
| 67 | + const handleSelect = (name: string) => { |
| 68 | + onSelect(name); |
| 69 | + onOpenChange(false); |
| 70 | + }; |
| 71 | + |
| 72 | + const handleCloseClick = () => { |
| 73 | + reset(); |
| 74 | + setDebouncedSearchTerm(""); |
| 75 | + onOpenChange(false); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleClearClick = () => { |
| 79 | + setValue("searchTerm", ""); |
| 80 | + setDebouncedSearchTerm(""); |
| 81 | + }; |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + if (!open) { |
| 85 | + reset(); |
| 86 | + setDebouncedSearchTerm(""); |
| 87 | + debouncedSearch.cancel(); |
| 88 | + } |
| 89 | + }, [open, reset, debouncedSearch]); |
| 90 | + |
| 91 | + const isSearching = !!debouncedSearchTerm && isLoading; |
| 92 | + |
| 93 | + return ( |
| 94 | + <BottomSheet.Root open={open} onOpenChange={onOpenChange}> |
| 95 | + <BottomSheet.Content className={styles.contentWrapper}> |
| 96 | + <BottomSheet.Title className={styles.titleWrapper}> |
| 97 | + <Text typo='title2Sb' color='neutral.10'> |
| 98 | + 가게 검색 |
| 99 | + </Text> |
| 100 | + <TextButton |
| 101 | + variant='primary' |
| 102 | + size='medium' |
| 103 | + onClick={handleCloseClick} |
| 104 | + > |
| 105 | + 취소 |
| 106 | + </TextButton> |
| 107 | + </BottomSheet.Title> |
| 108 | + |
| 109 | + <BottomSheet.Body> |
| 110 | + <TextField |
| 111 | + {...register("searchTerm", { |
| 112 | + onChange: e => handleSearchChange(e.target.value), |
| 113 | + })} |
| 114 | + placeholder='가게명을 입력해주세요' |
| 115 | + rightAddon={ |
| 116 | + searchTermValue ? ( |
| 117 | + <button |
| 118 | + className={styles.clearButtonWrapper} |
| 119 | + onClick={handleClearClick} |
| 120 | + type='button' |
| 121 | + > |
| 122 | + <CircleCloseIcon |
| 123 | + width={22} |
| 124 | + height={22} |
| 125 | + className={styles.icon} |
| 126 | + /> |
| 127 | + </button> |
| 128 | + ) : ( |
| 129 | + <SearchIcon width={20} height={20} /> |
| 130 | + ) |
| 131 | + } |
| 132 | + /> |
| 133 | + |
| 134 | + <AnimatePresence mode='wait'> |
| 135 | + {isSearching ? ( |
| 136 | + <motion.ul |
| 137 | + key='loading' |
| 138 | + className={styles.searchResultItems} |
| 139 | + initial='hidden' |
| 140 | + animate='visible' |
| 141 | + exit={{ opacity: 0 }} |
| 142 | + variants={styles.listVariants} |
| 143 | + > |
| 144 | + {Array.from({ length: 6 }).map((_, index) => ( |
| 145 | + <SearchResultItemSkeleton key={index} /> |
| 146 | + ))} |
| 147 | + </motion.ul> |
| 148 | + ) : ( |
| 149 | + <motion.ul |
| 150 | + className={styles.searchResultItems} |
| 151 | + initial='hidden' |
| 152 | + animate='visible' |
| 153 | + variants={styles.listVariants} |
| 154 | + > |
| 155 | + {list?.stores?.map(store => ( |
| 156 | + <SearchResultItemLayout |
| 157 | + key={store.kakaoId} |
| 158 | + onSelect={handleSelect} |
| 159 | + storeName={store.name} |
| 160 | + > |
| 161 | + <Text typo='body1Sb'>{store.name}</Text> |
| 162 | + <Text typo='label2Sb' color='neutral.50'> |
| 163 | + {store.address} |
| 164 | + </Text> |
| 165 | + </SearchResultItemLayout> |
| 166 | + ))} |
| 167 | + </motion.ul> |
| 168 | + )} |
| 169 | + </AnimatePresence> |
| 170 | + </BottomSheet.Body> |
| 171 | + |
| 172 | + <BottomSheet.Footer> |
| 173 | + <Button size='fullWidth' onClick={handleCloseClick}> |
| 174 | + 확인 |
| 175 | + </Button> |
| 176 | + </BottomSheet.Footer> |
| 177 | + </BottomSheet.Content> |
| 178 | + </BottomSheet.Root> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +const SearchResultItemLayout = ({ |
| 183 | + onSelect, |
| 184 | + storeName, |
| 185 | + className = styles.searchResultItem, |
| 186 | + children, |
| 187 | +}: { |
| 188 | + children: ReactNode; |
| 189 | + onSelect?: (name: string) => void; |
| 190 | + storeName?: string; |
| 191 | + className?: string; |
| 192 | +}) => ( |
| 193 | + <motion.li |
| 194 | + className={className} |
| 195 | + variants={styles.itemVariants} |
| 196 | + onClick={onSelect && storeName ? () => onSelect(storeName) : undefined} |
| 197 | + > |
| 198 | + {children} |
| 199 | + </motion.li> |
| 200 | +); |
| 201 | + |
| 202 | +const SearchResultItemSkeleton = () => { |
| 203 | + return ( |
| 204 | + <SearchResultItemLayout className={styles.skeletonItem}> |
| 205 | + <div className={styles.skeletonContent}> |
| 206 | + <Skeleton width='60%' height={20} radius={4} /> |
| 207 | + <Skeleton width='80%' height={16} radius={4} /> |
| 208 | + </div> |
| 209 | + </SearchResultItemLayout> |
| 210 | + ); |
| 211 | +}; |
0 commit comments