|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Search } from 'lucide-react'; |
| 4 | +import { useRouter } from 'next/navigation'; |
| 5 | +import { usePostHog } from 'posthog-js/react'; |
| 6 | +import { useState } from 'react'; |
| 7 | +import { ClipLoader } from 'react-spinners'; |
| 8 | +import { toast } from 'sonner'; |
| 9 | + |
| 10 | +import { Button } from '@/components/ui/button'; |
| 11 | +import { Input } from '@/components/ui/input'; |
| 12 | +import { graphqlRequest } from '@/lib/graphql-request'; |
| 13 | +import { IdByLoginDocument } from '@/types/generated/graphql'; |
| 14 | + |
| 15 | +export const SearchProfile = () => { |
| 16 | + const [login, setLogin] = useState(''); |
| 17 | + const [loading, setLoading] = useState(false); |
| 18 | + const router = useRouter(); |
| 19 | + const posthog = usePostHog(); |
| 20 | + |
| 21 | + const onSearch = async () => { |
| 22 | + if (!login) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + setLoading(true); |
| 27 | + const data = await graphqlRequest(IdByLoginDocument, { login }); |
| 28 | + |
| 29 | + const profileFound = data.rankByLogin?.githubId; |
| 30 | + |
| 31 | + posthog.capture('landingPage.search', { |
| 32 | + login, |
| 33 | + profileFound, |
| 34 | + }); |
| 35 | + |
| 36 | + if (profileFound) { |
| 37 | + return router.push(`/profile/${login}`); |
| 38 | + } |
| 39 | + |
| 40 | + setLoading(false); |
| 41 | + toast.error('User not found', { |
| 42 | + description: |
| 43 | + 'Our database currently includes only users who own or have contributed to repositories with more than 5 stars.', |
| 44 | + }); |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="flex flex-col gap-1"> |
| 49 | + <form |
| 50 | + onSubmit={(event) => { |
| 51 | + event.preventDefault(); |
| 52 | + onSearch(); |
| 53 | + }} |
| 54 | + className="flex gap-4" |
| 55 | + > |
| 56 | + <Input |
| 57 | + placeholder="GitHub login" |
| 58 | + value={login} |
| 59 | + onChange={(event) => setLogin(event.target.value)} |
| 60 | + disabled={loading} |
| 61 | + /> |
| 62 | + <Button onClick={onSearch} disabled={loading} className="w-[96px]"> |
| 63 | + {loading ? <ClipLoader loading={loading} size={16} /> : <Search className="size-4" />} |
| 64 | + {!loading && 'Search'} |
| 65 | + </Button> |
| 66 | + </form> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +}; |
0 commit comments