A local-first personal finance app
Add error styling to ThemeInstaller component (#6689)
Matiss Janis Aboltinse6e108ffbd07
diff --git a/packages/desktop-client/src/components/settings/ThemeInstaller.test.tsx b/packages/desktop-client/src/components/settings/ThemeInstaller.test.tsxindex 96f07ddb43..d4aa935134 100644--- a/packages/desktop-client/src/components/settings/ThemeInstaller.test.tsx+++ b/packages/desktop-client/src/components/settings/ThemeInstaller.test.tsx@@ -7,6 +7,7 @@ import { ThemeInstaller } from './ThemeInstaller';import { useThemeCatalog } from '@desktop-client/hooks/useThemeCatalog';import {fetchThemeCss,+ generateThemeId,validateThemeCss,} from '@desktop-client/style/customThemes';@@ -16,6 +17,13 @@ vi.mock('@desktop-client/style/customThemes', async () => {...actual,fetchThemeCss: vi.fn(),validateThemeCss: vi.fn(),+ generateThemeId: vi.fn((repo: string) => {+ // Generate predictable IDs for testing+ if (repo.includes('demo-theme')) return 'theme-demo123';+ if (repo.includes('ocean-theme')) return 'theme-ocean456';+ if (repo.includes('forest-theme')) return 'theme-forest789';+ return `theme-${repo.replace(/[^a-z0-9]/gi, '')}`;+ }),normalizeGitHubRepo: vi.fn((repo: string) =>repo.startsWith('http') ? repo : `https://github.com/${repo}`,),@@ -60,6 +68,13 @@ describe('ThemeInstaller', () => {mockOnClose.mockClear();vi.mocked(fetchThemeCss).mockResolvedValue(mockValidCss);vi.mocked(validateThemeCss).mockImplementation(css => css.trim());+ // Reset generateThemeId mock to default behavior+ vi.mocked(generateThemeId).mockImplementation((repo: string) => {+ if (repo.includes('demo-theme')) return 'theme-demo123';+ if (repo.includes('ocean-theme')) return 'theme-ocean456';+ if (repo.includes('forest-theme')) return 'theme-forest789';+ return `theme-${repo.replace(/[^a-z0-9]/gi, '')}`;+ });// Mock useThemeCatalog to return catalog data immediatelyvi.mocked(useThemeCatalog).mockReturnValue({@@ -191,6 +206,66 @@ describe('ThemeInstaller', () => {expect(testOnInstall).not.toHaveBeenCalled();});+ it('shows error styling on erroring theme and keeps previous active theme active', async () => {+ const user = userEvent.setup();+ const validationError = 'Invalid CSS format';++ // Set up a previously installed theme (Ocean Blue)+ const installedTheme = {+ id: 'theme-ocean456',+ name: 'Ocean Blue',+ repo: 'https://github.com/actualbudget/ocean-theme',+ cssContent: mockValidCss,+ };++ // Make validation fail for Demo Theme+ vi.mocked(validateThemeCss).mockImplementationOnce(() => {+ throw new Error(validationError);+ });++ render(+ <ThemeInstaller+ onInstall={mockOnInstall}+ onClose={mockOnClose}+ installedTheme={installedTheme}+ />,+ );++ // Click on Demo Theme which will fail validation+ const demoThemeButton = screen.getByRole('button', {+ name: 'Demo Theme',+ });+ await user.click(demoThemeButton);++ // Wait for error to appear+ await waitFor(() => {+ expect(screen.getByText(validationError)).toBeInTheDocument();+ });++ // Verify erroring theme (Demo Theme) has error styling+ await waitFor(() => {+ const demoButton = screen.getByRole('button', { name: 'Demo Theme' });+ const demoButtonStyle = demoButton.getAttribute('style') || '';+ // Check that error styling is applied - should contain CSS variable for errorText in border+ // and errorBackground in backgroundColor+ // The style will contain something like: border: "2px solid var(--color-errorText)"+ expect(demoButtonStyle).toMatch(/errorText/);+ expect(demoButtonStyle).toMatch(/errorBackground/);+ });++ // Verify previously active theme (Ocean Blue) still shows as active (not the erroring one)+ const oceanButton = screen.getByRole('button', { name: 'Ocean Blue' });+ const oceanButtonStyle = oceanButton.getAttribute('style') || '';+ // Active theme should have buttonPrimaryBackground in border (indicating it's active)+ expect(oceanButtonStyle).toMatch(/buttonPrimaryBackground/);+ // Should not have error styling - the previous active theme should remain active+ expect(oceanButtonStyle).not.toMatch(/errorText/);+ expect(oceanButtonStyle).not.toMatch(/errorBackground/);++ // Verify onInstall was not called (theme installation failed)+ expect(mockOnInstall).not.toHaveBeenCalled();+ });+it('displays generic error when fetchThemeCss fails with non-Error object', async () => {const user = userEvent.setup();vi.mocked(fetchThemeCss).mockRejectedValue('String error');diff --git a/packages/desktop-client/src/components/settings/ThemeInstaller.tsx b/packages/desktop-client/src/components/settings/ThemeInstaller.tsxindex c418992a3e..409de31fed 100644--- a/packages/desktop-client/src/components/settings/ThemeInstaller.tsx+++ b/packages/desktop-client/src/components/settings/ThemeInstaller.tsx@@ -5,6 +5,7 @@ import AutoSizer from 'react-virtualized-auto-sizer';import { Button } from '@actual-app/components/button';import { AnimatedLoading } from '@actual-app/components/icons/AnimatedLoading';+import { baseInputStyle } from '@actual-app/components/input';import { SpaceBetween } from '@actual-app/components/space-between';import { Text } from '@actual-app/components/text';import { theme as themeStyle } from '@actual-app/components/theme';@@ -44,6 +45,7 @@ export function ThemeInstaller({const { t } = useTranslation();const [selectedCatalogTheme, setSelectedCatalogTheme] =useState<CatalogTheme | null>(null);+ const [erroringTheme, setErroringTheme] = useState<CatalogTheme | null>(null);const [pastedCss, setPastedCss] = useState('');const [isLoading, setIsLoading] = useState(false);const [error, setError] = useState<string | null>(null);@@ -75,6 +77,21 @@ export function ThemeInstaller({);}, []);+ // Helper function to check if a catalog theme matches an installed theme+ const isCatalogThemeInstalled = useCallback(+ (catalogTheme: CatalogTheme): boolean => {+ if (!installedTheme) return false;+ try {+ const normalizedRepo = normalizeGitHubRepo(catalogTheme.repo);+ const themeId = generateThemeId(normalizedRepo);+ return themeId === installedTheme.id;+ } catch {+ return false;+ }+ },+ [installedTheme],+ );+const installTheme = useCallback(async (options: {css: string | Promise<string>;@@ -82,8 +99,10 @@ export function ThemeInstaller({repo: string;id: string;errorMessage: string;+ catalogTheme?: CatalogTheme | null;}) => {setError(null);+ setErroringTheme(null);setIsLoading(true);try {@@ -98,8 +117,17 @@ export function ThemeInstaller({cssContent: validatedCss,};onInstall(installedTheme);+ // Only set selectedCatalogTheme on success if it's a catalog theme+ if (options.catalogTheme) {+ setSelectedCatalogTheme(options.catalogTheme);+ }} catch (err) {setError(err instanceof Error ? err.message : options.errorMessage);+ // Track which theme failed and clear selection+ if (options.catalogTheme) {+ setErroringTheme(options.catalogTheme);+ }+ setSelectedCatalogTheme(null);} finally {setIsLoading(false);}@@ -109,8 +137,8 @@ export function ThemeInstaller({const handleCatalogThemeClick = useCallback(async (theme: CatalogTheme) => {- setSelectedCatalogTheme(theme);setPastedCss('');+ setSelectedCatalogTheme(theme);const normalizedRepo = normalizeGitHubRepo(theme.repo);await installTheme({@@ -119,6 +147,7 @@ export function ThemeInstaller({repo: normalizedRepo,id: generateThemeId(normalizedRepo),errorMessage: t('Failed to load theme'),+ catalogTheme: theme,});},[installTheme, t],@@ -127,6 +156,7 @@ export function ThemeInstaller({const handlePastedCssChange = useCallback((value: string) => {setPastedCss(value);setSelectedCatalogTheme(null);+ setErroringTheme(null);setError(null);}, []);@@ -239,6 +269,10 @@ export function ThemeInstaller({}}>{rowThemes.map((theme, themeIndex) => {+ const isActive = isCatalogThemeInstalled(theme);+ const hasError =+ erroringTheme?.name === theme.name &&+ erroringTheme?.repo === theme.repo;const isSelected =selectedCatalogTheme?.name === theme.name &&selectedCatalogTheme?.repo === theme.repo;@@ -257,13 +291,17 @@ export function ThemeInstaller({padding: 8,borderRadius: 6,border: `2px solid ${- isSelected- ? themeStyle.buttonPrimaryBackground- : themeStyle.tableBorder+ hasError+ ? themeStyle.errorText+ : isActive+ ? themeStyle.buttonPrimaryBackground+ : themeStyle.tableBorder}`,- backgroundColor: isSelected- ? themeStyle.tableRowBackgroundHover- : 'transparent',+ backgroundColor: hasError+ ? themeStyle.errorBackground+ : isActive+ ? themeStyle.tableRowBackgroundHover+ : 'transparent',flexDirection: 'column',alignItems: 'center',gap: 8,@@ -374,6 +412,7 @@ export function ThemeInstaller({placeholder={t(':root {\n --color-sidebarItemSelected: #007bff;\n}')}aria-label={t('Custom Theme CSS')}style={{+ ...baseInputStyle,height: 120,resize: 'vertical',}}diff --git a/packages/desktop-client/src/data/customThemeCatalog.json b/packages/desktop-client/src/data/customThemeCatalog.jsonindex 9b923b0358..c41b200b08 100644--- a/packages/desktop-client/src/data/customThemeCatalog.json+++ b/packages/desktop-client/src/data/customThemeCatalog.json@@ -11,6 +11,14 @@"name": "Miami Beach","repo": "Juulz/miami-beach"},+ {+ "name": "Simple Dark",+ "repo": "Juulz/simple-dark"+ },+ {+ "name": "Matrix",+ "repo": "MatissJanis/actualbudget-matrix-theme"+ },{"name": "Black Gold","repo": "MikesGlitch/actual-black-gold-theme"diff --git a/upcoming-release-notes/6689.md b/upcoming-release-notes/6689.mdnew file mode 100644index 0000000000..51ebdd9ae8--- /dev/null+++ b/upcoming-release-notes/6689.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [MatissJanis]+---++Themes: improved error handling and more themes in the catalog.