feat(api): implement product caching mechanism
- Add NODE_ENV=production to .env for environment configuration - Enhance product API route with JSON cache manager for local storage and retrieval - Introduce cache-only mode and detailed logging for better performance and offline support - Update UI to display cache status and storage statistics
This commit is contained in:
1 parent
bddf19e5c8
commit
c173fec668
19 files changed
+9240
-175
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { Product } from "./openFoodFactsApi";
|
||||
|
||||
/**
|
||||
* Calcula score de relevância de um produto em relação à query
|
||||
*/
|
||||
export function calculateRelevanceScore(
|
||||
product: Product,
|
||||
searchTerm: string
|
||||
): number {
|
||||
const productName = product.product_name?.toLowerCase() || "";
|
||||
const lowerSearchTerm = searchTerm.toLowerCase();
|
||||
|
||||
// Nome exato = maior prioridade
|
||||
if (productName === lowerSearchTerm) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Nome começa com a query = alta prioridade
|
||||
if (productName.startsWith(lowerSearchTerm)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Nome contém a query = média prioridade
|
||||
if (productName.includes(lowerSearchTerm)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Correspondência em marca ou categoria = baixa prioridade
|
||||
if (
|
||||
product.brands?.toLowerCase().includes(lowerSearchTerm) ||
|
||||
product.categories?.toLowerCase().includes(lowerSearchTerm)
|
||||
) {
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordena produtos por relevância em relação à query
|
||||
*/
|
||||
export function sortByRelevance(
|
||||
products: Product[],
|
||||
searchTerm: string
|
||||
): Product[] {
|
||||
return products
|
||||
.map((product) => ({
|
||||
product,
|
||||
relevanceScore: calculateRelevanceScore(product, searchTerm),
|
||||
}))
|
||||
.sort((a, b) => b.relevanceScore - a.relevanceScore)
|
||||
.map(({ product }) => product);
|
||||
}
|
||||
Reference in new issue
Block a user