From df8b8d0670acb471384252ec147888d148906e4e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 29 May 2026 14:00:05 +0800 Subject: [PATCH] fix(actions): stabilize rule editor state --- .../src/modules/action/rule-card.test.tsx | 84 +++++++++++++++++++ .../renderer/src/modules/action/rule-card.tsx | 22 ++++- .../src/modules/action/segment.test.tsx | 68 +++++++++++++++ .../src/modules/action/then-section.tsx | 11 ++- .../src/modules/action/when-section.tsx | 13 ++- .../components/src/ui/segment/index.tsx | 14 ++-- .../store/src/modules/action/store.test.ts | 33 ++++++++ .../store/src/modules/action/store.ts | 3 + 8 files changed, 234 insertions(+), 14 deletions(-) create mode 100644 apps/desktop/layer/renderer/src/modules/action/rule-card.test.tsx create mode 100644 apps/desktop/layer/renderer/src/modules/action/segment.test.tsx create mode 100644 packages/internal/store/src/modules/action/store.test.ts diff --git a/apps/desktop/layer/renderer/src/modules/action/rule-card.test.tsx b/apps/desktop/layer/renderer/src/modules/action/rule-card.test.tsx new file mode 100644 index 000000000..cd9642bc7 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/action/rule-card.test.tsx @@ -0,0 +1,84 @@ +import { useActionStore } from "@follow/store/action/store" +import { QueryClient, QueryClientProvider } from "@tanstack/react-query" +import * as React from "react" +import { act } from "react" +import type { Root } from "react-dom/client" +import { createRoot } from "react-dom/client" +import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest" + +import { RuleCard } from "./rule-card" + +vi.mock("react-i18next", async (importOriginal) => { + const actual = await importOriginal() + + return { + ...actual, + ["useTranslation"]: () => ({ + t: (key: string, options?: Record) => { + if (typeof options?.count === "number") { + return `${key}:${options.count}` + } + return key + }, + }), + } +}) + +vi.mock("~/components/ui/modal/stacked/hooks", () => ({ + ["useDialog"]: () => ({ + ask: vi.fn(), + }), +})) + +vi.mock("../settings/modal/useSettingModal", () => ({ + ["useSettingModal"]: () => vi.fn(), +})) + +describe("RuleCard", () => { + let root: Root | null = null + let container: HTMLElement | null = null + + beforeAll(() => { + ;(globalThis as typeof globalThis & { React: typeof React }).React = React + ;( + globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean } + ).IS_REACT_ACT_ENVIRONMENT = true + window.removeEventListener ??= () => {} + }) + + beforeEach(() => { + useActionStore.setState({ + rules: [], + isDirty: false, + }) + }) + + afterEach(async () => { + if (root) { + await act(async () => { + root?.unmount() + }) + } + + container?.remove() + root = null + container = null + }) + + test("does not crash while a deleted rule card is still mounted", async () => { + container = document.createElement("div") + document.body.append(container) + root = createRoot(container) + const queryClient = new QueryClient() + + await expect( + act(async () => { + root!.render( + + + , + ) + }), + ).resolves.toBeUndefined() + }) +}) diff --git a/apps/desktop/layer/renderer/src/modules/action/rule-card.tsx b/apps/desktop/layer/renderer/src/modules/action/rule-card.tsx index 760bf95b7..113515a52 100644 --- a/apps/desktop/layer/renderer/src/modules/action/rule-card.tsx +++ b/apps/desktop/layer/renderer/src/modules/action/rule-card.tsx @@ -28,6 +28,12 @@ export const RuleCard = ({ defaultOpen = false, onOpenChange, }: RuleCardProps) => { + const ruleExists = useActionRules((rules) => Boolean(rules[index])) + + if (!ruleExists) { + return null + } + if (mode === "compact") { return } @@ -73,6 +79,10 @@ const CompactRuleCard = ({ setOpen(defaultOpen) }, [defaultOpen]) + if (!rule) { + return null + } + const toggle = () => { setOpen((prev) => { const next = !prev @@ -123,12 +133,20 @@ const CompactRuleCard = ({ const RuleCardToolbar = ({ index }: { index: number }) => { const { t } = useTranslation("settings") - const name = useActionRule(index, (a) => a.name) - const disabled = useActionRule(index, (a) => a.result.disabled) + const rule = useActionRule(index) const ruleCount = useActionRules((s) => s.length) const mutation = useUpdateActionsMutation() const { ask } = useDialog() + if (!rule) { + return null + } + + const { + name, + result: { disabled }, + } = rule + const handleDelete = () => { if (ruleCount === 1) { ask({ diff --git a/apps/desktop/layer/renderer/src/modules/action/segment.test.tsx b/apps/desktop/layer/renderer/src/modules/action/segment.test.tsx new file mode 100644 index 000000000..4f2327e8b --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/action/segment.test.tsx @@ -0,0 +1,68 @@ +import { SegmentGroup, SegmentItem } from "@follow/components/ui/segment/index.js" +import * as React from "react" +import { act } from "react" +import type { Root } from "react-dom/client" +import { createRoot } from "react-dom/client" +import { afterEach, beforeAll, describe, expect, test } from "vitest" + +const ControlledSegment = ({ value }: { value: string }) => ( + + + + +) + +const getTab = (container: HTMLElement, name: string) => { + const tab = Array.from(container.querySelectorAll("[role=tab]")).find( + (element) => element.textContent?.trim() === name, + ) + + if (!tab) { + throw new Error(`Tab not found: ${name}`) + } + + return tab +} + +describe("SegmentGroup", () => { + let root: Root | null = null + let container: HTMLElement | null = null + + beforeAll(() => { + ;(globalThis as typeof globalThis & { React: typeof React }).React = React + ;( + globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean } + ).IS_REACT_ACT_ENVIRONMENT = true + }) + + afterEach(async () => { + if (root) { + await act(async () => { + root?.unmount() + }) + } + + container?.remove() + root = null + container = null + }) + + test("updates the active item when the controlled value changes", async () => { + container = document.createElement("div") + document.body.append(container) + root = createRoot(container) + + await act(async () => { + root!.render() + }) + + expect(getTab(container, "Custom Filters").dataset.state).toBe("active") + + await act(async () => { + root!.render() + }) + + expect(getTab(container, "All").dataset.state).toBe("active") + expect(getTab(container, "Custom Filters").dataset.state).toBe("inactive") + }) +}) diff --git a/apps/desktop/layer/renderer/src/modules/action/then-section.tsx b/apps/desktop/layer/renderer/src/modules/action/then-section.tsx index af27d82ec..5cc22a66d 100644 --- a/apps/desktop/layer/renderer/src/modules/action/then-section.tsx +++ b/apps/desktop/layer/renderer/src/modules/action/then-section.tsx @@ -206,9 +206,14 @@ export const ThenSection = ({ index, variant: _variant = "detail" }: ThenSection return (
- - {t("actions.action_card.then_do")} - +
+ + 2 + + + {t("actions.action_card.then_do")} + +
{enabledActions.length > 0 && ( {t("actions.action_card.summary.action_count", { count: enabledActions.length })} diff --git a/apps/desktop/layer/renderer/src/modules/action/when-section.tsx b/apps/desktop/layer/renderer/src/modules/action/when-section.tsx index c74897e38..1b5cc3ef3 100644 --- a/apps/desktop/layer/renderer/src/modules/action/when-section.tsx +++ b/apps/desktop/layer/renderer/src/modules/action/when-section.tsx @@ -26,7 +26,7 @@ export const WhenSection = ({ index }: WhenSectionProps) => { const { t } = useTranslation("settings") const disabled = useActionRule(index, (a) => a.result.disabled) - const condition = useActionRule(index, (a) => a.condition) + const condition = useActionRule(index, (a) => a.condition) ?? [] const mode = condition.length > 0 ? "filter" : "all" @@ -43,9 +43,14 @@ export const WhenSection = ({ index }: WhenSectionProps) => { return (
- - {t("actions.action_card.when_feeds_match")} - +
+ + 1 + + + {t("actions.action_card.when_feeds_match")} + +
handleModeChange(value as "all" | "filter")} diff --git a/packages/internal/components/src/ui/segment/index.tsx b/packages/internal/components/src/ui/segment/index.tsx index 0372451fc..49d3abaca 100644 --- a/packages/internal/components/src/ui/segment/index.tsx +++ b/packages/internal/components/src/ui/segment/index.tsx @@ -14,7 +14,9 @@ interface SegmentGroupProps { export const SegmentGroup = (props: ComponentType) => { const { onValueChanged, value, className } = props - const [currentValue, setCurrentValue] = useState(value || "") + const isControlled = value !== undefined + const [uncontrolledValue, setUncontrolledValue] = useState(value || "") + const currentValue = isControlled ? value : uncontrolledValue const componentId = useId() return ( @@ -23,13 +25,15 @@ export const SegmentGroup = (props: ComponentType) => { value={useMemo( () => ({ value: currentValue, - setValue: (value) => { - setCurrentValue(value) - onValueChanged?.(value) + setValue: (nextValue) => { + if (!isControlled) { + setUncontrolledValue(nextValue) + } + onValueChanged?.(nextValue) }, componentId, }), - [componentId, currentValue, onValueChanged], + [componentId, currentValue, isControlled, onValueChanged], )} >
{ + beforeEach(() => { + useActionStore.setState({ + rules: [], + isDirty: false, + }) + }) + + test("creates new rules that match all feeds by default", () => { + actionActions.addRule((index) => `Action ${index}`) + + expect(useActionStore.getState().rules[0]).toMatchObject({ + name: "Action 1", + condition: [], + index: 0, + result: {}, + }) + }) + + test("keeps rule indexes stable after deleting and adding rules", () => { + actionActions.addRule((index) => `Action ${index}`) + actionActions.addRule((index) => `Action ${index}`) + + actionActions.deleteRule(0) + actionActions.addRule((index) => `Action ${index}`) + + expect(useActionStore.getState().rules.map((rule) => rule.index)).toEqual([0, 1]) + }) +}) diff --git a/packages/internal/store/src/modules/action/store.ts b/packages/internal/store/src/modules/action/store.ts index 60be51bf9..c919a8f13 100644 --- a/packages/internal/store/src/modules/action/store.ts +++ b/packages/internal/store/src/modules/action/store.ts @@ -160,6 +160,9 @@ class ActionActions { deleteRule(index: number) { immerSet((state) => { state.rules.splice(index, 1) + state.rules.forEach((rule, ruleIndex) => { + rule.index = ruleIndex + }) state.isDirty = true }) }