File size: 13,745 Bytes
e59840b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a04377a
e59840b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a04377a
 
 
 
 
 
 
 
 
 
e59840b
 
a04377a
 
 
 
 
e59840b
 
a04377a
e59840b
 
 
a04377a
 
 
 
e59840b
 
 
 
a04377a
e59840b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import React, { useState, useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import {
  Search,
  X,
  Globe,
  Lock,
  Files,
  Database,
  Users,
  Heart,
  ArrowRight,
  ExternalLink,
  Sparkles,
  Command
} from 'lucide-react';
import { api } from '../api/client';

export default function GlobalSearchModal({ isOpen, onClose, onSelectWorkspace, onSelectUser }) {
  const [query, setQuery] = useState('');
  const [filterType, setFilterType] = useState('ALL'); // 'ALL' | 'WORKSPACES' | 'USERS'
  const [loading, setLoading] = useState(false);
  const [results, setResults] = useState({ workspaces: [], users: [] });
  const [recentSearches, setRecentSearches] = useState(['Clinical', 'Legal', 'Master', 'Adhi']);
  const inputRef = useRef(null);
  const navigate = useNavigate();

  useEffect(() => {
    if (isOpen) {
      setTimeout(() => inputRef.current?.focus(), 50);
      if (query.trim()) {
        performSearch(query);
      }
    } else {
      setQuery('');
    }
  }, [isOpen]);

  useEffect(() => {
    if (!isOpen) return;
    const timeout = setTimeout(() => {
      performSearch(query);
    }, 200);
    return () => clearTimeout(timeout);
  }, [query]);

  const performSearch = async (searchStr) => {
    const q = searchStr.trim();
    if (!q) {
      setResults({ workspaces: [], users: [] });
      return;
    }
    setLoading(true);
    try {
      const data = await api.searchGlobal(q);
      setResults({
        workspaces: data.workspaces || [],
        users: data.users || [],
      });
    } catch (err) {
      console.warn('Global search error:', err);
    } finally {
      setLoading(false);
    }
  };

  const handleOpenWorkspace = (ws) => {
    if (onSelectWorkspace) onSelectWorkspace(ws);
    navigate(`/workspaces/${ws.id}/diff`);
    onClose();
  };

  if (!isOpen) return null;

  const filteredWorkspaces = filterType === 'USERS' ? [] : results.workspaces;
  const filteredUsers = filterType === 'WORKSPACES' ? [] : results.users;
  const totalFound = filteredWorkspaces.length + filteredUsers.length;

  return (
    <div className="fixed inset-0 z-50 flex items-start justify-center pt-16 sm:pt-24 px-4 bg-black/80 backdrop-blur-md">
      <div className="bg-[#0B0F19] border border-slate-700/80 rounded-2xl w-full max-w-2xl shadow-2xl overflow-hidden flex flex-col max-h-[80vh] animate-in fade-in zoom-in-95 duration-150">
        {/* Search Input Bar */}
        <div className="p-4 border-b border-slate-800 flex items-center space-x-3 bg-[#0D121F]">
          <Search className="w-5 h-5 text-emerald-400 shrink-0" />
          <input
            ref={inputRef}
            type="text"
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            placeholder="Search workspaces, collaborators, or knowledge registries..."
            className="flex-1 bg-transparent text-sm text-white placeholder-slate-500 focus:outline-none font-mono"
          />
          {loading && (
            <div className="w-4 h-4 border-2 border-emerald-500 border-t-transparent rounded-full animate-spin shrink-0" />
          )}
          {query && (
            <button
              onClick={() => setQuery('')}
              className="p-1 rounded text-slate-500 hover:text-white transition"
            >
              <X className="w-4 h-4" />
            </button>
          )}
          <button
            onClick={onClose}
            className="px-2 py-0.5 rounded bg-slate-800 text-[11px] font-mono text-slate-400 hover:text-white transition"
          >
            ESC
          </button>
        </div>

        {/* Filter Pills */}
        <div className="px-4 py-2 bg-[#090D15] border-b border-slate-800/80 flex items-center space-x-2 text-xs">
          <span className="text-[11px] text-slate-500 font-mono mr-1">Filter:</span>
          {[
            { id: 'ALL', label: 'All Results' },
            { id: 'WORKSPACES', label: `Workspaces (${results.workspaces.length})` },
            { id: 'USERS', label: `Users (${results.users.length})` }
          ].map((pill) => (
            <button
              key={pill.id}
              onClick={() => setFilterType(pill.id)}
              className={`px-2.5 py-1 rounded-lg text-[11px] font-semibold transition ${
                filterType === pill.id
                  ? 'bg-emerald-500/10 text-emerald-300 border border-emerald-500/30'
                  : 'text-slate-400 hover:text-white hover:bg-slate-800'
              }`}
            >
              {pill.label}
            </button>
          ))}
        </div>

        {/* Results Container */}
        <div className="p-4 overflow-y-auto flex-1 space-y-5">
          {!query.trim() ? (
            <div className="space-y-4 py-2">
              <div className="text-xs text-slate-400 font-mono flex items-center space-x-1.5">
                <Sparkles className="w-3.5 h-3.5 text-amber-400" />
                <span>Suggested Searches</span>
              </div>
              <div className="flex flex-wrap gap-2">
                {recentSearches.map((term) => (
                  <button
                    key={term}
                    onClick={() => setQuery(term)}
                    className="px-3 py-1.5 rounded-xl bg-[#121826] hover:bg-[#1A2236] border border-slate-800 text-xs text-slate-300 font-mono hover:text-white transition flex items-center space-x-1.5"
                  >
                    <Search className="w-3 h-3 text-emerald-400" />
                    <span>{term}</span>
                  </button>
                ))}
              </div>
              <p className="text-[11px] text-slate-500 pt-3">
                Tip: Search by workspace name, topic keyword, or teammate's email/username.
              </p>
            </div>
          ) : totalFound === 0 && !loading ? (
            <div className="py-12 text-center space-y-2">
              <Search className="w-8 h-8 text-slate-600 mx-auto" />
              <p className="text-xs text-slate-300 font-medium">No results found for "{query}".</p>
              <p className="text-[11px] text-slate-500">
                Try searching by a broader keyword or checking for spelling.
              </p>
            </div>
          ) : (
            <>
              {/* Workspaces Section */}
              {filteredWorkspaces.length > 0 && (
                <div className="space-y-2.5">
                  <div className="flex items-center justify-between text-xs text-slate-400 font-mono">
                    <span className="font-bold uppercase tracking-wider text-slate-300">Workspaces</span>
                    <span>{filteredWorkspaces.length} found</span>
                  </div>
                  <div className="space-y-2">
                    {filteredWorkspaces.map((ws) => (
                      <div
                        key={ws.id}
                        onClick={() => handleOpenWorkspace(ws)}
                        className="p-3.5 rounded-xl bg-[#111726] hover:bg-[#172036] border border-slate-800 hover:border-emerald-500/40 transition cursor-pointer flex flex-col sm:flex-row sm:items-center justify-between gap-3 group"
                      >
                        <div className="flex items-start space-x-3 min-w-0">
                          <div className={`p-2 rounded-lg shrink-0 mt-0.5 ${
                            ws.visibility === 'public'
                              ? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
                              : 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
                          }`}>
                            {ws.visibility === 'public' ? (
                              <Globe className="w-4 h-4" />
                            ) : (
                              <Lock className="w-4 h-4" />
                            )}
                          </div>
                          <div className="min-w-0">
                            <div className="flex items-center space-x-2">
                              <span className="font-bold text-xs text-white group-hover:text-emerald-300 truncate">
                                {ws.name}
                              </span>
                              <span className={`px-1.5 py-0.2 rounded text-[9px] font-mono font-bold uppercase ${
                                ws.visibility === 'public'
                                  ? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
                                  : 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
                              }`}>
                                {ws.visibility}
                              </span>
                            </div>
                            <p className="text-[11px] text-slate-400 line-clamp-1 mt-0.5">
                              {ws.description || `Maintained by ${ws.owner?.username || 'Owner'}`}
                            </p>
                            <div className="flex items-center space-x-3 text-[10px] text-slate-500 font-mono mt-1">
                              <span className="flex items-center space-x-1">
                                <Files className="w-3 h-3 text-slate-400" />
                                <span>{ws.doc_count || 0} docs</span>
                              </span>
                              <span>&bull;</span>
                              <span className="flex items-center space-x-1">
                                <Database className="w-3 h-3 text-emerald-400" />
                                <span>{ws.knowledge_count || 0} facts</span>
                              </span>
                              <span>&bull;</span>
                              <span className="flex items-center space-x-1 text-pink-400">
                                <Heart className="w-3 h-3 fill-pink-400/50" />
                                <span>{ws.follower_count || 0} followers</span>
                              </span>
                            </div>
                          </div>
                        </div>

                        <div className="flex items-center space-x-2 shrink-0 self-end sm:self-center">
                          <span className="text-xs text-emerald-400 font-semibold group-hover:translate-x-0.5 transition flex items-center space-x-1">
                            <span>Open</span>
                            <ArrowRight className="w-3.5 h-3.5" />
                          </span>
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              )}

              {/* Users Section */}
              {filteredUsers.length > 0 && (
                <div className="space-y-2.5 pt-2">
                  <div className="flex items-center justify-between text-xs text-slate-400 font-mono">
                    <span className="font-bold uppercase tracking-wider text-slate-300">Users & Collaborators</span>
                    <span>{filteredUsers.length} found</span>
                  </div>
                  <div className="divide-y divide-slate-800/80 rounded-xl border border-slate-800 bg-[#111726] overflow-hidden text-xs">
                    {filteredUsers.map((u) => (
                      <div
                        key={u.id}
                        onClick={() => {
                          if (onSelectUser) onSelectUser(u);
                          onClose();
                        }}
                        className="p-3.5 flex items-center justify-between hover:bg-[#161D2E] transition cursor-pointer group"
                      >
                        <div className="flex items-center space-x-3 min-w-0">
                          <div className="w-8 h-8 rounded-full bg-emerald-500/20 text-emerald-400 font-bold flex items-center justify-center text-xs border border-emerald-500/30 shrink-0">
                            {(u.username || u.email)[0].toUpperCase()}
                          </div>
                          <div className="min-w-0">
                            <span className="font-semibold text-white block group-hover:text-emerald-300 transition truncate">
                              {u.username || 'Collaborator'}
                            </span>
                            <span className="text-[10px] text-slate-400 font-mono block truncate">{u.email}</span>
                          </div>
                        </div>
                        <div className="flex items-center space-x-3 shrink-0">
                          <span className="px-2 py-0.5 rounded text-[10px] font-mono bg-slate-800 text-slate-300">
                            {u.workspaces_count || 0} workspaces
                          </span>
                          <span className="text-xs text-emerald-400 font-semibold group-hover:translate-x-0.5 transition flex items-center space-x-1">
                            <span>Profile</span>
                            <ArrowRight className="w-3.5 h-3.5" />
                          </span>
                        </div>
                      </div>
                    ))}
                  </div>

                </div>
              )}
            </>
          )}
        </div>

        {/* Footer */}
        <div className="px-4 py-2.5 bg-[#090D15] border-t border-slate-800/80 flex items-center justify-between text-[11px] text-slate-500 font-mono">
          <div className="flex items-center space-x-2">
            <Command className="w-3.5 h-3.5 text-slate-400" />
            <span>Global Registry Search across all public & shared repositories</span>
          </div>
          <span>Press ESC to close</span>
        </div>
      </div>
    </div>
  );
}