Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : ORIX App Universe8 * File : src/components/catalog/Catalog.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website : https://www.orixlab.dev12 * Created : 2026-11-2113 * Last Modified : 2026-11-2114 *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: support@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 : None specified (no license file in this repository)34 *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";44import { useEffect, useMemo, useState, type ReactNode } from "react";45import { filterApps, type CardApp, type CatalogQuery, type PlatformFilter, type SortKey } from "@/models/filter";46import { CATEGORIES, STATUSES, type Category, type Status } from "@/models/constants";4748type Labels = {49 search: string; searchPlaceholder: string; platform: string; category: string; status: string; sort: string; any: string; universal: string; none: string; reset: string;50 sorts: Record<SortKey, string>; platforms: Record<"ios" | "ipados" | "macos", string>; categories: Record<Category, string>; statuses: Record<Status, string>;51 resultsOne: string; resultsMany: string; forDevice: string; listLabel: string;52};5354const read = (): CatalogQuery => {55 const p = new URLSearchParams(location.search);56 return { q: p.get("q") ?? "", platform: (p.get("platform") ?? "") as PlatformFilter | "", category: (p.get("category") ?? "") as Category | "", status: (p.get("status") ?? "") as Status | "", sort: (p.get("sort") ?? "featured") as SortKey };57};5859/** Best-effort hint only: the visitor always chooses the platform; nothing is filtered automatically. */60function guessDevice(): "ios" | "ipados" | "macos" | null {61 const ua = navigator.userAgent, touch = navigator.maxTouchPoints > 1;62 if (/iPhone|iPod/.test(ua)) return "ios";63 if (/iPad/.test(ua) || (/Macintosh/.test(ua) && touch)) return "ipados";64 if (/Macintosh/.test(ua)) return "macos";65 return null;66}6768export function Catalog({ apps, labels, initial, cardsBySlug }: { apps: CardApp[]; labels: Labels; initial: CatalogQuery; cardsBySlug: Record<string, ReactNode> }) {69 const [query, setQuery] = useState<CatalogQuery>(initial);70 const [device, setDevice] = useState<"ios" | "ipados" | "macos" | null>(null);71 useEffect(() => { setDevice(guessDevice()); const pop = () => setQuery(read()); addEventListener("popstate", pop); return () => removeEventListener("popstate", pop); }, []);7273 const set = (patch: Partial<CatalogQuery>) => {74 const next = { ...query, ...patch };75 setQuery(next);76 const p = new URLSearchParams();77 for (const [k, v] of Object.entries(next)) if (v && !(k === "sort" && v === "featured")) p.set(k, v);78 history.replaceState(null, "", p.size ? `?${p}` : location.pathname);79 };80 const results = useMemo(() => filterApps(apps, query), [apps, query]);81 const dirty = !!(query.q || query.platform || query.category || query.status);82 const categories = CATEGORIES.filter((c) => apps.some((a) => a.category === c));83 const statuses = STATUSES.filter((s) => apps.some((a) => a.status === s));84 const platformOptions: [PlatformFilter | "", string][] = [["", labels.any], ["macos", labels.platforms.macos], ["ios", labels.platforms.ios], ["ipados", labels.platforms.ipados], ["universal", labels.universal]];8586 return (87 <div data-testid="catalog">88 <div className="card grid gap-5 p-5 sm:p-6">89 <div className="relative">90 <label htmlFor="catalog-q" className="sr-only">{labels.search}</label>91 <svg className="pointer-events-none absolute left-4 top-1/2 -translate-y-1/2 text-ink3" width="18" height="18" viewBox="0 0 18 18" fill="none" aria-hidden><circle cx="8" cy="8" r="5.2" stroke="currentColor" strokeWidth="1.6" /><path d="m12 12 3.6 3.6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" /></svg>92 <input id="catalog-q" type="search" inputMode="search" autoComplete="off" className="field !min-h-[3.25rem] !rounded-2xl !pl-12 !text-[1.02rem]" placeholder={labels.searchPlaceholder} value={query.q ?? ""} onChange={(e) => set({ q: e.target.value })} />93 </div>94 <div className="scroll-x -mx-1 flex items-center gap-2 px-1" role="group" aria-label={labels.platform}>95 {platformOptions.map(([v, l]) => <button key={v || "any"} type="button" className="chip" aria-pressed={(query.platform ?? "") === v} onClick={() => set({ platform: v })} data-testid={`platform-${v || "any"}`}>{l}</button>)}96 {device && query.platform !== device && <button type="button" className="chip !border-dashed" onClick={() => set({ platform: device })}>{labels.forDevice.replace("{d}", labels.platforms[device])}</button>}97 </div>98 <div className="grid gap-4 sm:grid-cols-3">99 <label><span className="label">{labels.category}</span>100 <select className="field" value={query.category ?? ""} onChange={(e) => set({ category: e.target.value as Category | "" })} data-testid="filter-category">101 <option value="">{labels.any}</option>{categories.map((c) => <option key={c} value={c}>{labels.categories[c]}</option>)}102 </select></label>103 <label><span className="label">{labels.status}</span>104 <select className="field" value={query.status ?? ""} onChange={(e) => set({ status: e.target.value as Status | "" })} data-testid="filter-status">105 <option value="">{labels.any}</option>{statuses.map((s) => <option key={s} value={s}>{labels.statuses[s]}</option>)}106 </select></label>107 <label><span className="label">{labels.sort}</span>108 <select className="field" value={query.sort ?? "featured"} onChange={(e) => set({ sort: e.target.value as SortKey })} data-testid="filter-sort">109 {(Object.keys(labels.sorts) as SortKey[]).map((s) => <option key={s} value={s}>{labels.sorts[s]}</option>)}110 </select></label>111 </div>112 </div>113114 <div className="mt-8 flex items-center justify-between gap-4">115 <p className="mono text-[0.78rem] uppercase tracking-[0.12em] text-ink3" role="status" data-testid="catalog-count">{(results.length === 1 ? labels.resultsOne : labels.resultsMany).replace("{n}", String(results.length))}</p>116 {dirty && <button type="button" className="link text-sm font-semibold" onClick={() => set({ q: "", platform: "", category: "", status: "" })}>{labels.reset}</button>}117 </div>118 {results.length === 0119 ? <p className="card mt-6 p-10 text-center text-ink2" data-testid="catalog-empty">{labels.none}</p>120 : <ul className="mt-6 grid gap-5 md:grid-cols-2 xl:grid-cols-3" aria-label={labels.listLabel}>{results.map((a) => <li key={a.slug}>{cardsBySlug[a.slug]}</li>)}</ul>}121 </div>122 );123}124