import { Input } from "@/components/ui/input"; import { useState, useEffect } from "react"; import { Search, X } from "lucide-react"; import { useNavigate } from "@tanstack/react-router"; import { KbdKey } from "./ui/kbdKey"; import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog"; import { Button } from "./ui/button"; // function determineRouteType(input: string) { // if (input.startsWith("at://")) { // return "at url"; // } else if (input.startsWith("https://")) { // return "pds"; // } else { // return "handle"; // } // return "unknown"; // } export function isOnMac() { return navigator.userAgent.toUpperCase().indexOf("MAC") >= 0; } function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent, ); } export function SmartSearchBar({ isKeybindEnabled = false, }: { isKeybindEnabled?: boolean; }) { const navigate = useNavigate(); const [input, setInput] = useState(""); const [open, setOpen] = useState(false); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const value = input.trim(); if (value && value !== "@") { // replace at:// with / to match the route correctly if (value.includes("bsky.app") || value.includes("main.bsky.dev")) { try { const url = new URL(value); const pathParts = url.pathname.split("/"); if (pathParts[1] === "profile") { const handle = pathParts[2]; if (pathParts[3] === "post") { // Post URL: /profile/handle/post/postid navigate({ to: `/at:/${handle}/app.bsky.feed.post/${pathParts[4]}`, }); } else if (pathParts[3] === "lists") { // List URL: /profile/handle/lists/listid navigate({ to: `/at:/${handle}/app.bsky.graph.list/${pathParts[4]}`, }); } else { // Profile URL: /profile/handle navigate({ to: `/at:/${handle}`, }); } } } catch (e) { console.error("Invalid Bluesky URL:", e); } } else if (value.startsWith("pds/") || value.startsWith("https:/")) { navigate({ to: "/pds/$url", params: { url: value.replace("https:/", "").replace("pds/", ""), }, }); } else if (value === "typing") { navigate({ to: "/rnfgrertt/typing" }); } else if (value === "pdsls") { navigate({ to: "/rnfgrertt/pdsls" }); } else if (value === "borgle" || value === "wordle") { navigate({ to: "/rnfgrertt/borgle" }); } else { // Allow handle lookups to start with @ navigate({ to: `/at:/${value.replace("at:/", "").replace(/^@/, "")}`, }); } setOpen(false); } }; useEffect(() => { if (isMobile()) return; const handleKeyDown = (e: KeyboardEvent) => { if ( (e.metaKey || e.ctrlKey) && e.key === "k" && isKeybindEnabled === true ) { e.preventDefault(); setOpen(true); } }; document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, []); return (
setInput(e.currentTarget.value)} className="border focus-visible:ring-0 focus-visible:ring-offset-0 pl-12 py-6 text-lg rounded-xl shadow-lg dark:shadow-slate-950" autoCorrect="off" autoCapitalize="none" autoFocus />
{!isMobile() && } setOpen(false)} />
); }