"use client"; import { ChangeEvent, useEffect, useRef, useState, } from "react"; import { useRouter } from "next/navigation"; interface Props { query?: string; } export default ({ query }: Props) => { const router = useRouter(); const [inputDisabled, setInputDisabled] = useState(false); const inputRef = useRef(null); const [content, setContent] = useState(""); const handleInputChange = (e: ChangeEvent) => { setContent(e.target.value); }; const handleInputKeydown = (e: KeyboardEvent) => { if (e.code === "Enter" && !e.shiftKey) { if (e.keyCode !== 229) { e.preventDefault(); handleSubmit("", content); } } }; const handleSubmit = async (keyword: string, question: string) => { try { const url = `?q=${encodeURIComponent(question)}`; console.log("query url", url); await router.push(url); setInputDisabled(true); // Disable input after navigation } catch (e) { console.log("search failed: ", e); setInputDisabled(false); } }; useEffect(() => { if (query) { setContent(query); setInputDisabled(false); } }, [query]); return (
{ if (content) { handleSubmit("", content); } }} >
); };