|
9 | 9 | Trash2Icon, |
10 | 10 | UploadIcon, |
11 | 11 | } from "lucide-react"; |
12 | | -import { useCallback, useEffect, useState, type ReactElement } from "react"; |
| 12 | +import { useState, type ReactElement } from "react"; |
13 | 13 | import { useEnvironmentThemeDefinitions } from "../../hooks/useEnvironmentTheme"; |
14 | 14 | import { readThemeHalvesRaw } from "../../hooks/useTheme"; |
15 | 15 | import { cn } from "../../lib/utils"; |
@@ -442,9 +442,7 @@ function CustomThemeCollectionCard({ |
442 | 442 | const safeIndex = Math.min(variantIndex, themes.length - 1); |
443 | 443 | const theme = themes[safeIndex]; |
444 | 444 |
|
445 | | - useEffect(() => { |
446 | | - if (variantIndex !== safeIndex) setVariantIndex(safeIndex); |
447 | | - }, [safeIndex, variantIndex]); |
| 445 | + if (variantIndex !== safeIndex) setVariantIndex(safeIndex); |
448 | 446 |
|
449 | 447 | if (!theme) return null; |
450 | 448 | const collectionLabel = theme.collection?.label ?? theme.label; |
@@ -537,45 +535,42 @@ export function ThemeLibrary({ |
537 | 535 | const removeDialogCollectionLabel = |
538 | 536 | removeDialogTheme?.collection?.label ?? removeDialogTheme?.label; |
539 | 537 |
|
540 | | - const notifyThemeSaveFailure = useCallback(() => { |
| 538 | + const notifyThemeSaveFailure = () => { |
541 | 539 | toastManager.add( |
542 | 540 | stackedThreadToast({ |
543 | 541 | type: "error", |
544 | 542 | title: "Couldn’t save theme selection", |
545 | 543 | description: "Try again.", |
546 | 544 | }), |
547 | 545 | ); |
548 | | - }, []); |
| 546 | + }; |
549 | 547 |
|
550 | | - const notifyThemeRemovalFailure = useCallback(() => { |
| 548 | + const notifyThemeRemovalFailure = () => { |
551 | 549 | toastManager.add( |
552 | 550 | stackedThreadToast({ |
553 | 551 | type: "error", |
554 | 552 | title: "Couldn’t remove theme", |
555 | 553 | description: "Try again.", |
556 | 554 | }), |
557 | 555 | ); |
558 | | - }, []); |
| 556 | + }; |
559 | 557 |
|
560 | | - const persistTheme = useCallback( |
561 | | - (nextTheme: string) => { |
562 | | - const didSave = setTheme(nextTheme); |
563 | | - if (!didSave) notifyThemeSaveFailure(); |
564 | | - return didSave; |
565 | | - }, |
566 | | - [notifyThemeSaveFailure, setTheme], |
567 | | - ); |
| 558 | + const persistTheme = (nextTheme: string) => { |
| 559 | + const didSave = setTheme(nextTheme); |
| 560 | + if (!didSave) notifyThemeSaveFailure(); |
| 561 | + return didSave; |
| 562 | + }; |
568 | 563 |
|
569 | | - const handleRemoveTheme = useCallback( |
570 | | - (customTheme: ThemeDefinition, collectionThemes: ReadonlyArray<ThemeDefinition>) => { |
571 | | - setThemeRemovalTarget({ theme: customTheme, collectionThemes }); |
572 | | - setThemeIdsToRemove(collectionThemes.length > 1 ? [] : [customTheme.id]); |
573 | | - setIsThemeRemovalOpen(true); |
574 | | - }, |
575 | | - [], |
576 | | - ); |
| 564 | + const handleRemoveTheme = ( |
| 565 | + customTheme: ThemeDefinition, |
| 566 | + collectionThemes: ReadonlyArray<ThemeDefinition>, |
| 567 | + ) => { |
| 568 | + setThemeRemovalTarget({ theme: customTheme, collectionThemes }); |
| 569 | + setThemeIdsToRemove(collectionThemes.length > 1 ? [] : [customTheme.id]); |
| 570 | + setIsThemeRemovalOpen(true); |
| 571 | + }; |
577 | 572 |
|
578 | | - const handleConfirmRemoveTheme = useCallback(() => { |
| 573 | + const handleConfirmRemoveTheme = () => { |
579 | 574 | if (!themeRemovalTarget) return; |
580 | 575 | const removedIds = new Set(themeIdsToRemove); |
581 | 576 | if (removedIds.size === 0) return; |
@@ -603,52 +598,33 @@ export function ThemeLibrary({ |
603 | 598 | return; |
604 | 599 | } |
605 | 600 | setIsThemeRemovalOpen(false); |
606 | | - }, [ |
607 | | - appearanceMode, |
608 | | - notifyThemeRemovalFailure, |
609 | | - persistTheme, |
610 | | - setThemeHalf, |
611 | | - theme, |
612 | | - themeIdsToRemove, |
613 | | - themeRemovalTarget, |
614 | | - ]); |
| 601 | + }; |
615 | 602 |
|
616 | 603 | const baseCardId = getThemeDefinition(theme)?.id ?? null; |
617 | 604 | const lightOwner = themeHalves?.light ?? baseCardId; |
618 | 605 | const darkOwner = themeHalves?.dark ?? baseCardId; |
619 | 606 |
|
620 | | - const assignHalf = useCallback( |
621 | | - (appearance: ThemeAppearance, cardId: string | null) => { |
622 | | - const otherAppearance = appearance === "light" ? "dark" : "light"; |
623 | | - // Picking the default over a themed base cannot be stored as a half: |
624 | | - // the base would still own that appearance. Convert the base into an |
625 | | - // explicit half on the other side so this side falls back to default. |
626 | | - if (cardId === null && baseCardId !== null) { |
627 | | - // Read raw, before persistTheme clears the mix: the other half may |
628 | | - // name a published theme that has not streamed in yet, and falling |
629 | | - // back to the base would silently rewrite it. |
630 | | - const otherOwner = readThemeHalvesRaw()[otherAppearance] ?? baseCardId; |
631 | | - if (!persistTheme(appearanceMode === "system" ? "system" : appearanceMode)) return; |
632 | | - if (!setThemeHalf(otherAppearance, otherOwner)) { |
633 | | - setTheme(theme); |
634 | | - notifyThemeSaveFailure(); |
635 | | - } |
636 | | - return; |
637 | | - } |
638 | | - if (!setThemeHalf(appearance, cardId)) { |
| 607 | + const assignHalf = (appearance: ThemeAppearance, cardId: string | null) => { |
| 608 | + const otherAppearance = appearance === "light" ? "dark" : "light"; |
| 609 | + // Picking the default over a themed base cannot be stored as a half: |
| 610 | + // the base would still own that appearance. Convert the base into an |
| 611 | + // explicit half on the other side so this side falls back to default. |
| 612 | + if (cardId === null && baseCardId !== null) { |
| 613 | + // Read raw, before persistTheme clears the mix: the other half may |
| 614 | + // name a published theme that has not streamed in yet, and falling |
| 615 | + // back to the base would silently rewrite it. |
| 616 | + const otherOwner = readThemeHalvesRaw()[otherAppearance] ?? baseCardId; |
| 617 | + if (!persistTheme(appearanceMode === "system" ? "system" : appearanceMode)) return; |
| 618 | + if (!setThemeHalf(otherAppearance, otherOwner)) { |
| 619 | + setTheme(theme); |
639 | 620 | notifyThemeSaveFailure(); |
640 | 621 | } |
641 | | - }, |
642 | | - [ |
643 | | - appearanceMode, |
644 | | - baseCardId, |
645 | | - notifyThemeSaveFailure, |
646 | | - persistTheme, |
647 | | - setTheme, |
648 | | - setThemeHalf, |
649 | | - theme, |
650 | | - ], |
651 | | - ); |
| 622 | + return; |
| 623 | + } |
| 624 | + if (!setThemeHalf(appearance, cardId)) { |
| 625 | + notifyThemeSaveFailure(); |
| 626 | + } |
| 627 | + }; |
652 | 628 |
|
653 | 629 | const activeThemeForAppearance = |
654 | 630 | getThemeDefinition((initialAppearance === "light" ? lightOwner : darkOwner) ?? "") ?? null; |
|
0 commit comments