All files / components Search.tsx

100% Statements 31/31
87.5% Branches 14/16
100% Functions 10/10
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        2x                                                                                                 2x 46x 46x 46x 46x   46x 13x 1x 1x       13x 13x 13x       46x 21x   5x 35x     5x 5x   16x 16x       46x 1x 1x 1x 1x       46x           6x                                                   9x         1x 1x                                    
import React, { useState, useEffect, useRef } from 'react';
import { Link, navigate } from 'gatsby';
 
interface SearchResult {
  title: string;
  path: string;
  excerpt: string;
  category: string;
}
 
// This would normally be populated by a search index from a plugin like elasticlunr
// For demo purposes, we'll use a small static set of results
const mockResults: SearchResult[] = [
  {
    title: 'Getting Started',
    path: '/docs/getting-started',
    excerpt: 'Learn how to install and start using the Neo Rust SDK.',
    category: 'Documentation'
  },
  {
    title: 'Wallet Management',
    path: '/docs/wallets',
    excerpt: 'Create and manage Neo wallets securely with the SDK.',
    category: 'Documentation'
  },
  {
    title: 'Smart Contracts',
    path: '/docs/contracts',
    excerpt: 'Deploy and interact with smart contracts on the Neo blockchain.',
    category: 'Documentation'
  },
  {
    title: 'Message Signing',
    path: '/docs/wallets/message-signing',
    excerpt: 'Sign messages using Neo wallets to prove identity.',
    category: 'Documentation'
  },
  {
    title: 'NEP-17 Tokens',
    path: '/docs/contracts/token-standards',
    excerpt: 'Learn about NEP-17 fungible token standard implementation.',
    category: 'Documentation'
  },
  {
    title: 'Bridge Operations',
    path: '/docs/neo-x/bridge',
    excerpt: 'Transfer assets between Neo N3 and Neo X chains.',
    category: 'Documentation'
  },
  {
    title: 'API Reference',
    path: '/api-reference',
    excerpt: 'Complete API reference for the Neo Rust SDK.',
    category: 'Reference'
  }
];
 
interface SearchProps {
  placeholder?: string;
}
 
const Search: React.FC<SearchProps> = ({ placeholder = 'Search documentation...' }) => {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState<SearchResult[]>([]);
  const [isOpen, setIsOpen] = useState(false);
  const searchRef = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      Eif (searchRef.current && !searchRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    };
 
    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, []);
 
  useEffect(() => {
    if (query.length > 1) {
      // In a real app, this would call a search API or use a search index
      const filtered = mockResults.filter(result => 
        result.title.toLowerCase().includes(query.toLowerCase()) || 
        result.excerpt.toLowerCase().includes(query.toLowerCase())
      );
      setResults(filtered);
      setIsOpen(true);
    } else {
      setResults([]);
      setIsOpen(false);
    }
  }, [query]);
 
  const handleKeyDown = (e: React.KeyboardEvent) => {
    Eif (e.key === 'Enter' && results.length > 0) {
      navigate(results[0].path);
      setIsOpen(false);
      setQuery('');
    }
  };
 
  return (
    <div className="relative" ref={searchRef}>
      <div className="relative">
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          onKeyDown={handleKeyDown}
          placeholder={placeholder}
          className="w-full md:w-64 px-4 py-2 pr-10 rounded-lg bg-slate-700 border border-slate-600 focus:border-green-400 focus:outline-none focus:ring-1 focus:ring-green-400 text-gray-200 placeholder-gray-400"
        />
        <div className="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
          <svg
            className="h-5 w-5 text-gray-400"
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 20 20"
            fill="currentColor"
            aria-hidden="true"
          >
            <path
              fillRule="evenodd"
              d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z"
              clipRule="evenodd"
            />
          </svg>
        </div>
      </div>
 
      {isOpen && results.length > 0 && (
        <div className="absolute z-10 w-full mt-2 bg-slate-800 rounded-lg shadow-lg border border-slate-700 max-h-96 overflow-y-auto">
          <ul className="py-2">
            {results.map((result, index) => (
              <li key={index}>
                <Link
                  to={result.path}
                  className="block px-4 py-3 hover:bg-slate-700"
                  onClick={() => {
                    setIsOpen(false);
                    setQuery('');
                  }}
                >
                  <div className="flex items-center justify-between">
                    <span className="text-green-400 font-medium">{result.title}</span>
                    <span className="text-xs text-gray-400">{result.category}</span>
                  </div>
                  <p className="text-sm text-gray-300 mt-1">{result.excerpt}</p>
                </Link>
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  );
};
 
export default Search;