Spaces:
Sleeping
Sleeping
Download frontend/src/components/OutfitModal.tsx from Creatiste/taste-engine: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/frontend/src/components/OutfitModal.tsx
- Command line
-
hf download hf://spaces/Creatiste/taste-engine/frontend/src/components/OutfitModal.tsx
-
curl -L -o OutfitModal.tsx https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/frontend/src/components/OutfitModal.tsx
5.75 kB
| import { useEffect, useState } from "react"; | |
| import { getOutfit } from "../api"; | |
| import type { Outfit, OutfitComplement, PaletteSwatch, Product } from "../types"; | |
| const PLACEHOLDER = | |
| "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect fill='%23262626' width='100' height='100'/%3E%3Ctext x='50' y='55' text-anchor='middle' fill='%23737373' font-size='12'%3ENo Image%3C/text%3E%3C/svg%3E"; | |
| const BUCKET_LABEL: Record<string, string> = { | |
| top: "Top", | |
| bottom: "Bottom", | |
| dress: "Dress", | |
| outerwear: "Outerwear", | |
| shoes: "Shoes", | |
| bag: "Bag", | |
| accessory: "Accessory", | |
| intimates: "Intimates", | |
| swimwear: "Swimwear", | |
| }; | |
| function bucketLabel(b: string) { | |
| return BUCKET_LABEL[b] ?? b; | |
| } | |
| interface Props { | |
| anchorUrl: string; | |
| onClose: () => void; | |
| } | |
| function OutfitTile({ | |
| p, | |
| label, | |
| isAnchor = false, | |
| rationale, | |
| }: { | |
| p: Product; | |
| label: string; | |
| isAnchor?: boolean; | |
| rationale?: string; | |
| }) { | |
| return ( | |
| <a | |
| className={`outfit-tile${isAnchor ? " anchor" : ""}`} | |
| href={p.url} | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| > | |
| <div className="outfit-tile-label">{label}</div> | |
| <div className="outfit-tile-img-wrap"> | |
| <img | |
| className="outfit-tile-img" | |
| src={p.image_url} | |
| alt={p.name} | |
| onError={(e) => { | |
| (e.target as HTMLImageElement).src = PLACEHOLDER; | |
| }} | |
| /> | |
| </div> | |
| <div className="outfit-tile-body"> | |
| <div className="outfit-tile-brand">{p.brand || p.store}</div> | |
| <div className="outfit-tile-name">{p.name}</div> | |
| <div className="outfit-tile-price"> | |
| {p.price_text || `${Math.round(p.price_nok)} kr`} | |
| </div> | |
| {rationale && <div className="outfit-tile-rationale">{rationale}</div>} | |
| </div> | |
| </a> | |
| ); | |
| } | |
| function PaletteStrip({ palette }: { palette: PaletteSwatch[] }) { | |
| if (!palette || palette.length === 0) return null; | |
| return ( | |
| <div className="outfit-palette" aria-label="Outfit palette"> | |
| {palette.map((s, i) => ( | |
| <span | |
| key={`${s.source}-${i}`} | |
| className="outfit-palette-chip" | |
| style={{ backgroundColor: s.hex }} | |
| title={`${s.source} · ${s.name}`} | |
| /> | |
| ))} | |
| </div> | |
| ); | |
| } | |
| function StyleSummary({ outfit }: { outfit: Outfit }) { | |
| const axes = outfit.axes_covered; | |
| const contrasted = outfit.axes_contrasted; | |
| const isScandi = outfit.scandi_score >= 0.65; | |
| if (axes.length === 0 && !isScandi) return null; | |
| return ( | |
| <div className="outfit-summary"> | |
| {isScandi && <span className="outfit-tag scandi">Scandi-fit</span>} | |
| {axes.length > 0 && ( | |
| <span className="outfit-tag axes"> | |
| {axes.length} axis{axes.length === 1 ? "" : "es"} of style | |
| {contrasted.length > 0 ? `, ${contrasted.length} contrasted` : ""} | |
| </span> | |
| )} | |
| <span className="outfit-tag muted">{Math.round(outfit.scandi_score * 100)}% Scandi</span> | |
| </div> | |
| ); | |
| } | |
| export function OutfitModal({ anchorUrl, onClose }: Props) { | |
| const [outfit, setOutfit] = useState<Outfit | null>(null); | |
| const [error, setError] = useState<string | null>(null); | |
| const [loading, setLoading] = useState(true); | |
| useEffect(() => { | |
| setLoading(true); | |
| setError(null); | |
| getOutfit(anchorUrl) | |
| .then(setOutfit) | |
| .catch((e) => setError(String(e))) | |
| .finally(() => setLoading(false)); | |
| }, [anchorUrl]); | |
| useEffect(() => { | |
| function onKey(e: KeyboardEvent) { | |
| if (e.key === "Escape") onClose(); | |
| } | |
| window.addEventListener("keydown", onKey); | |
| return () => window.removeEventListener("keydown", onKey); | |
| }, [onClose]); | |
| return ( | |
| <div className="outfit-modal-backdrop" onClick={onClose}> | |
| <div className="outfit-modal" onClick={(e) => e.stopPropagation()}> | |
| <div className="outfit-modal-head"> | |
| <div> | |
| <h2>Shop the look</h2> | |
| {outfit && ( | |
| <p className="outfit-modal-subtitle"> | |
| Built around this {bucketLabel(outfit.anchor_bucket).toLowerCase()} | |
| </p> | |
| )} | |
| </div> | |
| <button className="outfit-modal-close" onClick={onClose} aria-label="Close"> | |
| × | |
| </button> | |
| </div> | |
| {loading && <div className="outfit-modal-state">Building the look…</div>} | |
| {error && <div className="outfit-modal-state error">Couldn't build outfit: {error}</div>} | |
| {outfit && ( | |
| <> | |
| <StyleSummary outfit={outfit} /> | |
| <PaletteStrip palette={outfit.palette} /> | |
| <div className="outfit-grid"> | |
| <OutfitTile | |
| p={outfit.anchor} | |
| label={`The ${bucketLabel(outfit.anchor_bucket).toLowerCase()}`} | |
| isAnchor | |
| /> | |
| {outfit.complements.map((c: OutfitComplement) => ( | |
| <OutfitTile | |
| key={c.product.url} | |
| p={c.product} | |
| label={`+ ${bucketLabel(c.bucket)}`} | |
| rationale={c.rationale} | |
| /> | |
| ))} | |
| </div> | |
| {outfit.complements.length === 0 ? ( | |
| <div className="outfit-modal-empty"> | |
| No complements found for this {bucketLabel(outfit.anchor_bucket).toLowerCase()} — | |
| try one with more catalog coverage. | |
| </div> | |
| ) : ( | |
| <div className="outfit-total"> | |
| <span>Total look</span> | |
| <span className="outfit-total-amount"> | |
| {Math.round(outfit.total_nok).toLocaleString()} kr | |
| </span> | |
| </div> | |
| )} | |
| </> | |
| )} | |
| </div> | |
| </div> | |
| ); | |
| } | |