|
1 | 1 | import { UnifiedFoodResult } from '../hooks/useUnifiedFoodSearch'; |
2 | 2 | import i18n from '../lang/lang'; |
3 | | -import { SearchResultProduct, SuccessFoodProductState } from '../types/openFoodFacts'; |
| 3 | +import { |
| 4 | + ProductNameFields, |
| 5 | + ProductV3, |
| 6 | + SearchResultProduct, |
| 7 | + SuccessFoodProductState, |
| 8 | +} from '../types/openFoodFacts'; |
4 | 9 |
|
5 | 10 | // All possible Open Food Facts nutriment properties |
6 | 11 | const NUTRIMENT_PROPERTIES = [ |
@@ -438,41 +443,57 @@ export function mapOpenFoodFactsProduct(product: SearchResultProduct): UnifiedFo |
438 | 443 | }; |
439 | 444 | } |
440 | 445 |
|
441 | | -// TODO: instead of any, use the possible types passed to this function (by scanning the codebase) |
442 | | -export function getProductName(data: any): string { |
443 | | - // Handle search results (array) vs single product (object) |
| 446 | +/** |
| 447 | + * All possible types passed to getProductName across the codebase: |
| 448 | + * - SearchResultProduct (V2 search result item) |
| 449 | + * - ProductV3 (V3 product, e.g. from FoodService.createFromV3Product) |
| 450 | + * - SuccessFoodProductState (V3 barcode-detail response with .product) |
| 451 | + * - Wrapper shapes from API: { product } or { products: [...] } |
| 452 | + */ |
| 453 | +export type GetProductNameInput = |
| 454 | + | SearchResultProduct |
| 455 | + | ProductV3 |
| 456 | + | SuccessFoodProductState |
| 457 | + | { product?: SearchResultProduct | ProductV3 } |
| 458 | + | { products?: (SearchResultProduct | ProductV3)[] }; |
| 459 | + |
| 460 | +export function getProductName(data: GetProductNameInput | null | undefined): string { |
| 461 | + // OFF API: single product has .product, search has .products[], or payload is the product itself |
| 462 | + type WithOptional = { product?: unknown; products?: unknown[] }; |
444 | 463 | const product = |
445 | | - data?.product || (Array.isArray(data?.products) ? data.products[0] : data); |
| 464 | + (data as WithOptional)?.product || |
| 465 | + (Array.isArray((data as WithOptional)?.products) ? (data as WithOptional).products?.[0] : data); |
446 | 466 |
|
447 | 467 | if (!product) { |
448 | 468 | return i18n.t('food.unknownFood'); |
449 | 469 | } |
450 | 470 |
|
451 | | - // 1. Try the standard name fields |
452 | | - let name = |
453 | | - product.product_name || |
454 | | - product[`product_name_${product.lang}`] || // Dynamic lookup based on product's main lang |
455 | | - product.product_name_en || |
456 | | - product.product_name_nl || |
457 | | - product.product_name_fr || |
458 | | - product.product_name_de; |
| 471 | + const p = product as ProductNameFields; |
459 | 472 |
|
460 | | - // 2. Fallback to Abbreviated Name (Receipt/Small UI names) |
| 473 | + // 1. Try the standard name fields (OFF: product_name, product_name_LANG) |
| 474 | + let name: string | undefined = |
| 475 | + p.product_name || |
| 476 | + p[`product_name_${p.lang}`] || // Dynamic lookup based on product's main lang |
| 477 | + p.product_name_en || |
| 478 | + p.product_name_nl || |
| 479 | + p.product_name_fr || |
| 480 | + p.product_name_de; |
| 481 | + |
| 482 | + // 2. Fallback to Abbreviated Name (OFF: abbreviated_product_name, receipt/small UI names) |
461 | 483 | if (!name) { |
462 | | - name = product.abbreviated_product_name; |
| 484 | + name = p.abbreviated_product_name; |
463 | 485 | } |
464 | 486 |
|
465 | | - // 3. Fallback to Generic Names (Common names) |
| 487 | + // 3. Fallback to Generic Names (OFF: generic_name, generic_name_LANG) |
466 | 488 | if (!name) { |
467 | | - name = |
468 | | - product.generic_name || product[`generic_name_${product.lang}`] || product.generic_name_en; |
| 489 | + name = p.generic_name || p[`generic_name_${p.lang}`] || p.generic_name_en; |
469 | 490 | } |
470 | 491 |
|
471 | 492 | // 4. Ultimate Fallback: Brand + Category |
472 | 493 | // Returns something like "Milbona (Yogurts)" instead of "Unknown" |
473 | | - if (!name && product.brands) { |
474 | | - const category = product.categories?.split(',')[0]; // Take the first category |
475 | | - name = category ? `${product.brands} (${category})` : product.brands; |
| 494 | + if (!name && p.brands) { |
| 495 | + const category = p.categories?.split(',')[0]; // Take the first category |
| 496 | + name = category ? `${p.brands} (${category})` : p.brands; |
476 | 497 | } |
477 | 498 |
|
478 | 499 | return name?.trim() || i18n.t('food.unknownFood'); |
|
0 commit comments