Skip to content

Commit

Permalink
Price fetcher nits. Backdrop blur on modal content. Update price fetc…
Browse files Browse the repository at this point in the history
…her types in keys
  • Loading branch information
IzaakPrats committed Mar 1, 2025
1 parent ac10a7f commit 7d4ce04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/lib/components/inventory/list_item_modal_styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const listItemModalStyles = [
.modal-content {
background: rgba(21, 23, 28, 0.8);
backdrop-filter: blur(16px)
padding: 20px;
width: 500px;
max-width: 90%;
Expand Down
23 changes: 14 additions & 9 deletions src/lib/services/price_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ interface DopplerPriceListResponse {
}

interface PriceCache {
timestamp: number;
lastUpdated: number;
prices: Record<string, number>;
dopplerPrices: Record<string, Record<number, number>>;
}

class PriceFetcher {
async fetch(market_hash_name: string, paintIndex?: number): Promise<number> {
async fetch(marketHashName: string, paintIndex?: number): Promise<number> {
const {prices, dopplerPrices} = await this.getValidPrices();

// If it's a Doppler and we have a paint index, use the Doppler price
if (paintIndex !== undefined) {
const dopplerPrice = dopplerPrices[market_hash_name]?.[paintIndex];
const dopplerPrice = dopplerPrices[marketHashName]?.[paintIndex];
if (dopplerPrice) {
return dopplerPrice;
}
}

return prices[market_hash_name] || 0;
return prices[marketHashName] || 0;
}

private async getValidPrices(): Promise<{
Expand All @@ -44,7 +44,7 @@ class PriceFetcher {

// Try loading from storage first
const storedCache = await gStore.getWithStorage<PriceCache>(chrome.storage.local, PRICE_CACHE.key);
if (storedCache && now - storedCache.timestamp < DEFAULT_CACHE_DURATION) {
if (storedCache && now - storedCache.lastUpdated < DEFAULT_CACHE_DURATION) {
return {
prices: storedCache.prices,
dopplerPrices: storedCache.dopplerPrices || {},
Expand Down Expand Up @@ -89,17 +89,22 @@ class PriceFetcher {
}

await gStore.setWithStorage(chrome.storage.local, PRICE_CACHE.key, {
timestamp: now,
lastUpdated: now,
prices,
dopplerPrices,
});

return {prices, dopplerPrices};
} catch (error) {
// On error, return existing cache regardless of age, or empty objects if no cache exists
// If we have no stored cache, bubble up the error
if (!storedCache) {
throw new Error('Failed to fetch prices and no cached data available');
}

// On error with existing cache, return existing cache regardless of age
return {
prices: storedCache?.prices || {},
dopplerPrices: storedCache?.dopplerPrices || {},
prices: storedCache.prices || {},
dopplerPrices: storedCache.dopplerPrices || {},
};
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/storage/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ export const PAGE_SIZE = newRow<number>(StorageKey.PAGE_SIZE);
// Dynamic prefixes should be the market hash name of the item
export const DYNAMIC_ITEM_FILTERS = newDynamicRow<SerializedFilter[]>(StorageKey.ITEM_FILTERS);
export const GLOBAL_FILTERS = newRow<SerializedFilter[]>(StorageKey.GLOBAL_FILTERS);
export const PRICE_CACHE = newRow<{timestamp: number; prices: Record<string, number>}>(StorageKey.PRICE_CACHE);
export const PRICE_CACHE = newRow<{
lastUpdated: number;
prices: Record<string, number>;
dopplerPrices?: Record<string, Record<number, number>>;
}>(StorageKey.PRICE_CACHE);

0 comments on commit 7d4ce04

Please sign in to comment.