Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ const fetchJson = async <T>(url: string): Promise<T | null> => {
}
};

const normalizeCategory = (category: string): Resource["category"] => {
const normalizeCategory = (category: string): Resource["category"] | null => {
if (category === "mcicons") return "minecraft-icons";
if (category === "mcsounds") return "mcsounds";
if (category === "resources") return "images";
if (category === "resources") return null; // Skip: these are not images and shouldn't appear in the Images tab
return category as Resource["category"];
};

Expand All @@ -70,14 +70,15 @@ const inferSubcategory = (url: string, category: string): string | undefined =>
return undefined;
};

const normalizeApiResource = (item: ApiResource, category: string): Resource => {
const normalizeApiResource = (item: ApiResource, category: string): Resource | null => {
const normalizedCategory = normalizeCategory(category);
if (!normalizedCategory) return null; // Skip excluded categories
let subcategory: string | undefined = item.subcategory || undefined;

if (!subcategory && category === "presets") {
subcategory = inferSubcategory(item.url, category);
}

return {
id: item.id,
title: item.title,
Expand Down Expand Up @@ -109,7 +110,7 @@ export const fetchCategory = async (category: string): Promise<Resource[]> => {
const cacheKey = `${CATEGORY_CACHE_PREFIX}${category}`;
const cached = readCache<ApiResource[]>(cacheKey);
if (cached && cached.length > 0) {
return cached.map(item => normalizeApiResource(item, category));
return cached.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null);
}

const apiCategory = toApiCategory(category);
Expand All @@ -122,7 +123,7 @@ export const fetchCategory = async (category: string): Promise<Resource[]> => {
} catch (e) {
console.warn(`Failed to cache category ${category}:`, e);
}
return data.files.map(item => normalizeApiResource(item, category));
return data.files.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null);
}
console.error(`Failed to fetch category ${category} from API`);
return [];
Expand All @@ -134,13 +135,13 @@ export const fetchAllResources = async (): Promise<Resource[]> => {
const categoryNames = Object.keys(cached.categories);
const existingCategoriesCache = readCache<ApiCategories>(CATEGORIES_CACHE_KEY);
if (!existingCategoriesCache) {
writeCache(CATEGORIES_CACHE_KEY, {
categories: categoryNames,
total: categoryNames.length
writeCache(CATEGORIES_CACHE_KEY, {
categories: categoryNames,
total: categoryNames.length
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
return Object.entries(cached.categories).flatMap(([category, items]) =>
items.map(item => normalizeApiResource(item, category))
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
);
}

Expand All @@ -149,14 +150,15 @@ export const fetchAllResources = async (): Promise<Resource[]> => {
try {
writeCache(ALL_RESOURCES_CACHE_KEY, data);
const categoryNames = Object.keys(data.categories);
writeCache(CATEGORIES_CACHE_KEY, {
categories: categoryNames,
total: categoryNames.length
writeCache(CATEGORIES_CACHE_KEY, {
categories: categoryNames,
total: categoryNames.length
});
Object.entries(data.categories).forEach(([category, items]) => {
if (items && items.length > 0) {
const normalized = normalizeCategory(category);
if (normalized && items && items.length > 0) {
try {
writeCache(`${CATEGORY_CACHE_PREFIX}${normalizeCategory(category)}`, items);
writeCache(`${CATEGORY_CACHE_PREFIX}${normalized}`, items);
} catch {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
// Ignore quota errors for individual category caches
}
Expand All @@ -166,7 +168,7 @@ export const fetchAllResources = async (): Promise<Resource[]> => {
console.warn("Failed to cache all resources (likely quota exceeded):", e);
}
return Object.entries(data.categories).flatMap(([category, items]) =>
items.map(item => normalizeApiResource(item, category))
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
);
}
console.error("Failed to fetch all resources from API");
Expand All @@ -178,7 +180,7 @@ export const refreshAllResources = async (): Promise<Resource[]> => {
if (data?.categories) {
writeCache(ALL_RESOURCES_CACHE_KEY, data);
return Object.entries(data.categories).flatMap(([category, items]) =>
items.map(item => normalizeApiResource(item, category))
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
);
}
return [];
Expand All @@ -190,7 +192,7 @@ export const getResourceByCategory = async (category: string): Promise<Resource[
if (allCached?.categories?.[apiCategory]) {
return allCached.categories[apiCategory].map(item =>
normalizeApiResource(item, category)
);
).filter((r): r is Resource => r !== null);
}

return fetchCategory(category);
Expand Down