From 7e1fc74660c415e7a1c902fe8362e1c5d3ed4c60 Mon Sep 17 00:00:00 2001 From: Taken Date: Mon, 29 Sep 2025 15:42:53 +0200 Subject: [PATCH] Updated search bar --- src/components/header.tsx | 4 +- src/components/search-bar.tsx | 113 +++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 11 deletions(-) diff --git a/src/components/header.tsx b/src/components/header.tsx index e3a5561..a63b3b5 100644 --- a/src/components/header.tsx +++ b/src/components/header.tsx @@ -1,6 +1,6 @@ import { Settings } from "lucide-react" import Link from "next/link" -import { SearchBar } from "./search-bar" +import { TopSearchBar } from "./search-bar" import { ThemeSwitcher } from "./theme-switcher" export default function Header({ searchBar = false }: { searchBar?: boolean }) { @@ -10,7 +10,7 @@ export default function Header({ searchBar = false }: { searchBar?: boolean }) {

HypStats

- {searchBar && } + {searchBar && }
diff --git a/src/components/search-bar.tsx b/src/components/search-bar.tsx index 91ab222..f1ef9f1 100644 --- a/src/components/search-bar.tsx +++ b/src/components/search-bar.tsx @@ -1,13 +1,13 @@ "use client" import { Input } from "@/components/ui/input" -import { cn } from "@/lib/utils" +import { capitalizeFirstLetter, cn } from "@/lib/utils" import { Search } from "lucide-react" import { useRouter } from "next/navigation" import { useEffect, useRef, useState } from "react" import { Button } from "./ui/button" -export function SearchBar({ navbar }: { navbar?: boolean }) { +export function TopSearchBar() { const [input, setInput] = useState("") const ref = useRef(null) const router = useRouter() @@ -31,23 +31,19 @@ export function SearchBar({ navbar }: { navbar?: boolean }) { async function handleSearch(e: React.FormEvent) { e.preventDefault() - if (input.trim()) { router.push(`/player/${encodeURIComponent(input.trim())}`) } - if (navbar) { - setInput("") - ref.current?.blur() - } + setInput("") + ref.current?.blur() } return ( -
+
) } + +const pages = ["player", "guild"] as const + +const guildTypes = ["player", "name", "id"] as const + +export function SearchBar() { + const [input, setInput] = useState("") + const [selected, setSelected] = useState("player") + const [guildType, setGuildType] = useState("player") + const ref = useRef(null) + const router = useRouter() + + useEffect(() => { + const controller = new AbortController() + + window.addEventListener("keydown", (ev) => { + if (!ref.current) return + + if (ev.ctrlKey && ev.key === "k") { + ev.preventDefault() + ref.current.focus() + } + }, { signal: controller.signal }) + + return () => { + controller.abort() + } + }) + + async function handleSearch(e: React.FormEvent) { + e.preventDefault() + if (!input.trim()) return + + switch (selected) { + case "player": + router.push(`/player/${encodeURIComponent(input.trim())}`) + break + case "guild": + router.push(`/guild/${encodeURIComponent(input.trim())}?type=${guildType}`) + break + default: + router.push(`/player/${encodeURIComponent(input.trim())}`) + break + } + } + + return ( +
+ +
+ setInput(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Enter") { + handleSearch(e) + } + }} + /> + +
+
+ {pages.map(p => { + return ( + + ) + })} +
+ {selected === "guild" && ( +
+ {guildTypes.map(gt => { + return ( + + ) + })} +
+ )} + +
+ ) +}