|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useCallback, useRef } from "react"; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogContent, |
| 7 | + DialogOverlay, |
| 8 | + DialogTitle, |
| 9 | +} from "@radix-ui/react-dialog"; |
| 10 | + |
| 11 | +export interface SharedProps { |
| 12 | + open: boolean; |
| 13 | + onOpenChange: (open: boolean) => void; |
| 14 | +} |
| 15 | + |
| 16 | +interface SearchDialogProps extends SharedProps { |
| 17 | + onSearchChange?: (v: string) => void; |
| 18 | +} |
| 19 | + |
| 20 | +export function SearchDialog({ |
| 21 | + open, |
| 22 | + onOpenChange, |
| 23 | + onSearchChange, |
| 24 | +}: SearchDialogProps) { |
| 25 | + const formRef = useRef<HTMLFormElement>(null); |
| 26 | + |
| 27 | + const handleSearch = useCallback((query: string) => { |
| 28 | + // create a new tab and append the search query |
| 29 | + window.open( |
| 30 | + `https://www.google.com/search?q=site:userclouds.com${encodeURIComponent(` ${query}`)}`, |
| 31 | + "_blank", |
| 32 | + ); |
| 33 | + }, []); |
| 34 | + |
| 35 | + const handleClose = useCallback(() => { |
| 36 | + onOpenChange(false); |
| 37 | + if (formRef.current) { |
| 38 | + formRef.current.reset(); |
| 39 | + } |
| 40 | + }, [onOpenChange]); |
| 41 | + |
| 42 | + const handleSubmit = useCallback( |
| 43 | + (e: React.FormEvent) => { |
| 44 | + e.preventDefault(); |
| 45 | + const formData = new FormData(e.target as HTMLFormElement); |
| 46 | + const query = formData.get("search") as string; |
| 47 | + if (query) { |
| 48 | + handleSearch(query); |
| 49 | + onSearchChange?.(query); |
| 50 | + } |
| 51 | + }, |
| 52 | + [handleSearch, onSearchChange], |
| 53 | + ); |
| 54 | + |
| 55 | + return ( |
| 56 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 57 | + <DialogOverlay className="fixed inset-0 z-50 bg-black/30 backdrop-blur-sm data-[state=closed]:animate-fd-fade-out data-[state=open]:animate-fd-fade-in" /> |
| 58 | + <DialogContent |
| 59 | + aria-describedby={undefined} |
| 60 | + className="fixed left-1/2 top-[10vh] z-50 w-[98vw] max-w-screen-sm -translate-x-1/2 rounded-lg border bg-fd-popover text-fd-popover-foreground shadow-lg data-[state=closed]:animate-fd-dialog-out data-[state=open]:animate-fd-dialog-in" |
| 61 | + > |
| 62 | + <DialogTitle className="hidden">Search…</DialogTitle> |
| 63 | + <form |
| 64 | + ref={formRef} |
| 65 | + onSubmit={handleSubmit} |
| 66 | + className="flex flex-row items-center gap-2 px-3" |
| 67 | + > |
| 68 | + <input |
| 69 | + name="search" |
| 70 | + defaultValue="" |
| 71 | + placeholder="Search…" |
| 72 | + onChange={(e) => onSearchChange?.(e.target.value)} |
| 73 | + className="w-0 flex-1 bg-transparent py-3 text-base placeholder:text-fd-muted-foreground focus-visible:outline-none" |
| 74 | + /> |
| 75 | + <button |
| 76 | + type="button" |
| 77 | + aria-label="Close Search" |
| 78 | + onClick={handleClose} |
| 79 | + className="text-xs p-1.5 flex items-center gap-2 " |
| 80 | + > |
| 81 | + <kbd className="rounded-md border bg-fd-background p-1.5">Esc</kbd> |
| 82 | + </button> |
| 83 | + </form> |
| 84 | + </DialogContent> |
| 85 | + </Dialog> |
| 86 | + ); |
| 87 | +} |
0 commit comments