From f60245b658da5d331ed963f89b222c00fbe99081 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 2 Apr 2026 15:22:07 -0500 Subject: [PATCH] query catalog file instead of gh api --- .../cosmetics/ThemeCatalogSettings.tsx | 5 +- src/app/hooks/useClientConfig.ts | 1 + src/app/theme/catalog.test.ts | 167 ++++++++++++++---- src/app/theme/catalog.ts | 64 ++++++- 4 files changed, 199 insertions(+), 38 deletions(-) diff --git a/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx b/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx index 6aa267ab1..aff9dbe6f 100644 --- a/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx +++ b/src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx @@ -60,6 +60,7 @@ export function ThemeCatalogSettings({ const patchSettings = usePatchSettings(); const configBase = clientConfig.themeCatalogBaseUrl?.trim(); const catalogBase = themeCatalogListingBaseUrl(configBase); + const catalogManifestUrl = clientConfig.themeCatalogManifestUrl?.trim() || undefined; const isAppearanceMode = mode === 'appearance'; const [browseOpen, setBrowseOpen] = useState(false); @@ -125,8 +126,8 @@ export function ThemeCatalogSettings({ ); const pairsQuery = useQuery({ - queryKey: ['theme-catalog-pairs', catalogBase], - queryFn: () => listThemePairsFromCatalog(catalogBase), + queryKey: ['theme-catalog-pairs', catalogBase, catalogManifestUrl ?? ''], + queryFn: () => listThemePairsFromCatalog(catalogBase, { manifestUrl: catalogManifestUrl }), enabled: isRemoteMode, staleTime: 5 * 60_000, }); diff --git a/src/app/hooks/useClientConfig.ts b/src/app/hooks/useClientConfig.ts index fe551a7d5..441a7fd49 100644 --- a/src/app/hooks/useClientConfig.ts +++ b/src/app/hooks/useClientConfig.ts @@ -45,6 +45,7 @@ export type ClientConfig = { settingsLinkBaseUrl?: string; themeCatalogBaseUrl?: string; + themeCatalogManifestUrl?: string; themeCatalogApprovedHostPrefixes?: string[]; }; diff --git a/src/app/theme/catalog.test.ts b/src/app/theme/catalog.test.ts index baf116422..cede007ca 100644 --- a/src/app/theme/catalog.test.ts +++ b/src/app/theme/catalog.test.ts @@ -1,5 +1,24 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { listThemePairsFromCatalog } from './catalog'; +import { listThemePairsFromCatalog, themeCatalogManifestUrlFromBase } from './catalog'; + +describe('themeCatalogManifestUrlFromBase', () => { + it('resolves catalog.json beside raw root', () => { + expect( + themeCatalogManifestUrlFromBase('https://raw.githubusercontent.com/SableClient/themes/main/') + ).toBe('https://raw.githubusercontent.com/SableClient/themes/main/catalog.json'); + expect( + themeCatalogManifestUrlFromBase('https://raw.githubusercontent.com/SableClient/themes/main') + ).toBe('https://raw.githubusercontent.com/SableClient/themes/main/catalog.json'); + }); + + it('resolves under nested path', () => { + expect( + themeCatalogManifestUrlFromBase( + 'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/' + ) + ).toBe('https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/catalog.json'); + }); +}); describe('listThemePairsFromCatalog', () => { const origFetch = globalThis.fetch; @@ -8,67 +27,145 @@ describe('listThemePairsFromCatalog', () => { globalThis.fetch = origFetch; }); - it('pairs preview and full theme files from GitHub contents response', async () => { + it('loads pairs from catalog.json manifest when present', async () => { + const base = 'https://raw.githubusercontent.com/SableClient/themes/main/'; globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, - json: async () => [ - { - name: 'rose-pine.preview.sable.css', - path: 'rose-pine.preview.sable.css', - type: 'file', - download_url: - 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css', - }, - { - name: 'rose-pine.sable.css', - path: 'rose-pine.sable.css', - type: 'file', - download_url: - 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css', - }, - { - name: 'README.md', - path: 'README.md', - type: 'file', - download_url: null, - }, - ], + json: async () => ({ + version: 1, + themes: [ + { + basename: 'rose-pine', + previewUrl: + 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css', + fullUrl: + 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css', + }, + ], + }), }); + const pairs = await listThemePairsFromCatalog(base); + + expect(pairs).toHaveLength(1); + expect(pairs[0].basename).toBe('rose-pine'); + expect(globalThis.fetch).toHaveBeenCalledTimes(1); + expect(globalThis.fetch).toHaveBeenCalledWith( + 'https://raw.githubusercontent.com/SableClient/themes/main/catalog.json', + expect.objectContaining({ mode: 'cors' }) + ); + }); + + it('uses custom manifest URL when provided', async () => { + globalThis.fetch = vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ + themes: [ + { + basename: 'x', + previewUrl: 'https://cdn.example/p.preview.sable.css', + fullUrl: 'https://cdn.example/p.sable.css', + }, + ], + }), + }); + + const pairs = await listThemePairsFromCatalog( + 'https://raw.githubusercontent.com/SableClient/themes/main/', + { + manifestUrl: 'https://pages.example.com/catalog.json', + } + ); + + expect(pairs[0].basename).toBe('x'); + expect(globalThis.fetch).toHaveBeenCalledWith( + 'https://pages.example.com/catalog.json', + expect.anything() + ); + }); + + it('returns empty array when manifest is valid but themes is empty', async () => { + globalThis.fetch = vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ version: 1, themes: [] }), + }); + + const pairs = await listThemePairsFromCatalog( + 'https://raw.githubusercontent.com/SableClient/themes/main/' + ); + expect(pairs).toEqual([]); + expect(globalThis.fetch).toHaveBeenCalledTimes(1); + }); + + it('falls back to GitHub contents API when manifest fetch fails', async () => { + globalThis.fetch = vi + .fn() + .mockResolvedValueOnce({ ok: false, status: 404 }) + .mockResolvedValueOnce({ + ok: true, + json: async () => [ + { + name: 'rose-pine.preview.sable.css', + path: 'rose-pine.preview.sable.css', + type: 'file', + download_url: + 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css', + }, + { + name: 'rose-pine.sable.css', + path: 'rose-pine.sable.css', + type: 'file', + download_url: + 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css', + }, + ], + }); + const base = 'https://raw.githubusercontent.com/SableClient/themes/main/'; const pairs = await listThemePairsFromCatalog(base); expect(pairs).toHaveLength(1); expect(pairs[0].basename).toBe('rose-pine'); - expect(pairs[0].fullUrl).toContain('rose-pine.sable.css'); - expect(pairs[0].previewUrl).toContain('rose-pine.preview.sable.css'); - + expect(globalThis.fetch).toHaveBeenCalledWith( + 'https://raw.githubusercontent.com/SableClient/themes/main/catalog.json', + expect.objectContaining({ mode: 'cors' }) + ); expect(globalThis.fetch).toHaveBeenCalledWith( 'https://api.github.com/repos/SableClient/themes/contents?ref=main', expect.objectContaining({ headers: { Accept: 'application/vnd.github+json' } }) ); }); - it('requests nested directory when catalog URL includes a path', async () => { - globalThis.fetch = vi.fn().mockResolvedValue({ - ok: true, - json: async () => [], - }); + it('requests nested directory when catalog URL includes a path and manifest is missing', async () => { + globalThis.fetch = vi + .fn() + .mockResolvedValueOnce({ ok: false, status: 404 }) + .mockResolvedValueOnce({ + ok: true, + json: async () => [], + }); await listThemePairsFromCatalog( 'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/' ); + expect(globalThis.fetch).toHaveBeenCalledWith( + 'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/catalog.json', + expect.anything() + ); expect(globalThis.fetch).toHaveBeenCalledWith( 'https://api.github.com/repos/SableClient/themes/contents/dist/themes?ref=main', expect.anything() ); }); - it('returns empty array when base URL is not a raw GitHub URL', async () => { - globalThis.fetch = vi.fn(); + it('returns empty array when base URL is not a raw GitHub URL and manifest fails', async () => { + globalThis.fetch = vi.fn().mockResolvedValue({ ok: false, status: 404 }); const pairs = await listThemePairsFromCatalog('https://example.com/'); expect(pairs).toEqual([]); - expect(globalThis.fetch).not.toHaveBeenCalled(); + expect(globalThis.fetch).toHaveBeenCalledWith( + 'https://example.com/catalog.json', + expect.anything() + ); }); }); diff --git a/src/app/theme/catalog.ts b/src/app/theme/catalog.ts index 831fefa23..739dbd09d 100644 --- a/src/app/theme/catalog.ts +++ b/src/app/theme/catalog.ts @@ -13,9 +13,61 @@ export type ThemePair = { fullUrl: string; }; +export type ThemeCatalogManifest = { + version?: number; + themes?: ThemePair[]; +}; + +export type ListThemeCatalogOptions = { + manifestUrl?: string | null; +}; + const PREVIEW_SUFFIX = '.preview.sable.css'; const FULL_SUFFIX = '.sable.css'; +export function themeCatalogManifestUrlFromBase(catalogBaseUrl: string): string | null { + const trimmed = catalogBaseUrl.trim(); + if (!trimmed) return null; + const base = trimmed.endsWith('/') ? trimmed : `${trimmed}/`; + try { + return new URL('catalog.json', base).href; + } catch { + return null; + } +} + +async function fetchThemePairsFromManifest(manifestUrl: string): Promise { + const res = await fetch(manifestUrl, { mode: 'cors' }); + if (!res.ok) return null; + let data: unknown; + try { + data = await res.json(); + } catch { + return null; + } + if (!data || typeof data !== 'object') return null; + const themes = (data as ThemeCatalogManifest).themes; + if (!Array.isArray(themes)) return null; + const pairs: ThemePair[] = []; + for (const row of themes) { + if (!row || typeof row !== 'object') continue; + const o = row as Record; + const basename = o.basename; + const previewUrl = o.previewUrl; + const fullUrl = o.fullUrl; + if ( + typeof basename !== 'string' || + typeof previewUrl !== 'string' || + typeof fullUrl !== 'string' + ) { + continue; + } + if (!basename || !previewUrl || !fullUrl) continue; + pairs.push({ basename, previewUrl, fullUrl }); + } + return pairs; +} + /** Path segments for GET /repos/.../contents/{path} */ function directoryPathToApiSegment(directoryPath: string): string { if (!directoryPath) return ''; @@ -40,7 +92,17 @@ async function fetchGithubContents(parts: GithubRawParts): Promise { +export async function listThemePairsFromCatalog( + baseUrl: string, + options?: ListThemeCatalogOptions +): Promise { + const manifestUrl = + options?.manifestUrl?.trim() || themeCatalogManifestUrlFromBase(baseUrl) || undefined; + if (manifestUrl) { + const fromManifest = await fetchThemePairsFromManifest(manifestUrl); + if (fromManifest !== null) return fromManifest; + } + const parts = parseGithubRawBaseUrl(baseUrl); if (!parts) return []; const items = await fetchGithubContents(parts);