2022-06-28 07:42:42 +00:00
|
|
|
import { useCurrentStateAndParams, useRouter } from '@uirouter/react';
|
2022-07-28 17:24:25 +00:00
|
|
|
import { useEffect } from 'react';
|
2022-11-28 02:00:28 +00:00
|
|
|
import { X, Slash } from 'lucide-react';
|
2022-08-10 04:12:20 +00:00
|
|
|
import clsx from 'clsx';
|
2022-12-13 20:56:09 +00:00
|
|
|
import { useStore } from 'zustand';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
PlatformType,
|
|
|
|
EnvironmentId,
|
|
|
|
Environment,
|
2022-10-23 06:53:25 +00:00
|
|
|
} from '@/react/portainer/environments/types';
|
|
|
|
import { getPlatformType } from '@/react/portainer/environments/utils';
|
|
|
|
import { useEnvironment } from '@/react/portainer/environments/queries/useEnvironment';
|
2022-11-13 10:29:25 +00:00
|
|
|
import { isBE } from '@/react/portainer/feature-flags/feature-flags.service';
|
2022-12-13 20:56:09 +00:00
|
|
|
import { environmentStore } from '@/react/hooks/current-environment-store';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-11-28 02:00:28 +00:00
|
|
|
import { Icon } from '@@/Icon';
|
|
|
|
|
2022-06-28 07:42:42 +00:00
|
|
|
import { getPlatformIcon } from '../portainer/environments/utils/get-platform-icon';
|
|
|
|
|
2022-09-09 00:47:06 +00:00
|
|
|
import styles from './EnvironmentSidebar.module.css';
|
2022-06-23 07:25:56 +00:00
|
|
|
import { AzureSidebar } from './AzureSidebar';
|
|
|
|
import { DockerSidebar } from './DockerSidebar';
|
|
|
|
import { KubernetesSidebar } from './KubernetesSidebar';
|
2022-07-06 05:09:14 +00:00
|
|
|
import { SidebarSection, SidebarSectionTitle } from './SidebarSection';
|
2022-06-28 07:42:42 +00:00
|
|
|
import { useSidebarState } from './useSidebarState';
|
2022-11-13 10:29:25 +00:00
|
|
|
import { NomadSidebar } from './NomadSidebar';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
|
|
|
export function EnvironmentSidebar() {
|
2022-06-28 07:42:42 +00:00
|
|
|
const { query: currentEnvironmentQuery, clearEnvironment } =
|
|
|
|
useCurrentEnvironment();
|
2022-06-23 07:25:56 +00:00
|
|
|
const environment = currentEnvironmentQuery.data;
|
2022-07-06 05:09:14 +00:00
|
|
|
|
|
|
|
const { isOpen } = useSidebarState();
|
|
|
|
|
|
|
|
if (!isOpen && !environment) {
|
2022-06-23 07:25:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-06 05:09:14 +00:00
|
|
|
return (
|
2022-09-09 00:47:06 +00:00
|
|
|
<div className={clsx(styles.root, 'rounded border border-dotted py-2')}>
|
2022-07-06 05:09:14 +00:00
|
|
|
{environment ? (
|
|
|
|
<Content environment={environment} onClear={clearEnvironment} />
|
|
|
|
) : (
|
|
|
|
<SidebarSectionTitle>
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
<span>Environment:</span>
|
2022-11-28 02:00:28 +00:00
|
|
|
<Icon icon={Slash} className="text-xl !text-gray-6" />
|
2023-02-12 21:04:24 +00:00
|
|
|
<span className="text-sm text-gray-6">None selected</span>
|
2022-07-06 05:09:14 +00:00
|
|
|
</div>
|
|
|
|
</SidebarSectionTitle>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ContentProps {
|
|
|
|
environment: Environment;
|
|
|
|
onClear: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Content({ environment, onClear }: ContentProps) {
|
2022-06-23 07:25:56 +00:00
|
|
|
const platform = getPlatformType(environment.Type);
|
2022-06-28 07:42:42 +00:00
|
|
|
const Sidebar = getSidebar(platform);
|
2022-06-23 07:25:56 +00:00
|
|
|
|
|
|
|
return (
|
2022-07-06 05:09:14 +00:00
|
|
|
<SidebarSection
|
|
|
|
title={<Title environment={environment} onClear={onClear} />}
|
|
|
|
aria-label={environment.Name}
|
|
|
|
showTitleWhenOpen
|
|
|
|
>
|
|
|
|
<div className="mt-2">
|
2022-11-13 10:29:25 +00:00
|
|
|
{Sidebar && (
|
|
|
|
<Sidebar environmentId={environment.Id} environment={environment} />
|
|
|
|
)}
|
2022-07-06 05:09:14 +00:00
|
|
|
</div>
|
|
|
|
</SidebarSection>
|
2022-06-28 07:42:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
function getSidebar(platform: PlatformType) {
|
|
|
|
const sidebar: {
|
|
|
|
[key in PlatformType]: React.ComponentType<{
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
environment: Environment;
|
2022-11-13 10:29:25 +00:00
|
|
|
}> | null;
|
2022-06-28 07:42:42 +00:00
|
|
|
} = {
|
|
|
|
[PlatformType.Azure]: AzureSidebar,
|
|
|
|
[PlatformType.Docker]: DockerSidebar,
|
|
|
|
[PlatformType.Kubernetes]: KubernetesSidebar,
|
2022-11-13 10:29:25 +00:00
|
|
|
[PlatformType.Nomad]: isBE ? NomadSidebar : null,
|
2022-06-28 07:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return sidebar[platform];
|
2022-06-23 07:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function useCurrentEnvironment() {
|
|
|
|
const { params } = useCurrentStateAndParams();
|
2022-06-28 07:42:42 +00:00
|
|
|
const router = useRouter();
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-12-13 20:56:09 +00:00
|
|
|
const envStore = useStore(environmentStore);
|
|
|
|
const { setEnvironmentId } = envStore;
|
2022-06-23 07:25:56 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const environmentId = parseInt(params.endpointId, 10);
|
|
|
|
if (params.endpointId && !Number.isNaN(environmentId)) {
|
|
|
|
setEnvironmentId(environmentId);
|
|
|
|
}
|
2022-12-13 20:56:09 +00:00
|
|
|
}, [setEnvironmentId, params.endpointId, params.environmentId]);
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-12-13 20:56:09 +00:00
|
|
|
return { query: useEnvironment(envStore.environmentId), clearEnvironment };
|
2022-06-28 07:42:42 +00:00
|
|
|
|
|
|
|
function clearEnvironment() {
|
2022-12-13 20:56:09 +00:00
|
|
|
if (params.endpointId || params.environmentId) {
|
2022-06-28 07:42:42 +00:00
|
|
|
router.stateService.go('portainer.home');
|
|
|
|
}
|
|
|
|
|
2022-12-13 20:56:09 +00:00
|
|
|
envStore.clear();
|
2022-06-28 07:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TitleProps {
|
|
|
|
environment: Environment;
|
|
|
|
onClear(): void;
|
|
|
|
}
|
|
|
|
|
2022-07-06 05:09:14 +00:00
|
|
|
function Title({ environment, onClear }: TitleProps) {
|
2022-06-28 07:42:42 +00:00
|
|
|
const { isOpen } = useSidebarState();
|
2022-07-06 05:09:14 +00:00
|
|
|
|
2022-06-28 07:42:42 +00:00
|
|
|
const EnvironmentIcon = getPlatformIcon(environment.Type);
|
|
|
|
|
|
|
|
if (!isOpen) {
|
|
|
|
return (
|
2023-02-12 21:04:24 +00:00
|
|
|
<div className="-ml-3 flex w-8 justify-center" title={environment.Name}>
|
2022-06-28 07:42:42 +00:00
|
|
|
<EnvironmentIcon className="text-2xl" />
|
2022-07-06 05:09:14 +00:00
|
|
|
</div>
|
2022-06-28 07:42:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-07-06 05:09:14 +00:00
|
|
|
<div className="flex items-center">
|
2023-02-12 21:04:24 +00:00
|
|
|
<EnvironmentIcon className="mr-3 text-2xl" />
|
|
|
|
<span className="overflow-hidden text-ellipsis whitespace-nowrap text-white">
|
2022-07-06 05:09:14 +00:00
|
|
|
{environment.Name}
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<button
|
|
|
|
title="Clear environment"
|
|
|
|
type="button"
|
|
|
|
onClick={onClear}
|
2022-09-09 00:47:06 +00:00
|
|
|
className={clsx(
|
|
|
|
styles.closeBtn,
|
2023-02-12 21:04:24 +00:00
|
|
|
'ml-auto mr-2 flex h-5 w-5 items-center justify-center rounded border-0 p-1 text-sm text-gray-5 transition-colors duration-200 hover:text-white be:text-gray-6 be:hover:text-white'
|
2022-09-09 00:47:06 +00:00
|
|
|
)}
|
2022-07-06 05:09:14 +00:00
|
|
|
>
|
|
|
|
<X />
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-06-28 07:42:42 +00:00
|
|
|
);
|
2022-06-23 07:25:56 +00:00
|
|
|
}
|