|
| 1 | +import { useState, useRef } from 'react' |
| 2 | +import { ClickableTooltip } from '@/components/clickableTooltip' |
| 3 | +import type { ProfileMetaItem } from '@/services/hooks/profile/getProfileMeta.hook' |
| 4 | + |
| 5 | +interface InterestsSelectorProps { |
| 6 | + interests: ProfileMetaItem[] |
| 7 | + selectedInterests: string[] |
| 8 | + onSelect: (interestIds: string[]) => void |
| 9 | + isLoading?: boolean |
| 10 | + triggerElement: React.ReactNode |
| 11 | +} |
| 12 | + |
| 13 | +export const InterestsSelector = ({ |
| 14 | + interests, |
| 15 | + selectedInterests, |
| 16 | + onSelect, |
| 17 | + isLoading = false, |
| 18 | + triggerElement, |
| 19 | +}: InterestsSelectorProps) => { |
| 20 | + const [isOpen, setIsOpen] = useState(false) |
| 21 | + const triggerRef = useRef<HTMLButtonElement>(null) |
| 22 | + |
| 23 | + const handleInterestToggle = (interestId: string) => { |
| 24 | + const isSelected = selectedInterests.includes(interestId) |
| 25 | + if (isSelected) { |
| 26 | + onSelect(selectedInterests.filter((id) => id !== interestId)) |
| 27 | + } else if (selectedInterests.length < 3) { |
| 28 | + onSelect([...selectedInterests, interestId]) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + const content = ( |
| 33 | + <div className="w-48 p-1 overflow-x-hidden overflow-y-auto scrollbar-none max-h-40"> |
| 34 | + {isLoading ? ( |
| 35 | + <div className="py-4 text-center text-[10px] font-black italic animate-pulse"> |
| 36 | + صبر کنید... |
| 37 | + </div> |
| 38 | + ) : ( |
| 39 | + <div className="flex flex-row flex-wrap gap-1"> |
| 40 | + {interests.map((interest) => { |
| 41 | + const isSelected = selectedInterests.includes(interest.id) |
| 42 | + const canSelect = selectedInterests.length < 3 || isSelected |
| 43 | + |
| 44 | + return ( |
| 45 | + <button |
| 46 | + key={interest.id} |
| 47 | + type="button" |
| 48 | + disabled={!canSelect} |
| 49 | + onClick={() => handleInterestToggle(interest.id)} |
| 50 | + className={` |
| 51 | + h-8 px-3 w-fit flex items-center justify-center |
| 52 | + text-[10px] font-black italic rounded-full text-muted border |
| 53 | + transition-all duration-200 active:scale-95 cursor-pointer |
| 54 | + ${ |
| 55 | + isSelected |
| 56 | + ? 'bg-primary border-primary text-white! shadow-md shadow-primary/20' |
| 57 | + : !canSelect |
| 58 | + ? 'opacity-30 cursor-not-allowed border-base-300' |
| 59 | + : 'bg-base-200/50 border-base-300/30 text-content/70 hover:bg-base-200' |
| 60 | + } |
| 61 | + `} |
| 62 | + > |
| 63 | + {interest.title} |
| 64 | + </button> |
| 65 | + ) |
| 66 | + })} |
| 67 | + </div> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + ) |
| 71 | + |
| 72 | + return ( |
| 73 | + <> |
| 74 | + <button |
| 75 | + ref={triggerRef} |
| 76 | + type="button" |
| 77 | + onClick={() => setIsOpen(!isOpen)} |
| 78 | + className="flex items-center justify-between w-full p-3 text-right transition-colors hover:bg-content" |
| 79 | + > |
| 80 | + {triggerElement} |
| 81 | + </button> |
| 82 | + |
| 83 | + <ClickableTooltip |
| 84 | + content={content} |
| 85 | + triggerRef={triggerRef} |
| 86 | + isOpen={isOpen} |
| 87 | + setIsOpen={setIsOpen} |
| 88 | + position="bottom" |
| 89 | + /> |
| 90 | + </> |
| 91 | + ) |
| 92 | +} |
0 commit comments