Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 2x 9x 9x 9x 7x 5x 5x 2x 2x 3x 3x 3x 3x 9x 9x 7x 9x 3x 3x 1x 2x 2x 2x 3x 3x 2x 3x 2x | import api from './api'
import { useItemsStore } from '../stores/items'
export interface HistorySuggestion {
name: string
quantityUnit: string | null
price: number | null
imageUrl: string | null
}
let debounceTimer: ReturnType<typeof setTimeout> | null = null
export function searchHistory(q: string, limit = 8): Promise<HistorySuggestion[]> {
return new Promise((resolve) => {
if (debounceTimer) clearTimeout(debounceTimer)
debounceTimer = setTimeout(async () => {
if (!q || q.length < 2) { resolve([]); return }
try {
const data = await api
.get<HistorySuggestion[]>('/items/history', { params: { q, limit } })
.then(r => r.data)
resolve(data)
} catch {
// Offline fallback: scan Pinia store
const itemsStore = useItemsStore()
const lower = q.toLowerCase()
const seen = new Map<string, HistorySuggestion>()
for (const item of Object.values(itemsStore.itemsByList).flat()) {
const n = item.name.trim().toLowerCase()
if (!n.startsWith(lower) || seen.has(n)) continue
seen.set(n, {
name: item.name,
quantityUnit: item.quantityUnit ?? null,
price: item.price != null ? Number(item.price) : null,
imageUrl: item.imageUrl ?? null,
})
if (seen.size >= limit) break
}
resolve(Array.from(seen.values()))
}
}, 200)
})
}
/** Load recent items (no prefix filter) for the library view. */
export function loadHistory(limit = 50): Promise<HistorySuggestion[]> {
return api
.get<HistorySuggestion[]>('/items/history', { params: { q: '', limit } })
.then(r => r.data)
.catch(() => {
const itemsStore = useItemsStore()
const seen = new Map<string, HistorySuggestion>()
for (const item of Object.values(itemsStore.itemsByList).flat()) {
const n = item.name.trim().toLowerCase()
if (seen.has(n)) continue
seen.set(n, {
name: item.name,
quantityUnit: item.quantityUnit ?? null,
price: item.price != null ? Number(item.price) : null,
imageUrl: item.imageUrl ?? null,
})
Iif (seen.size >= limit) break
}
return Array.from(seen.values())
})
}
|