"use client"; import { useRouter, useSearchParams } from "next/navigation"; import React from "react"; export function BoardToolbar({ boardId }: { boardId: string }) { const router = useRouter(); const sp = useSearchParams(); const sort = (sp.get("sort") as "recent" | "popular" | null) ?? "recent"; const scope = (sp.get("scope") as "q" | "author" | null) ?? "q"; // q: 제목+내용, author: 작성자 const defaultText = scope === "author" ? sp.get("author") ?? "" : sp.get("q") ?? ""; const period = sp.get("period") ?? "all"; // all | 1d | 1w | 1m const onChangeSort = (e: React.ChangeEvent) => { const next = new URLSearchParams(sp.toString()); next.set("sort", e.target.value); router.push(`/boards/${boardId}?${next.toString()}`); }; const onChangePeriod = (e: React.ChangeEvent) => { const next = new URLSearchParams(sp.toString()); const v = e.target.value; next.set("period", v); // 계산된 start 적용 const now = new Date(); if (v === "1d") now.setDate(now.getDate() - 1); if (v === "1w") now.setDate(now.getDate() - 7); if (v === "1m") now.setMonth(now.getMonth() - 1); if (v === "all") next.delete("start"); else next.set("start", now.toISOString()); router.push(`/boards/${boardId}?${next.toString()}`); }; const onSubmit = (formData: FormData) => { const next = new URLSearchParams(sp.toString()); const scopeSel = String(formData.get("scope") || "q"); const text = String(formData.get("text") || ""); next.set("scope", scopeSel); if (scopeSel === "author") { next.delete("q"); if (text) next.set("author", text); else next.delete("author"); } else { next.delete("author"); if (text) next.set("q", text); else next.delete("q"); } router.push(`/boards/${boardId}?${next.toString()}`); }; return (
); } export default BoardToolbar;