Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dataSourceList 排序时直接点完成数据源会清空 #90

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions packages/plugin-datasource-pane/src/utils/stateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type DataSourcePaneStateEvent =
| { type: 'START_VIEW' }
| { type: 'EXPORT.toggleSelect' };

export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => createMachine<DataSourcePaneStateContext, DataSourcePaneStateEvent>(
export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) =>
createMachine<DataSourcePaneStateContext, DataSourcePaneStateEvent>(
{
id: 'dataSourcePane',
initial: 'idle',
Expand Down Expand Up @@ -82,7 +83,9 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
target: 'idle',
actions: assign({
dataSourceList: (context, event) => {
return context.sort.dataSourceList;
return context.sort.dataSourceList.length !== 0
? context.sort.dataSourceList
: context.dataSourceList;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的问题是不是 当没有发生排序时, context.sort.dataSourceList为空,所以导致了这个问题,那这里应该只是做一个 context.sort.dataSourceList 为空时的兜底逻辑。
你这段代码逻辑是:sort ds === ds 则使用 sort ds, 否则使用 ds。这样不就一直使用 ds了吗

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本意是给context.sort.dataSourceList 为空时兜底,当 SAVE_SORT 执行之后也会把 当前ds传给sort.ds,感觉效果是一样的,我改一下

},
}),
},
Expand All @@ -106,7 +109,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
export: (context, event) => {
const { selectedDataSourceIdList } = context.export;
const index = selectedDataSourceIdList.indexOf(
event.dataSourceId,
event.dataSourceId
);
if (index !== -1) {
return {
Expand All @@ -118,7 +121,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
return {
selectedDataSourceIdList:
context.export.selectedDataSourceIdList.concat(
event.dataSourceId,
event.dataSourceId
),
};
},
Expand Down Expand Up @@ -162,11 +165,13 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
actions: assign({
dataSourceList: (context, event) => {
// 直接 concat 会出现重复
const filterDataSourceList = context.dataSourceList.filter((item) => {
return !event.payload.find(
(dataSource: DataSourceConfig) => dataSource.id === item.id,
)
})
const filterDataSourceList =
context.dataSourceList.filter((item) => {
return !event.payload.find(
(dataSource: DataSourceConfig) =>
dataSource.id === item.id
);
});

return filterDataSourceList.concat(event.payload);
},
Expand Down Expand Up @@ -256,7 +261,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
const dataSourceUpdateIndex = dataSourceList.findIndex(
(dataSource) => {
return dataSource.id === id;
},
}
);
dataSourceList[dataSourceUpdateIndex] = event.payload;
return dataSourceList;
Expand Down Expand Up @@ -284,7 +289,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
actions: assign({
dataSourceList: (context, event) => {
return context.dataSourceList.filter(
(item) => item.id !== event.dataSourceId,
(item) => item.id !== event.dataSourceId
);
},
}),
Expand All @@ -308,10 +313,10 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
actions: assign({
detail: (context, event) => {
const dataSource = context.dataSourceList.find(
(item) => item.id === event.dataSourceId,
(item) => item.id === event.dataSourceId
);
const dataSourceType = event.dataSourceTypes.find(
(i) => i.type === dataSource.type,
(i) => i.type === dataSource.type
);
return {
visible: true,
Expand All @@ -329,7 +334,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
actions: assign({
detail: (context, event) => {
const dataSource = context.dataSourceList.find(
(item) => item.id === event.dataSourceId,
(item) => item.id === event.dataSourceId
);
return {
visible: true,
Expand All @@ -339,7 +344,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
data: {
dataSource,
dataSourceType: event.dataSourceTypes.find(
(i) => i.type === dataSource.type,
(i) => i.type === dataSource.type
),
},
};
Expand All @@ -351,7 +356,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
actions: assign({
detail: (context, event) => {
const dataSource = context.dataSourceList.find(
(item) => item.id === event.dataSourceId,
(item) => item.id === event.dataSourceId
);
return {
visible: true,
Expand All @@ -362,7 +367,7 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
id: _uniqueId(`${event.dataSourceId}_`),
},
dataSourceType: event.dataSourceTypes.find(
(i) => i.type === dataSource.type,
(i) => i.type === dataSource.type
),
},
};
Expand Down Expand Up @@ -397,9 +402,10 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
},
{
actions: {},
},
}
);

export const createStateService = () => interpret(createStateMachine()).onTransition((current) => {
export const createStateService = () =>
interpret(createStateMachine()).onTransition((current) => {
// console.log('current transition list', current.value);
});