@nocobase/client-v2 2.3.0-beta.22 → 2.3.0-beta.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/components/form/VariableInput.d.ts +1 -0
- package/es/flow/models/base/PageModel/closeGuard.d.ts +8 -1
- package/es/flow/models/blocks/form/EditFormModel.d.ts +2 -0
- package/es/flow/models/blocks/table/sortUtils.d.ts +3 -0
- package/es/flow/models/topbar/UserCenterTopbarActionModel.d.ts +2 -0
- package/es/index.mjs +86 -86
- package/lib/index.js +89 -89
- package/package.json +7 -7
- package/src/components/form/VariableInput.tsx +1 -0
- package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.mobile.test.tsx +55 -1
- package/src/flow/models/base/PageModel/closeGuard.ts +9 -8
- package/src/flow/models/blocks/form/EditFormModel.tsx +23 -1
- package/src/flow/models/blocks/form/__tests__/EditFormModel.pagination.test.tsx +77 -0
- package/src/flow/models/blocks/table/TableBlockModel.tsx +4 -10
- package/src/flow/models/blocks/table/__tests__/sortUtils.test.ts +11 -1
- package/src/flow/models/blocks/table/sortUtils.ts +10 -0
- package/src/flow/models/topbar/UserCenterTopbarActionModel.tsx +58 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.3.0-beta.
|
|
3
|
+
"version": "2.3.0-beta.24",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"@formily/antd-v5": "1.2.3",
|
|
28
28
|
"@formily/react": "^2.2.27",
|
|
29
29
|
"@formily/shared": "^2.2.27",
|
|
30
|
-
"@nocobase/evaluators": "2.3.0-beta.
|
|
31
|
-
"@nocobase/flow-engine": "2.3.0-beta.
|
|
32
|
-
"@nocobase/sdk": "2.3.0-beta.
|
|
33
|
-
"@nocobase/shared": "2.3.0-beta.
|
|
34
|
-
"@nocobase/utils": "2.3.0-beta.
|
|
30
|
+
"@nocobase/evaluators": "2.3.0-beta.24",
|
|
31
|
+
"@nocobase/flow-engine": "2.3.0-beta.24",
|
|
32
|
+
"@nocobase/sdk": "2.3.0-beta.24",
|
|
33
|
+
"@nocobase/shared": "2.3.0-beta.24",
|
|
34
|
+
"@nocobase/utils": "2.3.0-beta.24",
|
|
35
35
|
"ahooks": "^3.7.2",
|
|
36
36
|
"antd": "5.24.2",
|
|
37
37
|
"antd-style": "3.7.1",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"react-router-dom": "^6.30.1",
|
|
50
50
|
"zxing-wasm": "^3.1.3"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "9780899cbaa5fc4657ac9ea16da2ac99978d748d"
|
|
53
53
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
10
|
+
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
11
11
|
import { Drawer, Input } from 'antd';
|
|
12
12
|
import React, { useState } from 'react';
|
|
13
13
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
@@ -157,6 +157,60 @@ describe('mobile user center popover', () => {
|
|
|
157
157
|
expect(screen.queryByRole('img', { name: 'ellipsis' })).not.toBeInTheDocument();
|
|
158
158
|
});
|
|
159
159
|
|
|
160
|
+
it('shows profile while checking availability and ignores a result from an earlier opening', async () => {
|
|
161
|
+
let resolveFirst!: (visible: boolean) => void;
|
|
162
|
+
const firstCheck = new Promise<boolean>((resolve) => {
|
|
163
|
+
resolveFirst = resolve;
|
|
164
|
+
});
|
|
165
|
+
const check = vi.fn().mockReturnValueOnce(firstCheck).mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
|
166
|
+
const prepareProfile = vi.fn(async () => {});
|
|
167
|
+
class Profile extends UserCenterActionItemModel {
|
|
168
|
+
static itemId = 'profile';
|
|
169
|
+
prepare = prepareProfile;
|
|
170
|
+
label = 'Edit profile';
|
|
171
|
+
}
|
|
172
|
+
class Verification extends UserCenterActionItemModel {
|
|
173
|
+
static itemId = 'verification';
|
|
174
|
+
label = 'Verification';
|
|
175
|
+
checkAvailability = check;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const model = new UserCenterTopbarActionModel({ uid: 'menu', use: 'UserCenterTopbarActionModel' });
|
|
179
|
+
Object.defineProperty(model, 'flowEngine', {
|
|
180
|
+
value: {
|
|
181
|
+
getSubclassesOfAsync: () =>
|
|
182
|
+
new Map<string, typeof UserCenterActionItemModel>([
|
|
183
|
+
['profile', Profile],
|
|
184
|
+
['verification', Verification],
|
|
185
|
+
]),
|
|
186
|
+
createModel: ({ use, uid }: { use: typeof UserCenterActionItemModel; uid: string }) =>
|
|
187
|
+
new use({ uid, use: use.name }),
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
render(<TopbarActionsBar actions={[model]} />);
|
|
191
|
+
|
|
192
|
+
const closeMenu = async () => {
|
|
193
|
+
fireEvent.click(screen.getByRole('button', { name: 'Edit profile' }));
|
|
194
|
+
await waitFor(() => expect(screen.queryByRole('button', { name: 'Edit profile' })).not.toBeInTheDocument());
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
await openUserMenu(false);
|
|
198
|
+
expect(check).toHaveBeenCalledTimes(1);
|
|
199
|
+
expect(screen.queryByRole('button', { name: 'Verification' })).not.toBeInTheDocument();
|
|
200
|
+
await closeMenu();
|
|
201
|
+
|
|
202
|
+
await openUserMenu(false);
|
|
203
|
+
expect(check).toHaveBeenCalledTimes(2);
|
|
204
|
+
await act(async () => resolveFirst(true));
|
|
205
|
+
expect(screen.queryByRole('button', { name: 'Verification' })).not.toBeInTheDocument();
|
|
206
|
+
await closeMenu();
|
|
207
|
+
|
|
208
|
+
await openUserMenu(false);
|
|
209
|
+
expect(await screen.findByRole('button', { name: 'Verification' })).toBeVisible();
|
|
210
|
+
expect(check).toHaveBeenCalledTimes(3);
|
|
211
|
+
expect(prepareProfile).toHaveBeenCalledOnce();
|
|
212
|
+
});
|
|
213
|
+
|
|
160
214
|
it.each([true, false])('respects closeOnChange=%s for preference selectors', async (closeOnChange) => {
|
|
161
215
|
const onChange = vi.fn(async () => {});
|
|
162
216
|
const item = new UserCenterSelectItemModel({ uid: 'language', use: 'UserCenterSelectItemModel' });
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import type { FlowModelContext } from '@nocobase/flow-engine';
|
|
11
|
+
|
|
10
12
|
type CloseGuardContext = {
|
|
11
13
|
inputArgs?: {
|
|
12
14
|
dirty?: {
|
|
@@ -23,25 +25,24 @@ type CloseGuardContext = {
|
|
|
23
25
|
content: string;
|
|
24
26
|
okText: string;
|
|
25
27
|
cancelText: string;
|
|
26
|
-
}) => boolean | Promise<boolean>;
|
|
28
|
+
}) => boolean | Promise<boolean> | ReturnType<FlowModelContext['modal']['confirm']>;
|
|
27
29
|
};
|
|
28
30
|
t: (value: string) => string;
|
|
29
31
|
exitAll: () => void;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const confirmed = await ctx.modal.confirm({
|
|
34
|
+
export function confirmDiscardChanges(ctx: Pick<CloseGuardContext, 'modal' | 't'>) {
|
|
35
|
+
return ctx.modal.confirm({
|
|
38
36
|
title: ctx.t('Unsaved changes'),
|
|
39
37
|
content: ctx.t("Are you sure you don't want to save?"),
|
|
40
38
|
okText: ctx.t('Confirm'),
|
|
41
39
|
cancelText: ctx.t('Cancel'),
|
|
42
40
|
});
|
|
41
|
+
}
|
|
43
42
|
|
|
44
|
-
|
|
43
|
+
export async function confirmUnsavedChangesHandler(ctx: CloseGuardContext) {
|
|
44
|
+
if (!ctx.inputArgs?.dirty?.hasDirtyForms) return;
|
|
45
|
+
if (!(await confirmDiscardChanges(ctx))) {
|
|
45
46
|
ctx.inputArgs?.controller?.prevent?.();
|
|
46
47
|
ctx.exitAll();
|
|
47
48
|
}
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
FlowModelRenderer,
|
|
17
17
|
MemoFlowModelRenderer,
|
|
18
18
|
MultiRecordResource,
|
|
19
|
+
observable,
|
|
19
20
|
SingleRecordResource,
|
|
20
21
|
} from '@nocobase/flow-engine';
|
|
21
22
|
import { Pagination, Space } from 'antd';
|
|
@@ -23,12 +24,14 @@ import { isEqual } from 'lodash';
|
|
|
23
24
|
import React from 'react';
|
|
24
25
|
import { BlockSceneEnum } from '../../base/BlockModel';
|
|
25
26
|
import { FormBlockContent, FormBlockModel } from './FormBlockModel';
|
|
27
|
+
import { confirmDiscardChanges } from '../../base/PageModel/closeGuard';
|
|
26
28
|
import { dispatchEventDeep } from '../../../utils';
|
|
27
29
|
|
|
28
30
|
export class EditFormModel extends FormBlockModel {
|
|
29
31
|
static scene = BlockSceneEnum.oam;
|
|
30
32
|
static blockCapabilityActionName = 'update';
|
|
31
33
|
|
|
34
|
+
private readonly confirmingPageChange = observable.ref(false);
|
|
32
35
|
private actionFlowSettings = { showBackground: false, showBorder: false, toolbarPosition: 'above' as const };
|
|
33
36
|
private actionExtraToolbarItems = [
|
|
34
37
|
{
|
|
@@ -71,9 +74,26 @@ export class EditFormModel extends FormBlockModel {
|
|
|
71
74
|
getAclActionName() {
|
|
72
75
|
return 'update';
|
|
73
76
|
}
|
|
77
|
+
|
|
78
|
+
private hasUnsavedChanges = () => {
|
|
79
|
+
const record = this.getCurrentRecord();
|
|
80
|
+
return Array.from(this.getUserModifiedFields()).some(
|
|
81
|
+
(field) => !isEqual(this.form.getFieldValue(field), record?.[field]),
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
74
85
|
handlePageChange = async (page: number) => {
|
|
75
86
|
if (this.resource instanceof MultiRecordResource) {
|
|
76
87
|
const multiResource = this.resource as MultiRecordResource;
|
|
88
|
+
if (page === multiResource.getPage() || this.confirmingPageChange.value) return;
|
|
89
|
+
if (this.hasUnsavedChanges()) {
|
|
90
|
+
this.confirmingPageChange.value = true;
|
|
91
|
+
try {
|
|
92
|
+
if (!(await confirmDiscardChanges(this.context))) return;
|
|
93
|
+
} finally {
|
|
94
|
+
this.confirmingPageChange.value = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
77
97
|
multiResource.setPage(page);
|
|
78
98
|
multiResource.loading = true;
|
|
79
99
|
await multiResource.refresh();
|
|
@@ -116,10 +136,12 @@ export class EditFormModel extends FormBlockModel {
|
|
|
116
136
|
}}
|
|
117
137
|
>
|
|
118
138
|
<Pagination
|
|
139
|
+
// Simple pagination keeps its own editable page number. Reset it while confirming the switch.
|
|
140
|
+
key={this.confirmingPageChange.value ? 'confirming' : 'editing'}
|
|
119
141
|
simple
|
|
120
142
|
pageSize={1}
|
|
121
143
|
showSizeChanger={false}
|
|
122
|
-
|
|
144
|
+
current={(this.resource as MultiRecordResource).getPage()}
|
|
123
145
|
total={(this.resource as MultiRecordResource).getTotalPage()}
|
|
124
146
|
onChange={this.handlePageChange}
|
|
125
147
|
style={{ display: 'inline-block' }}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { FlowEngine, MultiRecordResource, observer } from '@nocobase/flow-engine';
|
|
11
|
+
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
|
12
|
+
import { Form, Input, Modal } from 'antd';
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import { expect, it, vi } from 'vitest';
|
|
15
|
+
import { EditFormModel } from '../EditFormModel';
|
|
16
|
+
|
|
17
|
+
it('keeps the visible page and edits on cancel and loads the next record on confirm', async () => {
|
|
18
|
+
const records = [{ name: 'Record B' }, { name: 'Record A' }];
|
|
19
|
+
const model = new EditFormModel({ uid: 'edit', flowEngine: new FlowEngine() });
|
|
20
|
+
const resource = new MultiRecordResource(model.context);
|
|
21
|
+
resource.setPage(1).setPageSize(1).setMeta({ count: 2, totalPage: 2 }).setData([records[0]]);
|
|
22
|
+
model.context.defineProperty('resource', { value: resource });
|
|
23
|
+
model.context.defineProperty('record', { get: () => model.getCurrentRecord() });
|
|
24
|
+
const refresh = vi.spyOn(resource, 'refresh').mockImplementation(async () => {
|
|
25
|
+
const record = records[resource.getPage() - 1];
|
|
26
|
+
resource.setData([record]);
|
|
27
|
+
model.form.setFieldsValue(record);
|
|
28
|
+
model.resetUserModifiedFields();
|
|
29
|
+
resource.loading = false;
|
|
30
|
+
});
|
|
31
|
+
const Editor = observer(() => {
|
|
32
|
+
const [form] = Form.useForm();
|
|
33
|
+
const [modal, dialogs] = Modal.useModal();
|
|
34
|
+
model.context.defineProperty('form', { value: form });
|
|
35
|
+
model.context.defineProperty('modal', { value: modal });
|
|
36
|
+
const block = model.renderComponent();
|
|
37
|
+
return (
|
|
38
|
+
<>
|
|
39
|
+
{dialogs}
|
|
40
|
+
{React.cloneElement(block, {
|
|
41
|
+
grid: (
|
|
42
|
+
<Form.Item name="name" label="Name">
|
|
43
|
+
<Input />
|
|
44
|
+
</Form.Item>
|
|
45
|
+
),
|
|
46
|
+
actions: null,
|
|
47
|
+
})}
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const { container } = render(<Editor />);
|
|
53
|
+
const field = screen.getByRole('textbox', { name: 'Name' });
|
|
54
|
+
const pageInput = () => screen.getByRole('textbox', { name: 'Go to' });
|
|
55
|
+
const nextPage = () => fireEvent.click(container.querySelector('.ant-pagination-next button'));
|
|
56
|
+
expect(field).toHaveValue('Record B');
|
|
57
|
+
fireEvent.change(field, { target: { value: 'unsaved draft' } });
|
|
58
|
+
nextPage();
|
|
59
|
+
const dialog = await screen.findByRole('dialog');
|
|
60
|
+
expect(dialog).toHaveTextContent('Unsaved changes');
|
|
61
|
+
expect(pageInput()).toHaveValue('1');
|
|
62
|
+
fireEvent.click(within(dialog).getByText('Cancel'));
|
|
63
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
|
|
64
|
+
expect(pageInput()).toHaveValue('1');
|
|
65
|
+
expect(field).toHaveValue('unsaved draft');
|
|
66
|
+
expect(refresh).not.toHaveBeenCalled();
|
|
67
|
+
|
|
68
|
+
nextPage();
|
|
69
|
+
fireEvent.click(within(await screen.findByRole('dialog')).getByText('Confirm'));
|
|
70
|
+
await waitFor(() => expect(field).toHaveValue('Record A'));
|
|
71
|
+
expect(pageInput()).toHaveValue('2');
|
|
72
|
+
expect(refresh).toHaveBeenCalledTimes(1);
|
|
73
|
+
|
|
74
|
+
fireEvent.click(container.querySelector('.ant-pagination-prev button'));
|
|
75
|
+
await waitFor(() => expect(field).toHaveValue('Record B'));
|
|
76
|
+
expect(pageInput()).toHaveValue('1');
|
|
77
|
+
});
|
|
@@ -45,7 +45,7 @@ import {
|
|
|
45
45
|
getRowKey,
|
|
46
46
|
useBlockHeight,
|
|
47
47
|
} from './utils';
|
|
48
|
-
import {
|
|
48
|
+
import { getTableSorterOrder } from './sortUtils';
|
|
49
49
|
import { commonConditionHandler, ConditionBuilder } from '../../../components/ConditionBuilder';
|
|
50
50
|
import { BulkDeleteActionModel } from '../../actions/BulkDeleteActionModel';
|
|
51
51
|
import {
|
|
@@ -1038,15 +1038,9 @@ const HighPerformanceTable = React.memo(
|
|
|
1038
1038
|
const mergedColumns = leftAuxiliaryColumn ? [leftAuxiliaryColumn, ...columns] : columns;
|
|
1039
1039
|
|
|
1040
1040
|
const handleChange = useCallback(
|
|
1041
|
-
async (pagination, filters, sorter) => {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
const currentSort = resourceSort?.length ? resourceSort : globalSort;
|
|
1045
|
-
const sorterField = resolveTableSorterField(sorter);
|
|
1046
|
-
const sort =
|
|
1047
|
-
sorter.order && sorterField ? (sorter.order === `ascend` ? [sorterField] : [`-${sorterField}`]) : currentSort;
|
|
1048
|
-
if (sorter) {
|
|
1049
|
-
model.resource.setSort(sort);
|
|
1041
|
+
async (pagination, filters, sorter, extra) => {
|
|
1042
|
+
if (extra?.action === 'sort') {
|
|
1043
|
+
model.resource.setSort(getTableSorterOrder(sorter, model.getDragSortFieldName(), model.props.globalSort));
|
|
1050
1044
|
}
|
|
1051
1045
|
if (model.resource.getPageSize() !== pagination.pageSize) {
|
|
1052
1046
|
model.resource.setPage(1);
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { describe, expect, it } from 'vitest';
|
|
11
11
|
|
|
12
|
-
import { getTableColumnSortField, resolveTableSorterField } from '../sortUtils';
|
|
12
|
+
import { getTableColumnSortField, getTableSorterOrder, resolveTableSorterField } from '../sortUtils';
|
|
13
13
|
|
|
14
14
|
describe('table sort utils', () => {
|
|
15
15
|
it('builds a relation sort path from the selected association title field', () => {
|
|
@@ -56,3 +56,13 @@ describe('table sort utils', () => {
|
|
|
56
56
|
).toBe('org_oho.orgname');
|
|
57
57
|
});
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
it('restores configured sorting after both directions of manual association sorting', () => {
|
|
61
|
+
const column = { sortField: 'team.title' };
|
|
62
|
+
const defaults = ['-createdAt', 'id'];
|
|
63
|
+
expect(getTableSorterOrder({ column, order: 'ascend' }, undefined, defaults)).toEqual(['team.title']);
|
|
64
|
+
expect(getTableSorterOrder({ column, order: 'descend' }, undefined, defaults)).toEqual(['-team.title']);
|
|
65
|
+
expect(getTableSorterOrder({ column }, undefined, defaults)).toEqual(defaults);
|
|
66
|
+
expect(getTableSorterOrder({ column })).toEqual([]);
|
|
67
|
+
expect(getTableSorterOrder({ column }, 'position', defaults)).toEqual(['position']);
|
|
68
|
+
});
|
|
@@ -50,3 +50,13 @@ export function resolveTableSorterField(sorter: {
|
|
|
50
50
|
}) {
|
|
51
51
|
return sorter?.column?.sortField || sorter?.field;
|
|
52
52
|
}
|
|
53
|
+
|
|
54
|
+
export function getTableSorterOrder(
|
|
55
|
+
sorter: Parameters<typeof resolveTableSorterField>[0] & { order?: string },
|
|
56
|
+
dragSortField?: string,
|
|
57
|
+
defaultSort: string[] = [],
|
|
58
|
+
) {
|
|
59
|
+
const field = resolveTableSorterField(sorter);
|
|
60
|
+
if (sorter?.order && field) return [sorter.order === 'ascend' ? field : `-${field}`];
|
|
61
|
+
return dragSortField ? [dragSortField] : defaultSort;
|
|
62
|
+
}
|
|
@@ -11,7 +11,7 @@ import { UserOutlined } from '@ant-design/icons';
|
|
|
11
11
|
import { css, cx } from '@emotion/css';
|
|
12
12
|
import { FlowModel, FlowModelRenderer, observer } from '@nocobase/flow-engine';
|
|
13
13
|
import { Divider, Dropdown, Empty, Select, Spin, theme } from 'antd';
|
|
14
|
-
import React, { useCallback, useContext, useEffect, useState } from 'react';
|
|
14
|
+
import React, { useCallback, useContext, useEffect, useLayoutEffect, useState } from 'react';
|
|
15
15
|
import { ErrorBoundary } from 'react-error-boundary';
|
|
16
16
|
import { createAclSnippetAllow } from '../../../acl/createAclSnippetAllow';
|
|
17
17
|
import { MobileTopbarActionsContext } from '../../admin-shell/admin-layout/MobileTopbarActionsContext';
|
|
@@ -240,6 +240,9 @@ export class UserCenterItemModel extends FlowModel {
|
|
|
240
240
|
|
|
241
241
|
async prepare() {}
|
|
242
242
|
|
|
243
|
+
/** Recheck this item on each opening without delaying other menu items. */
|
|
244
|
+
checkAvailability?(): Promise<boolean>;
|
|
245
|
+
|
|
243
246
|
render() {
|
|
244
247
|
return <UserCenterTextItemView model={this as UserCenterTextItemModel} />;
|
|
245
248
|
}
|
|
@@ -373,6 +376,9 @@ const UserCenterSelectItemView = observer(
|
|
|
373
376
|
{ displayName: 'UserCenterSelectItemView' },
|
|
374
377
|
);
|
|
375
378
|
|
|
379
|
+
// Ant Design caches the hidden dropdown. Context lets availability checks observe every opening.
|
|
380
|
+
const UserCenterOpenContext = React.createContext(false);
|
|
381
|
+
|
|
376
382
|
const UserCenterDropdownPanel = (props: {
|
|
377
383
|
model: UserCenterTopbarActionModel;
|
|
378
384
|
open: boolean;
|
|
@@ -381,7 +387,9 @@ const UserCenterDropdownPanel = (props: {
|
|
|
381
387
|
const { model, open, closeDropdown } = props;
|
|
382
388
|
const { token } = theme.useToken();
|
|
383
389
|
const [loading, setLoading] = useState(false);
|
|
384
|
-
const [
|
|
390
|
+
const [preparedItems, setPreparedItems] = useState<UserCenterItemModel[]>([]);
|
|
391
|
+
const menuOpen = useContext(UserCenterOpenContext);
|
|
392
|
+
const [availability, setAvailability] = useState(new Map<UserCenterItemModel, boolean>());
|
|
385
393
|
|
|
386
394
|
useEffect(() => {
|
|
387
395
|
if (!open) {
|
|
@@ -390,37 +398,57 @@ const UserCenterDropdownPanel = (props: {
|
|
|
390
398
|
|
|
391
399
|
let cancelled = false;
|
|
392
400
|
setLoading(true);
|
|
393
|
-
|
|
401
|
+
setPreparedItems([]);
|
|
394
402
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (!cancelled)
|
|
399
|
-
|
|
400
|
-
}
|
|
401
|
-
})
|
|
402
|
-
.catch((error) => {
|
|
403
|
+
async function prepareItems() {
|
|
404
|
+
try {
|
|
405
|
+
const items = await model.getPreparedItems(closeDropdown);
|
|
406
|
+
if (!cancelled) setPreparedItems(items);
|
|
407
|
+
} catch (error) {
|
|
403
408
|
console.error('[NocoBase] User center prepare failed.', error);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
});
|
|
409
|
+
} finally {
|
|
410
|
+
if (!cancelled) setLoading(false);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
prepareItems();
|
|
410
414
|
|
|
411
415
|
return () => {
|
|
412
416
|
cancelled = true;
|
|
413
417
|
};
|
|
414
418
|
}, [closeDropdown, model, open]);
|
|
415
419
|
|
|
420
|
+
useLayoutEffect(() => {
|
|
421
|
+
// Keep the overlay stable during its exit animation. Clear old availability before the next opening paints.
|
|
422
|
+
if (!menuOpen) return;
|
|
423
|
+
let cancelled = false;
|
|
424
|
+
setAvailability(new Map());
|
|
425
|
+
async function checkItem(item: UserCenterItemModel) {
|
|
426
|
+
if (!item.checkAvailability) return;
|
|
427
|
+
let available = false;
|
|
428
|
+
try {
|
|
429
|
+
available = await item.checkAvailability();
|
|
430
|
+
} catch (error) {
|
|
431
|
+
console.error(`[NocoBase] User center item '${item.getItemId()}' availability check failed.`, error);
|
|
432
|
+
}
|
|
433
|
+
if (!cancelled) setAvailability((previous) => new Map(previous).set(item, available));
|
|
434
|
+
}
|
|
435
|
+
preparedItems.forEach(checkItem);
|
|
436
|
+
return () => {
|
|
437
|
+
cancelled = true;
|
|
438
|
+
};
|
|
439
|
+
}, [menuOpen, preparedItems]);
|
|
440
|
+
|
|
441
|
+
const items = preparedItems.filter((item) => !item.checkAvailability || availability.get(item));
|
|
442
|
+
const waiting = loading || (!items.length && preparedItems.some((item) => !availability.has(item)));
|
|
443
|
+
|
|
416
444
|
return (
|
|
417
445
|
<div className={userCenterDropdownClassName} style={getUserCenterDropdownVars(token)}>
|
|
418
|
-
{
|
|
446
|
+
{waiting ? (
|
|
419
447
|
<div className="nb-user-center-loading">
|
|
420
448
|
<Spin size="small" />
|
|
421
449
|
</div>
|
|
422
450
|
) : null}
|
|
423
|
-
{!
|
|
451
|
+
{!waiting && !items.length ? (
|
|
424
452
|
<div className="nb-user-center-empty">
|
|
425
453
|
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={model.context.t('No data')} />
|
|
426
454
|
</div>
|
|
@@ -460,15 +488,17 @@ const UserCenterTopbarActionView = observer(
|
|
|
460
488
|
}, [closeMobileTopbar]);
|
|
461
489
|
|
|
462
490
|
return model.renderWrapper(
|
|
463
|
-
<
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
491
|
+
<UserCenterOpenContext.Provider value={open}>
|
|
492
|
+
<Dropdown
|
|
493
|
+
trigger={['hover']}
|
|
494
|
+
placement="bottomRight"
|
|
495
|
+
open={open}
|
|
496
|
+
onOpenChange={setOpen}
|
|
497
|
+
dropdownRender={() => <UserCenterDropdownPanel model={model} open={open} closeDropdown={closeDropdown} />}
|
|
498
|
+
>
|
|
499
|
+
<span className={userCenterTriggerClassName}>{model.renderButton()}</span>
|
|
500
|
+
</Dropdown>
|
|
501
|
+
</UserCenterOpenContext.Provider>,
|
|
472
502
|
);
|
|
473
503
|
},
|
|
474
504
|
{ displayName: 'UserCenterTopbarActionView' },
|