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 (
{/* Search Input Bar */}
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 && (
)}
{query && (
)}
{/* Filter Pills */}
Filter:
{[
{ id: 'ALL', label: 'All Results' },
{ id: 'WORKSPACES', label: `Workspaces (${results.workspaces.length})` },
{ id: 'USERS', label: `Users (${results.users.length})` }
].map((pill) => (
))}
{/* Results Container */}
{!query.trim() ? (
Suggested Searches
{recentSearches.map((term) => (
))}
Tip: Search by workspace name, topic keyword, or teammate's email/username.
) : totalFound === 0 && !loading ? (
No results found for "{query}".
Try searching by a broader keyword or checking for spelling.
) : (
<>
{/* Workspaces Section */}
{filteredWorkspaces.length > 0 && (
Workspaces
{filteredWorkspaces.length} found
{filteredWorkspaces.map((ws) => (
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"
>
{ws.visibility === 'public' ? (
) : (
)}
{ws.name}
{ws.visibility}
{ws.description || `Maintained by ${ws.owner?.username || 'Owner'}`}
{ws.doc_count || 0} docs
•
{ws.knowledge_count || 0} facts
•
{ws.follower_count || 0} followers
))}
)}
{/* Users Section */}
{filteredUsers.length > 0 && (
Users & Collaborators
{filteredUsers.length} found
{filteredUsers.map((u) => (
{
if (onSelectUser) onSelectUser(u);
onClose();
}}
className="p-3.5 flex items-center justify-between hover:bg-[#161D2E] transition cursor-pointer group"
>
{(u.username || u.email)[0].toUpperCase()}
{u.username || 'Collaborator'}
{u.email}
{u.workspaces_count || 0} workspaces
Profile
))}
)}
>
)}
{/* Footer */}
Global Registry Search across all public & shared repositories
Press ESC to close
);
}