refactor(trash): dirty 派生值改用 use_memo

依据 dioxus-render-purity skill 规则二:派生值不应在 render 期内联重算。
dirty 是 settings_draft_* 与 settings 的纯派生布尔值,改用 use_memo,
依赖信号不变时跳过 trim/parse 重算。同步更新两处使用点为 dirty() 调用。
This commit is contained in:
xfy 2026-06-26 09:33:13 +08:00
parent 9e9a3faeed
commit afebd441ae

View File

@ -109,13 +109,16 @@ pub fn TrashPage(page: i32) -> Element {
}; };
// 草稿相对已保存配置是否存在差异:控制保存按钮可用性与“未保存”提示。 // 草稿相对已保存配置是否存在差异:控制保存按钮可用性与“未保存”提示。
let dirty = settings_draft_enabled() != settings().auto_purge_enabled // 派生值用 use_memo依赖信号不变时不重算避免每次渲染重复 parse 字符串)。
|| settings_draft_days() let dirty = use_memo(move || {
.trim() settings_draft_enabled() != settings().auto_purge_enabled
.parse::<i32>() || settings_draft_days()
.ok() .trim()
.map(|d| d != settings().retention_days) .parse::<i32>()
.unwrap_or(true); .ok()
.map(|d| d != settings().retention_days)
.unwrap_or(true)
});
// 折叠箭头旋转类(展开时翻转 180° // 折叠箭头旋转类(展开时翻转 180°
let chevron_rotate = if settings_panel_open() { let chevron_rotate = if settings_panel_open() {
"rotate-180" "rotate-180"
@ -281,7 +284,7 @@ pub fn TrashPage(page: i32) -> Element {
} }
"已保存" "已保存"
} }
} else if dirty { } else if dirty() {
span { class: "text-xs text-paper-secondary", "有未保存的更改" } span { class: "text-xs text-paper-secondary", "有未保存的更改" }
} else { } else {
span { class: "text-xs text-transparent select-none", span { class: "text-xs text-transparent select-none",
@ -291,7 +294,7 @@ pub fn TrashPage(page: i32) -> Element {
// 保存按钮:启用主题色,禁用/保存中态灰化 // 保存按钮:启用主题色,禁用/保存中态灰化
button { button {
class: if saving_settings() { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium cursor-not-allowed text-paper-secondary bg-paper-tertiary rounded-full" } else if just_saved() { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium cursor-not-allowed text-paper-secondary bg-paper-tertiary rounded-full" } else { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium text-paper-theme bg-paper-accent rounded-full hover:brightness-110 active:scale-[0.98] transition-all cursor-pointer focus:outline-none focus-visible:ring-2 focus-visible:ring-paper-accent/40" }, class: if saving_settings() { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium cursor-not-allowed text-paper-secondary bg-paper-tertiary rounded-full" } else if just_saved() { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium cursor-not-allowed text-paper-secondary bg-paper-tertiary rounded-full" } else { "inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-medium text-paper-theme bg-paper-accent rounded-full hover:brightness-110 active:scale-[0.98] transition-all cursor-pointer focus:outline-none focus-visible:ring-2 focus-visible:ring-paper-accent/40" },
disabled: saving_settings() || just_saved() || !dirty, disabled: saving_settings() || just_saved() || !dirty(),
onclick: move |_| { onclick: move |_| {
let days: i32 = settings_draft_days().parse().unwrap_or(30); let days: i32 = settings_draft_days().parse().unwrap_or(30);
let enabled = settings_draft_enabled(); let enabled = settings_draft_enabled();