ultisuite-client/lib/mail-search/use-advanced-search-form.ts
2026-05-20 18:22:36 +02:00

79 lines
2.0 KiB
TypeScript

"use client"
import { useState } from "react"
import {
EMPTY_SEARCH_PARAMS,
type SearchParams,
} from "@/lib/mail-search/search-params"
export function useAdvancedSearchForm(
initialQuery: string,
currentParams: SearchParams | null
) {
const [from, setFrom] = useState(currentParams?.from ?? "")
const [to, setTo] = useState(currentParams?.to ?? "")
const [subject, setSubject] = useState(currentParams?.subject ?? "")
const [hasWords, setHasWords] = useState(
currentParams?.hasWords || currentParams?.q || initialQuery
)
const [doesNotHave, setDoesNotHave] = useState(currentParams?.doesNotHave ?? "")
const [sizeVal, setSizeVal] = useState(currentParams?.size ?? "")
const [sizeOp, setSizeOp] = useState<"gt" | "lt">(currentParams?.sizeOp ?? "gt")
const [sizeUnit, setSizeUnit] = useState<"Mo" | "Ko">(currentParams?.sizeUnit ?? "Mo")
const [within, setWithin] = useState(currentParams?.within ?? "")
const [dateAfter, setDateAfter] = useState(currentParams?.after ?? "")
const [searchIn, setSearchIn] = useState(currentParams?.in ?? "all")
const [hasAttachment, setHasAttachment] = useState(
currentParams?.has?.includes("attachment") ?? false
)
const [excludeChats, setExcludeChats] = useState(currentParams?.excludeChats ?? false)
const buildParams = (): Partial<SearchParams> => ({
...EMPTY_SEARCH_PARAMS,
q: "",
from,
to,
subject,
hasWords,
doesNotHave,
size: sizeVal,
sizeOp,
sizeUnit,
within,
after: dateAfter,
in: searchIn,
has: hasAttachment ? ["attachment"] : [],
excludeChats,
})
return {
from,
setFrom,
to,
setTo,
subject,
setSubject,
hasWords,
setHasWords,
doesNotHave,
setDoesNotHave,
sizeVal,
setSizeVal,
sizeOp,
setSizeOp,
sizeUnit,
setSizeUnit,
within,
setWithin,
dateAfter,
setDateAfter,
searchIn,
setSearchIn,
hasAttachment,
setHasAttachment,
excludeChats,
setExcludeChats,
buildParams,
}
}