'use client' import { useEffect, useRef, useState } from 'react' // --------------------------------------------------------------------------- // Shared types — exported so the saved-searches page can reuse the shape // --------------------------------------------------------------------------- export type ContentType = 'users' | 'repos' | 'posts' | 'snippets' | 'hashtags' export type SortOption = 'relevance' | 'recent' | 'popular' | 'stars' export interface SearchFilters { query: string contentTypes: ContentType[] languages: string[] dateFrom: string dateTo: string sort: SortOption minStars: number minLikes: number } interface AdvancedSearchModalProps { isOpen: boolean onClose: () => void /** Pre-populate the builder (e.g. when editing a saved search) */ initialFilters?: Partial /** Fired when the user clicks "Search" */ onSearch: (filters: SearchFilters) => void /** Fired when the user clicks "Save search" */ onSaveSearch?: (filters: SearchFilters) => void } // --------------------------------------------------------------------------- // Constants // --------------------------------------------------------------------------- const CONTENT_TYPES: { key: ContentType; label: string }[] = [ { key: 'users', label: 'People' }, { key: 'repos', label: 'Repositories' }, { key: 'posts', label: 'Posts' }, { key: 'snippets', label: 'Snippets' }, { key: 'hashtags', label: 'Hashtags' }, ] const LANGUAGES: string[] = [ 'TypeScript', 'JavaScript', 'Python', 'Go', 'Rust', 'Java', 'C++', 'C#', 'Ruby', 'PHP', 'Swift', 'Kotlin', ] const SORT_OPTIONS: { key: SortOption; label: string }[] = [ { key: 'relevance', label: 'Most relevant' }, { key: 'recent', label: 'Most recent' }, { key: 'popular', label: 'Most popular' }, { key: 'stars', label: 'Most stars' }, ] const MAX_STARS = 50000 const MAX_LIKES = 10000 export const DEFAULT_FILTERS: SearchFilters = { query: '', contentTypes: ['repos'], languages: [], dateFrom: '', dateTo: '', sort: 'relevance', minStars: 0, minLikes: 0, } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function mergeFilters(partial?: Partial): SearchFilters { return { ...DEFAULT_FILTERS, ...partial } } function toggleInArray(arr: readonly T[], value: T): T[] { return arr.includes(value) ? arr.filter((v) => v !== value) : [...arr, value] } // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- export function AdvancedSearchModal({ isOpen, onClose, initialFilters, onSearch, onSaveSearch, }: AdvancedSearchModalProps) { const [filters, setFilters] = useState(mergeFilters(initialFilters)) const backdropRef = useRef(null) // Reset filters whenever the modal (re)opens useEffect(() => { if (isOpen) setFilters(mergeFilters(initialFilters)) }, [isOpen, initialFilters]) // Close on Escape useEffect(() => { if (!isOpen) return const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }, [isOpen, onClose]) // Lock body scroll while open useEffect(() => { document.body.style.overflow = isOpen ? 'hidden' : '' return () => { document.body.style.overflow = '' } }, [isOpen]) if (!isOpen) return null const update = (key: K, value: SearchFilters[K]) => { setFilters((prev) => ({ ...prev, [key]: value })) } const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === backdropRef.current) onClose() } const canSearch = filters.query.trim().length > 0 && filters.contentTypes.length > 0 return (
) } export default AdvancedSearchModal