diff --git a/src/pages/ModulesPage/index.tsx b/src/pages/ModulesPage/index.tsx new file mode 100644 index 0000000..e5e6e37 --- /dev/null +++ b/src/pages/ModulesPage/index.tsx @@ -0,0 +1,239 @@ +import { ExclamationCircleOutlined } from '@ant-design/icons' +import { List, message, Spin, Tabs, Tag, Tooltip } from 'antd' +import { compareVersions } from 'compare-versions' +import { Layout } from 'isp-ui-kit' +import { useEffect, useState } from 'react' +import { + createSearchParams, + Outlet, + useLocation, + useNavigate, + useParams, + useSearchParams +} from 'react-router-dom' + +import { ModuleType } from '@pages/ModulesPage/module.type.ts' + +import { filterFirstColumnItems } from '@utils/firstColumnUtils.ts' + +import useRole from '@hooks/useRole.tsx' + +import modulesServiceApi from '@services/modulesService.ts' + +import { routePaths } from '@routes/routePaths.ts' + +import { PermissionKeysType } from '@type/roles.type.ts' + +import './modules-page.scss' + +const { Column, EmptyData, NoData } = Layout + +const ModulesPage = () => { + const [activeTab, setActiveTab] = useState('configurations') + const { id: selectedItemId = '' } = useParams() + const navigate = useNavigate() + const { role, hasPermission } = useRole() + const [searchParams, setSearchParams] = useSearchParams() + const { data: ModulesList = [], isLoading: isModulesLoading } = + modulesServiceApi.useGetModulesQuery('modules', { + pollingInterval: 1000, + skipPollingIfUnfocused: true + }) + const [removeModule] = modulesServiceApi.useRemoveModuleMutation() + const location = useLocation() + + const searchValue = searchParams.get('search') || '' + const isPageAvailable = hasPermission(PermissionKeysType.read) + const isRemoveModule = hasPermission(PermissionKeysType.write) + const isLoading = isModulesLoading + + useEffect(() => { + if (!isPageAvailable) { + navigate(routePaths.home) + } + }, [isPageAvailable]) + + useEffect(() => { + if (!role) { + navigate(routePaths.error) + } + }, [role]) + + useEffect(() => { + const url = location.pathname.substring( + location.pathname.lastIndexOf('/') + 1 + ) + switch (url) { + case 'configurations': + return setActiveTab('configurations') + case 'connections': + return setActiveTab('connections') + } + }, [location]) + + const setSelectedItemId = (id: string): void => { + if (id) { + navigate( + { + pathname: `${routePaths.modules}/${id}/${activeTab}`, + search: createSearchParams(searchParams).toString() + }, + { replace: true } + ) + } + } + + const handleOnChangeSearchValue = (value: string): void => { + setSearchParams((prev) => { + if (!value) { + prev.delete('search') + } else { + prev.set('search', value) + } + return prev + }) + } + + const handleRemoveModule = (id: string) => { + removeModule(id) + .unwrap() + .then(() => { + navigate(routePaths.modules) + message.success('Модуль успешно удален').then() + setSelectedItemId('') + }) + .catch(() => message.error('Не удалось удалить модуль')) + } + + const versionCompare = (versions: string[]) => { + const sorted = versions?.sort(compareVersions) + const firstVersion = sorted[0] + for (let i = 1; i < sorted.length; i++) { + if (compareVersions(firstVersion, sorted[i]) !== 0) { + return false + } + } + return true + } + + const getLastVersion = (versions: string[]) => { + const sorted = versions?.sort(compareVersions) + return sorted[sorted.length - 1] + } + + if (isLoading) { + return + } + + const currentRole = ModulesList + ? ModulesList.find( + (item: ModuleType) => item.id.toString() === selectedItemId + ) + : null + + const renderItems = (item: ModuleType) => { + const isSame = versionCompare(item.status.map((i) => i.version)) + const textTooltip = isSame + ? 'Активны экземпляры одинаковых версий' + : 'Активны экземпляры разных версий' + const lastVersion = getLastVersion(item.status.map((i) => i.version)) + + const renderVersion = () => { + if (!isSame && lastVersion) { + return ( + } color="warning"> + {lastVersion} + + ) + } + if (isSame && lastVersion) { + return {lastVersion} + } + return '' + } + + return ( + +
+
+ {item.name} + + {renderVersion()} + +
+
+ + активные экземпляры: + + + {(item.status && item.status.length) || '0'} + +
+
+
+ ) + } + + const renderMainContent = () => { + if (!selectedItemId) { + return + } + + if (!currentRole) { + return + } + + return + } + const secondColumnItems = [ + { + key: 'configurations', + name: 'Конфигурации' + }, + { key: 'connections', name: 'Подключения' } + ] + + return ( +
+ + { + setActiveTab(activeKey) + const path = `${selectedItemId}/${activeKey}` + navigate(path) + }} + className="modules-page__tabs" + tabPosition={'left'} + items={secondColumnItems.map((item) => { + return { + disabled: selectedItemId === '', + label: item.name, + key: item.key, + children: ( +
{renderMainContent()}
+ ) + } + })} + /> +
+ ) +} + +export default ModulesPage