File size: 8,525 Bytes
0dd2082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221ee46
 
 
 
 
 
 
0dd2082
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { useState, useEffect, useRef, useMemo } from 'react';
import useSearchHistory from '../hooks/useSearchHistory';
import './SearchBar.css';

export default function SearchBar({
    onSearch,
    loading,
    userLocation,
    locationLoading,
    locationError,
    requestLocation,
    clearLocation
}) {
    const [query, setQuery] = useState('');
    const [showSuggestions, setShowSuggestions] = useState(false);
    const [activeIdx, setActiveIdx] = useState(-1);
    const { history } = useSearchHistory();
    const debounceRef = useRef(null);
    const wrapperRef = useRef(null);

    // Filter local history based on query
    const historySuggestions = useMemo(() => {
        if (!query.trim()) {
            return history.slice(0, 5).map(h => ({ ...h, type: 'history', text: h.query }));
        }
        const lowerQuery = query.toLowerCase();
        return history
            .filter(h => h.query.toLowerCase().includes(lowerQuery))
            .slice(0, 5)
            .map(h => ({ ...h, type: 'history', text: h.query }));
    }, [history, query]);

    // Only show private history suggestions
    const allSuggestions = useMemo(() => {
        return historySuggestions;
    }, [historySuggestions]);

    useEffect(() => {
        function handleClickOutside(e) {
            if (wrapperRef.current && !wrapperRef.current.contains(e.target)) {
                setShowSuggestions(false);
            }
        }
        document.addEventListener('mousedown', handleClickOutside);
        return () => document.removeEventListener('mousedown', handleClickOutside);
    }, []);

    function handleInputChange(value) {
        setQuery(value);
        setActiveIdx(-1);

        if (debounceRef.current) clearTimeout(debounceRef.current);

        setShowSuggestions(true);

        // We stop fetching global suggestions to respect "no global searches"
        // and only rely on the filtered local historySuggestions.
    }


    function handleSelect(text) {
        if (debounceRef.current) clearTimeout(debounceRef.current);
        setQuery(text);
        setShowSuggestions(false);
        onSearch(text);
    }


    function handleSubmit(e) {
        e.preventDefault();
        if (query.trim().length < 3) return;
        if (debounceRef.current) clearTimeout(debounceRef.current);
        setShowSuggestions(false);
        onSearch(query.trim());
    }

    function handleKeyDown(e) {
        if (!showSuggestions || allSuggestions.length === 0) return;

        if (e.key === 'ArrowDown') {
            e.preventDefault();
            setActiveIdx(prev => (prev < allSuggestions.length - 1 ? prev + 1 : 0));
        } else if (e.key === 'ArrowUp') {
            e.preventDefault();
            setActiveIdx(prev => (prev > 0 ? prev - 1 : allSuggestions.length - 1));
        } else if (e.key === 'Enter' && activeIdx >= 0) {
            e.preventDefault();
            handleSelect(allSuggestions[activeIdx].text);
        } else if (e.key === 'Escape') {
            setShowSuggestions(false);
        } else if (e.key === 'Tab') {
            setShowSuggestions(false);
        }
    }

    return (
        <div className="searchbar-container" ref={wrapperRef}>
            <form className="searchbar" onSubmit={handleSubmit}>
                <div className={`searchbar-input-wrap ${userLocation ? 'has-location' : ''}`}>
                    <button
                        type="button"
                        className={`location-btn ${userLocation ? 'active' : ''} ${locationError ? 'error' : ''}`}
                        onClick={userLocation ? clearLocation : requestLocation}
                        disabled={locationLoading || loading}
                        title={userLocation ? "Clear Location" : locationError ? locationError : "Use My Location"}
                    >
                        {locationLoading ? (
                            <span className="location-spinner"></span>
                        ) : (
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                                <polygon points="3 11 22 2 13 21 11 13 3 11" />
                            </svg>
                        )}
                    </button>
                    <input
                        id="search-input"
                        type="text"
                        className="searchbar-input"
                        placeholder='Search anything — add your city or use your location'
                        value={query}
                        onChange={e => handleInputChange(e.target.value)}
                        onFocus={() => { setShowSuggestions(true); }}
                        onKeyDown={handleKeyDown}
                        disabled={loading}
                        autoComplete="off"
                    />

                    {showSuggestions && allSuggestions.length > 0 && !loading && (
                        <ul className="searchbar-suggestions" role="listbox">
                            {allSuggestions.map((s, i) => (
                                <li
                                    key={`${s.type}-${i}`}
                                    className={`searchbar-suggestion ${i === activeIdx ? 'active' : ''} ${s.type === 'history' ? 'is-history' : ''}`}
                                    onClick={() => handleSelect(s.text)}
                                    role="option"
                                    aria-selected={i === activeIdx}
                                >
                                    <span className="suggestion-type-icon">
                                        {s.type === 'history' ? (
                                            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                                                <circle cx="12" cy="12" r="10" /><polyline points="12 6 12 12 16 14" />
                                            </svg>
                                        ) : s.type === 'place' ? '📍' : s.type === 'category' ? '📂' : '💡'}
                                    </span>
                                    <span className="suggestion-text">{s.text}</span>
                                    {s.type === 'history' && (
                                        <span className="suggestion-tag">Recent</span>
                                    )}
                                    {s.category && (
                                        <span className="suggestion-cat">{s.category}</span>
                                    )}
                                </li>
                            ))}
                        </ul>
                    )}
                </div>
                <button
                    id="search-button"
                    type="submit"
                    className="searchbar-btn"
                    disabled={loading || query.trim().length < 3}
                >
                    {loading ? (
                        <span className="searchbar-spinner"></span>
                    ) : (
                        <>
                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
                                <path d="M22 2L11 13" /><path d="M22 2L15 22L11 13L2 9L22 2Z" />
                            </svg>
                            Search
                        </>
                    )}
                </button>
            </form>

            {userLocation ? (
                <div className="location-active-plate">
                    <span className="active-dot"></span>
                    Using precise GPS location
                    <button type="button" className="clear-location-text" onClick={clearLocation}>
                        Clear
                    </button>
                </div>
            ) : (
                <div className="searchbar-hints">
                    <div className="location-hint">
                        Tip: include your city in the search or use your location
                    </div>
                    <div className="easter-egg-hint">
                        Psst... try searching for something from <span>Your Name</span>
                    </div>
                </div>
            )}
        </div>
    );
}