SPB Git

Repository Public

ProductExplorer.tsx 6.0 KB 172 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : agentilab-web8 * File             : components/marketing/ProductExplorer.tsx9 * Module           : User Interface10 * Repository       : https://git.spboucher.ai/agentilab-web11 * Project Website  : Not specified12 * Created          : 2026-12-0513 * Last Modified    : 2026-12-0514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243'use client';4445import { useCallback, useSyncExternalStore } from 'react';46import type { ReactNode } from 'react';4748export interface ExplorerCard {49  id: string;50  slug: string;51  brand: string;52  platforms: string[];53  status: string;54  node: ReactNode;55}5657interface Option {58  value: string;59  label: string;60}6162interface ProductExplorerProps {63  cards: ExplorerCard[];64  groups: { key: 'brand' | 'platform' | 'status'; label: string; options: Option[] }[];65  labels: {66    title: string;67    all: string;68    reset: string;69    emptyTitle: string;70    emptyText: string;71    /** Deux gabarits (« {n} produit affiché » / « {n} produits affichés ») : une fonction ne traverse pas la frontière serveur → client. */72    countOne: string;73    countMany: string;74  };75}7677const EVENT = 'agl:filters';78const subscribe = (notify: () => void) => {79  window.addEventListener('popstate', notify);80  window.addEventListener(EVENT, notify);81  return () => {82    window.removeEventListener('popstate', notify);83    window.removeEventListener(EVENT, notify);84  };85};86const snapshot = () => window.location.search;87const serverSnapshot = () => '';8889/**90 * Filtres du portefeuille. L’état vit dans l’URL (?brand=…&platform=…&status=…) :91 * une sélection se partage par simple copie de l’adresse. Sans JavaScript, toutes92 * les cartes restent affichées et la barre de filtres est masquée (<noscript>).93 */94export function ProductExplorer({ cards, groups, labels }: ProductExplorerProps) {95  const search = useSyncExternalStore(subscribe, snapshot, serverSnapshot);96  const params = new URLSearchParams(search);97  const selected: Record<string, string> = {};98  for (const group of groups) {99    const value = params.get(group.key);100    // Une valeur inconnue dans l’URL est ignorée plutôt que de vider la liste.101    if (value && group.options.some((option) => option.value === value)) selected[group.key] = value;102  }103104  const update = useCallback((key: string | null, value: string | null) => {105    const next = new URLSearchParams(key ? window.location.search : '');106    if (key) {107      if (value) next.set(key, value);108      else next.delete(key);109    }110    const query = next.toString();111    window.history.replaceState(null, '', `${window.location.pathname}${query ? `?${query}` : ''}${window.location.hash}`);112    window.dispatchEvent(new Event(EVENT));113  }, []);114115  const visible = (card: ExplorerCard) =>116    (!selected.brand || card.brand === selected.brand) &&117    (!selected.platform || card.platforms.includes(selected.platform)) &&118    (!selected.status || card.status === selected.status);119  const count = cards.filter(visible).length;120  const active = Object.keys(selected).length > 0;121122  return (123    <>124      <form className="filters" aria-label={labels.title} onSubmit={(event) => event.preventDefault()}>125        {groups.map((group) => (126          <fieldset key={group.key}>127            <legend>{group.label}</legend>128            <button type="button" className="filter" aria-pressed={!selected[group.key]} onClick={() => update(group.key, null)}>129              {labels.all}130            </button>131            {group.options.map((option) => (132              <button133                key={option.value}134                type="button"135                className="filter"136                aria-pressed={selected[group.key] === option.value}137                onClick={() => update(group.key, selected[group.key] === option.value ? null : option.value)}138              >139                {option.label}140              </button>141            ))}142          </fieldset>143        ))}144        <div className="filters__foot">145          <p role="status" aria-live="polite">146            {(count === 1 ? labels.countOne : labels.countMany).replace('{n}', String(count))}147          </p>148          <button type="button" className="btn btn--secondary" disabled={!active} onClick={() => update(null, null)}>149            {labels.reset}150          </button>151        </div>152      </form>153      <ul className="products">154        {cards.map((card) => (155          <li key={card.id} id={card.slug} hidden={!visible(card)}>156            {card.node}157          </li>158        ))}159      </ul>160      {count === 0 ? (161        <div className="empty">162          <h3>{labels.emptyTitle}</h3>163          <p>{labels.emptyText}</p>164          <button type="button" className="btn btn--primary" onClick={() => update(null, null)}>165            {labels.reset}166          </button>167        </div>168      ) : null}169    </>170  );171}172
Files main
Menu

Theme