SPB Git

Repository Public

FilterPanel.tsx 7.1 KB 155 lines tsx

1// SP Mail Next — panneau de filtres avancés de la recherche : chaque champ ÉDITE les pastilles du champ de recherche (aucun état de requête à part).2import { useEffect, useId, useRef, useState } from "react"3import { t } from "../../i18n"4import { IN_ROLES, applyPanel, emptyPanel, panelFromChips, type Chip, type PanelState } from "../../next/search"5import { LABELS } from "../../next/search-state"6import { S } from "../../state/app"7import { folderLabel, sortedFolders } from "../../state/mail"8import { useStore } from "../../state/store"9import { Dialog } from "../kit"10import { roleLabel } from "./SearchBar"1112interface Props {13  chips: Chip[]14  /** téléphone : dialogue pleine largeur ; sinon panneau sous le champ */15  sheet: boolean16  /** recherche dans toutes les boîtes : seuls les dossiers communs (rôles) sont proposés */17  all: boolean18  onChange: (chips: Chip[]) => void19  onRun: (chips: Chip[]) => void20  onClose: () => void21}2223export default function FilterPanel({ chips, sheet, all, onChange, onRun, onClose }: Props) {24  // texte des champs tel que tapé (espaces finales comprises) ; les pastilles en reçoivent la valeur nettoyée à chaque frappe25  const [form, setForm] = useState<PanelState>(() => panelFromChips(chips))26  const labels = LABELS.use()27  const account = useStore(S, (s) => (s.mb ? s.accounts[s.mb] : undefined))28  const custom = all ? [] : sortedFolders(account).custom29  const ids = { from: useId(), to: useId(), subject: useId(), after: useId(), before: useId(), att: useId(), min: useId(), max: useId(), label: useId(), folder: useId() }30  const latest = useRef(chips)31  latest.current = chips32  const edit = (patch: Partial<PanelState>) => {33    const next = { ...form, ...patch }34    setForm(next)35    onChange(applyPanel(latest.current, next))36  }37  const ref = useRef<HTMLFormElement>(null)38  // panneau (ordinateur) : Échap le ferme (comme le bouton des filtres) ; le dialogue (téléphone) s'en charge lui-même39  useEffect(() => {40    if (sheet) return41    ref.current?.querySelector<HTMLElement>("input")?.focus()42    const onKey = (e: KeyboardEvent) => {43      if (e.key !== "Escape") return44      e.stopPropagation()45      onClose()46    }47    const node = ref.current48    node?.addEventListener("keydown", onKey)49    return () => node?.removeEventListener("keydown", onKey)50  }, [sheet, onClose])51  // un libellé ou un dossier écrit à la main dans la requête reste sélectionnable52  const labelNames = labels.map((l) => l.name)53  if (form.label && !labelNames.some((n) => n.toLowerCase() === form.label.toLowerCase())) labelNames.push(form.label)54  const folderValues: { value: string; label: string }[] = [...IN_ROLES.map((r) => ({ value: r as string, label: roleLabel(r) })), ...custom.map((f) => ({ value: f.name, label: folderLabel(f) }))]55  if (form.folder && !folderValues.some((f) => f.value === form.folder)) folderValues.push({ value: form.folder, label: form.folder })5657  const fields = (58    <>59      <div className="field">60        <label htmlFor={ids.from}>{t("search.from")}</label>61        <input id={ids.from} className="input" value={form.from} onChange={(e) => edit({ from: e.target.value })} autoComplete="off" spellCheck={false} data-autofocus />62      </div>63      <div className="field">64        <label htmlFor={ids.to}>{t("search.to")}</label>65        <input id={ids.to} className="input" value={form.to} onChange={(e) => edit({ to: e.target.value })} autoComplete="off" spellCheck={false} />66      </div>67      <div className="field">68        <label htmlFor={ids.subject}>{t("search.subject")}</label>69        <input id={ids.subject} className="input" value={form.subject} onChange={(e) => edit({ subject: e.target.value })} autoComplete="off" />70      </div>71      <div className="search-dates">72        <div className="field">73          <label htmlFor={ids.after}>{t("search.after")}</label>74          <input id={ids.after} className="input" type="date" value={form.after} onChange={(e) => edit({ after: e.target.value })} />75        </div>76        <div className="field">77          <label htmlFor={ids.before}>{t("search.before")}</label>78          <input id={ids.before} className="input" type="date" value={form.before} onChange={(e) => edit({ before: e.target.value })} />79        </div>80      </div>81      <div className="search-dates">82        <div className="field">83          <label htmlFor={ids.min}>{t("nx.search.panel.minSize")}</label>84          <input id={ids.min} className="input" type="number" inputMode="decimal" min={0} step="any" value={form.minSize} onChange={(e) => edit({ minSize: e.target.value })} />85        </div>86        <div className="field">87          <label htmlFor={ids.max}>{t("nx.search.panel.maxSize")}</label>88          <input id={ids.max} className="input" type="number" inputMode="decimal" min={0} step="any" value={form.maxSize} onChange={(e) => edit({ maxSize: e.target.value })} />89        </div>90      </div>91      <div className="search-dates">92        {labelNames.length ? (93          <div className="field">94            <label htmlFor={ids.label}>{t("nx.search.op.label")}</label>95            <select id={ids.label} className="input" value={labelNames.find((n) => n.toLowerCase() === form.label.toLowerCase()) ?? ""} onChange={(e) => edit({ label: e.target.value })}>96              <option value="">{t("nx.search.panel.any")}</option>97              {labelNames.map((n) => (98                <option key={n} value={n}>99                  {n}100                </option>101              ))}102            </select>103          </div>104        ) : null}105        <div className="field">106          <label htmlFor={ids.folder}>{t("nx.search.op.in")}</label>107          <select id={ids.folder} className="input" value={form.folder} onChange={(e) => edit({ folder: e.target.value })}>108            <option value="">{t("nx.search.panel.anyFolder")}</option>109            {folderValues.map((f) => (110              <option key={f.value} value={f.value}>111                {f.label}112              </option>113            ))}114          </select>115        </div>116      </div>117      <label className="check" htmlFor={ids.att}>118        <input id={ids.att} type="checkbox" checked={form.hasAttachment} onChange={(e) => edit({ hasAttachment: e.target.checked })} />119        <span>{t("search.hasAttachment")}</span>120      </label>121    </>122  )123  const buttons = (124    <>125      <button type="button" className="btn small" onClick={() => edit(emptyPanel())}>126        {t("search.reset")}127      </button>128      <button type="submit" className="btn primary small" form={ids.from + "f"}>129        {t("search.run")}130      </button>131    </>132  )133  const submit = (e: React.FormEvent) => {134    e.preventDefault()135    onRun(latest.current)136  }137  if (sheet)138    return (139      <Dialog title={t("search.filters")} onClose={onClose} footer={buttons}>140        <form id={ids.from + "f"} className="nx-fp nx-fp-sheet" aria-label={t("search.filters")} onSubmit={submit}>141          {fields}142        </form>143      </Dialog>144    )145  return (146    <form ref={ref} id={ids.from + "f"} className="search-panel nx-fp" aria-label={t("search.filters")} onSubmit={submit}>147      {fields}148      <div className="row">149        <span className="spacer" />150        {buttons}151      </div>152    </form>153  )154}155
Files main
Menu

Theme