Skip to content

Commit e63ce5a

Browse files
cyfung1031CodFrmCopilot
authored
🐛 修正 ScriptCard 动画问题 (#1234)
* v1.3 修正 ScriptCard 动画问题 * Update src/pages/options/routes/ScriptList/ScriptCard.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: wangyizhi <i@xloli.top> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 4044a22 commit e63ce5a

2 files changed

Lines changed: 28 additions & 29 deletions

File tree

src/pages/options/routes/ScriptList/ScriptCard.tsx

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import type { DragEndEvent } from "@dnd-kit/core";
2727
import { closestCenter, DndContext, KeyboardSensor, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
2828
import { rectSwappingStrategy, SortableContext, sortableKeyboardCoordinates, useSortable } from "@dnd-kit/sortable";
2929
import { CSS } from "@dnd-kit/utilities";
30-
import { useAppContext } from "@App/pages/store/AppContext";
3130
import { type SearchFilterRequest } from "./SearchFilter";
3231
import type { SearchType } from "@App/app/service/service_worker/types";
3332

@@ -410,28 +409,7 @@ const ScriptCard = ({
410409
handleRunStop,
411410
}: ScriptCardProps) => {
412411
const { t } = useTranslation();
413-
const { guideMode } = useAppContext();
414-
415-
// 如果是引导模式,且没有脚本,则创建一条演示数据
416-
const list = useMemo(
417-
() =>
418-
guideMode && scriptList.length === 0
419-
? [
420-
{
421-
uuid: "demo-uuid-1234",
422-
name: "Demo Script",
423-
namespace: "demo",
424-
sort: 0,
425-
createtime: Date.now(),
426-
checktime: Date.now(),
427-
metadata: {},
428-
type: SCRIPT_TYPE_NORMAL,
429-
favorite: [{ match: "Example", icon: "", website: "https://example.com" }],
430-
} as ScriptLoading,
431-
]
432-
: scriptList,
433-
[guideMode, scriptList]
434-
);
412+
435413
// Sensors — move them here (or even higher in parent) so they're stable
436414
const sensors = useSensors(
437415
useSensor(PointerSensor, {
@@ -442,8 +420,8 @@ const ScriptCard = ({
442420
})
443421
);
444422

445-
// 故意生成一个字串 避免因 list 的参考频繁改动而导致 ctx sortableIds 参考出现非预期更改。
446-
const sortableIdsString = list?.map((s) => s.uuid).join(",") || "";
423+
// 基于 scriptList 的 uuid 生成稳定字串,避免因 scriptList 引用频繁变动导致 ctx 内部 sortableIds 参考出现非预期更改。
424+
const sortableIdsString = scriptList?.map((s) => s.uuid).join(",") || "";
447425

448426
// sortableIds 应该只包含 ID 字符串数组,而不是对象数组,
449427
// 且确保 items 属性接收的是纯 ID 列表,这样 dnd-kit 内部对比更高效。
@@ -529,7 +507,7 @@ const ScriptCard = ({
529507
padding: "16px",
530508
}}
531509
>
532-
{list.length === 0 ? (
510+
{scriptList.length === 0 ? (
533511
loadingList ? (
534512
<div
535513
style={{
@@ -566,7 +544,7 @@ const ScriptCard = ({
566544
gap: "16px",
567545
}}
568546
>
569-
{list.map((item) => (
547+
{scriptList.map((item) => (
570548
<ScriptCardItem
571549
key={item.uuid}
572550
item={item}

src/pages/options/routes/ScriptList/index.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { SearchFilter, type SearchFilterRequest } from "./SearchFilter";
3636
import type { ScriptLoading } from "@App/pages/store/features/script";
3737

3838
import { type TSelectFilter, useScriptDataManagement, useScriptFilters } from "./hooks";
39+
import { useAppContext } from "@App/pages/store/AppContext";
3940

4041
type TableProps = React.ComponentProps<typeof ScriptTable>;
4142
type CardProps = React.ComponentProps<typeof ScriptCard>;
@@ -51,12 +52,28 @@ const MainContent = memo(({ viewMode, ...props }: { viewMode: "table" | "card" }
5152

5253
MainContent.displayName = "MainContent";
5354

55+
// 一条演示数据
56+
const guideModeScriptList = [
57+
{
58+
uuid: "demo-uuid-1234",
59+
name: "Demo Script",
60+
namespace: "demo",
61+
sort: 0,
62+
createtime: Date.now(),
63+
checktime: Date.now(),
64+
metadata: {},
65+
type: SCRIPT_TYPE_NORMAL,
66+
favorite: [{ match: "Example", icon: "", website: "https://example.com" }],
67+
} as ScriptLoading,
68+
];
69+
5470
/**
5571
* 主组件
5672
*/
5773
function ScriptList() {
5874
const { t } = useTranslation();
5975
const [usp] = useSearchParams();
76+
const { guideMode } = useAppContext();
6077

6178
// 1. 基础 UI 状态
6279
const [sidebarOpen, setSidebarOpen] = useState(() => localStorage.getItem("script-list-sidebar") === "1");
@@ -190,7 +207,11 @@ function ScriptList() {
190207
// 6. 执行过滤逻辑
191208
useEffect(() => {
192209
const { status, type, tags, source } = selectedFilters;
193-
const list = scriptList.filter((s) => {
210+
211+
// 如果是引导模式,且没有脚本,则使用演示数据
212+
const targetList = guideMode && scriptList.length === 0 ? guideModeScriptList : scriptList;
213+
214+
const list = targetList.filter((s) => {
194215
if (status !== null) {
195216
if (status === SCRIPT_STATUS_ENABLE || status === SCRIPT_STATUS_DISABLE) {
196217
if (s.status !== status) return false;
@@ -221,7 +242,7 @@ function ScriptList() {
221242
return () => {
222243
enableKeywordSearch = false;
223244
};
224-
}, [scriptList, selectedFilters, stats, searchRequest]);
245+
}, [guideMode, scriptList, selectedFilters, stats, searchRequest]);
225246

226247
// 处理 URL 传参打开配置
227248
useEffect(() => {

0 commit comments

Comments
 (0)