|
| 1 | +import { Search, Lightbulb, ArrowRight, Code2, FileSearch, Sparkles } from 'lucide-react' |
| 2 | +import { Button } from '@/components/ui/button' |
| 3 | + |
| 4 | +interface SearchEmptyStateProps { |
| 5 | + query: string |
| 6 | + onSuggestionClick: (suggestion: string) => void |
| 7 | +} |
| 8 | + |
| 9 | +const EXAMPLE_QUERIES = [ |
| 10 | + { query: 'authentication', description: 'Find auth logic' }, |
| 11 | + { query: 'error handling', description: 'Locate error handlers' }, |
| 12 | + { query: 'api routes', description: 'Discover API endpoints' }, |
| 13 | + { query: 'database', description: 'Find DB operations' }, |
| 14 | +] |
| 15 | + |
| 16 | +const SEARCH_TIPS = [ |
| 17 | + 'Use natural language: "function that handles user login"', |
| 18 | + 'Search by concept: "error handling" or "validation"', |
| 19 | + 'Find patterns: "async function" or "useEffect hook"', |
| 20 | + 'Be specific: "parse JSON response" beats "parse"', |
| 21 | +] |
| 22 | + |
| 23 | +export function SearchEmptyState({ query, onSuggestionClick }: SearchEmptyStateProps) { |
| 24 | + // Generate query-specific suggestions |
| 25 | + const getSuggestions = (q: string): string[] => { |
| 26 | + const lowerQ = q.toLowerCase() |
| 27 | + const suggestions: string[] = [] |
| 28 | + |
| 29 | + // If query is very short, suggest expanding |
| 30 | + if (q.length < 4) { |
| 31 | + suggestions.push(`${q} function`, `${q} handler`, `${q} component`) |
| 32 | + } |
| 33 | + |
| 34 | + // Common refinements |
| 35 | + if (lowerQ.includes('auth')) { |
| 36 | + suggestions.push('login handler', 'authentication middleware', 'session management') |
| 37 | + } else if (lowerQ.includes('api') || lowerQ.includes('route')) { |
| 38 | + suggestions.push('REST endpoint', 'API handler', 'route middleware') |
| 39 | + } else if (lowerQ.includes('error')) { |
| 40 | + suggestions.push('error boundary', 'try catch block', 'error middleware') |
| 41 | + } else if (lowerQ.includes('test')) { |
| 42 | + suggestions.push('unit test', 'test helper', 'mock function') |
| 43 | + } else { |
| 44 | + // Generic suggestions based on query |
| 45 | + suggestions.push( |
| 46 | + `${q} implementation`, |
| 47 | + `${q} helper function`, |
| 48 | + `how to ${q}` |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + return suggestions.slice(0, 3) |
| 53 | + } |
| 54 | + |
| 55 | + const suggestions = query ? getSuggestions(query) : [] |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="bg-card border border-border rounded-xl overflow-hidden"> |
| 59 | + {/* Header */} |
| 60 | + <div className="p-8 text-center border-b border-border bg-muted/30"> |
| 61 | + <div className="w-16 h-16 mx-auto mb-4 rounded-2xl bg-primary/10 border border-primary/20 flex items-center justify-center"> |
| 62 | + <FileSearch className="w-8 h-8 text-primary" /> |
| 63 | + </div> |
| 64 | + <h3 className="text-lg font-semibold mb-2 text-foreground">No results for "{query}"</h3> |
| 65 | + <p className="text-sm text-muted-foreground max-w-md mx-auto"> |
| 66 | + We couldn't find any code matching your query. Try one of the suggestions below or refine your search. |
| 67 | + </p> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div className="p-6 grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 71 | + {/* Query Suggestions */} |
| 72 | + {suggestions.length > 0 && ( |
| 73 | + <div> |
| 74 | + <h4 className="text-sm font-semibold text-foreground mb-3 flex items-center gap-2"> |
| 75 | + <Sparkles className="w-4 h-4 text-primary" /> |
| 76 | + Try these instead |
| 77 | + </h4> |
| 78 | + <div className="space-y-2"> |
| 79 | + {suggestions.map((suggestion, idx) => ( |
| 80 | + <button |
| 81 | + key={idx} |
| 82 | + onClick={() => onSuggestionClick(suggestion)} |
| 83 | + className="w-full flex items-center justify-between p-3 bg-muted/50 hover:bg-muted rounded-lg text-left transition-colors group" |
| 84 | + > |
| 85 | + <span className="text-sm text-foreground">{suggestion}</span> |
| 86 | + <ArrowRight className="w-4 h-4 text-muted-foreground group-hover:text-primary transition-colors" /> |
| 87 | + </button> |
| 88 | + ))} |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + {/* Example Queries */} |
| 94 | + <div> |
| 95 | + <h4 className="text-sm font-semibold text-foreground mb-3 flex items-center gap-2"> |
| 96 | + <Code2 className="w-4 h-4 text-primary" /> |
| 97 | + Popular searches |
| 98 | + </h4> |
| 99 | + <div className="space-y-2"> |
| 100 | + {EXAMPLE_QUERIES.map((example, idx) => ( |
| 101 | + <button |
| 102 | + key={idx} |
| 103 | + onClick={() => onSuggestionClick(example.query)} |
| 104 | + className="w-full flex items-center justify-between p-3 bg-muted/50 hover:bg-muted rounded-lg text-left transition-colors group" |
| 105 | + > |
| 106 | + <div> |
| 107 | + <span className="text-sm text-foreground">{example.query}</span> |
| 108 | + <span className="text-xs text-muted-foreground ml-2">— {example.description}</span> |
| 109 | + </div> |
| 110 | + <ArrowRight className="w-4 h-4 text-muted-foreground group-hover:text-primary transition-colors" /> |
| 111 | + </button> |
| 112 | + ))} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + {/* Search Tips */} |
| 118 | + <div className="p-5 bg-muted/30 border-t border-border"> |
| 119 | + <h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-3 flex items-center gap-2"> |
| 120 | + <Lightbulb className="w-3.5 h-3.5" /> |
| 121 | + Search Tips |
| 122 | + </h4> |
| 123 | + <ul className="grid grid-cols-1 md:grid-cols-2 gap-2"> |
| 124 | + {SEARCH_TIPS.map((tip, idx) => ( |
| 125 | + <li key={idx} className="text-xs text-muted-foreground flex items-start gap-2"> |
| 126 | + <span className="text-primary">•</span> |
| 127 | + {tip} |
| 128 | + </li> |
| 129 | + ))} |
| 130 | + </ul> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + ) |
| 134 | +} |
0 commit comments