SPB Git

Repository Public

FavButton.tsx 3.9 KB 131 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/components/FavButton.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/vrai-prix11 * Created          : 2026-11-2612 * Last Modified    : 2026-11-2613 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/**43 * Bouton ♥ : garde une propriété dans les favoris de l'appareil44 * (src/lib/store, localStorage). Aucun compte, aucun appel serveur.45 * Utilisable dans un <Link> parent (stopPropagation + preventDefault).46 */47"use client";48import { useLang } from "./LangContext";49import { favoris, useEstFavori, type FavoriKind } from "@/lib/store";5051export type FavPayload = {52  item_id: string;53  title: string;54  subtitle?: string;55  price_label?: string;56  image_url?: string;57  url?: string;58  kind?: FavoriKind;59};6061/** Chemin local de la fiche à partir d'une URL absolue ou relative. */62function cheminLocal(item: FavPayload): string {63  if (item.url) {64    if (item.url.startsWith("/")) return item.url;65    try {66      const u = new URL(item.url);67      return u.pathname + u.search;68    } catch {69      /* URL invalide : retombée ci-dessous */70    }71  }72  return `/estimation/${encodeURIComponent(item.item_id)}`;73}7475export default function FavButton({76  item,77  size = "sm",78  className = "",79}: {80  item: FavPayload;81  size?: "sm" | "lg";82  className?: string;83}) {84  const { lang } = useLang();85  const fr = lang === "fr";86  const fav = useEstFavori(item.item_id);8788  const onClick = (e: React.MouseEvent) => {89    e.preventDefault();90    e.stopPropagation();91    const url = cheminLocal(item);92    favoris.basculer({93      id: item.item_id,94      kind: item.kind ?? (url.startsWith("/a-vendre") ? "annonce" : "unite"),95      titre: item.title,96      sousTitre: item.subtitle,97      valeur: item.price_label,98      image: item.image_url,99      url,100    });101  };102103  const label = fav104    ? fr105      ? "Retirer de mes favoris"106      : "Remove from my favourites"107    : fr108      ? "Ajouter à mes favoris"109      : "Add to my favourites";110  const dims = size === "lg" ? "h-11 w-11 text-[19px]" : "h-8 w-8 text-[14px]";111112  return (113    <button114      type="button"115      onClick={onClick}116      aria-pressed={fav}117      aria-label={label}118      title={label}119      className={`flex shrink-0 items-center justify-center rounded-full border-[1.5px] border-ink leading-none transition-all ${dims} ${120        fav121          ? "bg-ink text-lime hover:bg-green-deep"122          : "bg-surface text-ink-3 hover:bg-lime-soft hover:text-ink"123      } ${className}`}124    >125      <span aria-hidden="true" className="translate-y-[-1px]">126        {fav ? "♥" : "♡"}127      </span>128    </button>129  );130}131
Files main
Menu

Theme