nocobase/packages/core/client/src/user/CurrentUser.tsx
被雨水过滤的空气-Rain a57c93d35b
feat: support e2e (#2624)
* chore: upgrade vitest to v0.34.3

* feat: setup NocoBase

* chore: preparing test env

* test: add a test of rigster

* refactor: rename test dir to testUtils

* chore: add tests

* chore: add ci for e2e

* chore: fix ci

* chore: avoid error in CI

* chore: add some utils for test

* chore: make more stable

* chore: should not close server in CI

* chore: add comments

* chore: change output dir

* fix: should use current branch to run tests

* chore: should request systemSettings by api in e2e

* chore: should build first in e2e CI

* chore: remove key

* chore: use execa to replace execSync

* refactor: extract test suite

* chore: add gotoPage

* chore: update uid of pageSchema

* chore: update collection name

* chore: use faker.js to generate data

* refactor: extract page config

* chore: ignore for association fields in faker

* chore: add testid

* chore: optimize action designer

* chore: associationFilter.Item designer

* chore: AssiciationFilter & BlockItem

* Revert "chore: AssiciationFilter & BlockItem"

This reverts commit b418df650e106fd0c8e23035d2f75acf60dcafe4.

* Revert "chore: associationFilter.Item designer"

This reverts commit 7aa4d35c1af7f3a780b370d8b1b44aac01697be3.

* Revert "chore: optimize action designer"

This reverts commit ff717b972ffd64f7968d565a3a84ad617ff889e2.

* chore: optimize Designer

* chore: compat with older browsers

* chore: use describe to avoid hooks is not run

* chore: add no-floating-promises to eslint rules

* chore: support argv

* chore: demo

* chore: better testId

* chore: change .e2e.ts to .test.ts

* fix(SchemaInitializer): avoid error

* refactor: move e2eUtils.ts to @nocobase/test

* fix: move e2eUtils to client

* chore: remove uselesscode

* refactor: add .env.e2e.example

* chore: optimize log

* refactor: use mockPage to replace gotoPage

* chore: update env.e2e

* chore: add APP_BASE_URL

* chore: gitigore

* test: add test related of menu

* chore: add SOCKET_PATH in env

* fix(vscode): load env when using vscode plugin
2023-09-27 20:00:17 +08:00

193 lines
5.3 KiB
TypeScript

import { css } from '@emotion/css';
import { error } from '@nocobase/utils/client';
import { App, Dropdown, Menu, MenuProps } from 'antd';
import React, { createContext, useCallback, useMemo as useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { useACLRoleContext, useAPIClient, useCurrentUserContext } from '..';
import { useCurrentAppInfo } from '../appInfo/CurrentAppInfoProvider';
import { useChangePassword } from './ChangePassword';
import { useCurrentUserSettingsMenu } from './CurrentUserSettingsMenuProvider';
import { useEditProfile } from './EditProfile';
import { useLanguageSettings } from './LanguageSettings';
import { useSwitchRole } from './SwitchRole';
const useApplicationVersion = () => {
const data = useCurrentAppInfo();
return useEffect(() => {
return {
key: 'version',
disabled: true,
label: `Version ${data?.data?.version}`,
};
}, [data?.data?.version]);
};
/**
* @note If you want to change here, Note the Setting block on the mobile side
*/
export const SettingsMenu: React.FC<{
redirectUrl?: string;
}> = (props) => {
const { addMenuItem, getMenuItems } = useCurrentUserSettingsMenu();
const { redirectUrl = '' } = props;
const { allowAll, snippets } = useACLRoleContext();
const appAllowed = allowAll || snippets?.includes('app');
const navigate = useNavigate();
const api = useAPIClient();
const { t } = useTranslation();
const silenceApi = useAPIClient();
const check = useCallback(async () => {
return await new Promise((resolve) => {
const heartbeat = setInterval(() => {
silenceApi
.silent()
.resource('app')
.getInfo()
.then((res) => {
if (res?.status === 200) {
resolve('ok');
clearInterval(heartbeat);
}
return res;
})
.catch((err) => {
error(err);
});
}, 3000);
});
}, [silenceApi]);
const appVersion = useApplicationVersion();
const editProfile = useEditProfile();
const changePassword = useChangePassword();
const switchRole = useSwitchRole();
const languageSettings = useLanguageSettings();
const { modal } = App.useApp();
const controlApp = useEffect<MenuProps['items']>(() => {
if (!appAllowed) {
return [];
}
return [
{
key: 'cache',
label: t('Clear cache'),
onClick: async () => {
await api.resource('app').clearCache();
window.location.reload();
},
},
{
key: 'reboot',
label: t('Restart application'),
onClick: async () => {
modal.confirm({
title: t('Restart application'),
// content: t('The will interrupt service, it may take a few seconds to restart. Are you sure to continue?'),
okText: t('Restart'),
okButtonProps: {
danger: true,
},
onOk: async () => {
await api.resource('app').restart();
},
});
},
},
{
key: 'divider_4',
type: 'divider',
},
];
}, [api, appAllowed, check, modal, t]);
useEffect(() => {
const items = [
appVersion,
{
key: 'divider_1',
type: 'divider',
},
editProfile,
changePassword,
{
key: 'divider_2',
type: 'divider',
},
switchRole,
{
key: 'divider_3',
type: 'divider',
},
...controlApp,
{
key: 'signout',
label: t('Sign out'),
onClick: async () => {
await api.auth.signOut();
navigate(`/signin?redirect=${encodeURIComponent(redirectUrl)}`);
},
},
];
items.forEach((item) => {
if (item) {
addMenuItem(item);
}
});
if (languageSettings) {
addMenuItem(languageSettings, { before: 'divider_3' });
}
}, [
addMenuItem,
api.auth,
appVersion,
changePassword,
controlApp,
editProfile,
languageSettings,
navigate,
redirectUrl,
switchRole,
t,
]);
return <Menu items={getMenuItems()} />;
};
export const DropdownVisibleContext = createContext(null);
export const CurrentUser = () => {
const [visible, setVisible] = useState(false);
const { data } = useCurrentUserContext();
return (
<div style={{ display: 'inline-flex', verticalAlign: 'top' }}>
<DropdownVisibleContext.Provider value={{ visible, setVisible }}>
<Dropdown
open={visible}
onOpenChange={(visible) => {
setVisible(visible);
}}
dropdownRender={() => {
return <SettingsMenu />;
}}
>
<span
data-testid="user-center-button"
className={css`
max-width: 160px;
overflow: hidden;
display: inline-block;
line-height: 12px;
white-space: nowrap;
text-overflow: ellipsis;
`}
style={{ cursor: 'pointer', border: 0, padding: '16px', color: 'rgba(255, 255, 255, 0.65)' }}
>
{data?.data?.nickname || data?.data?.username || data?.data?.email}
</span>
</Dropdown>
</DropdownVisibleContext.Provider>
</div>
);
};