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-07-06 05:09:14 +00:00
|
|
|
import { X, Slash } from 'react-feather';
|
2022-08-10 04:12:20 +00:00
|
|
|
import clsx from 'clsx';
|
2022-09-06 23:08:45 +00:00
|
|
|
import angular from 'angular';
|
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 08:10:18 +00:00
|
|
|
import { useLocalStorage } from '@/react/hooks/useLocalStorage';
|
2022-11-02 11:29:26 +00:00
|
|
|
import { EndpointProviderInterface } from '@/portainer/services/endpointProvider';
|
2022-11-13 10:29:25 +00:00
|
|
|
import { isBE } from '@/react/portainer/feature-flags/feature-flags.service';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
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>
|
|
|
|
<Slash size="1em" className="text-xl text-gray-6" />
|
|
|
|
<span className="text-gray-6 text-sm">None selected</span>
|
|
|
|
</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-07-28 17:24:25 +00:00
|
|
|
const [environmentId, setEnvironmentId] = useLocalStorage<
|
|
|
|
EnvironmentId | undefined
|
|
|
|
>('environmentId', undefined, sessionStorage);
|
2022-06-23 07:25:56 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const environmentId = parseInt(params.endpointId, 10);
|
|
|
|
if (params.endpointId && !Number.isNaN(environmentId)) {
|
|
|
|
setEnvironmentId(environmentId);
|
|
|
|
}
|
2022-07-28 17:24:25 +00:00
|
|
|
}, [params.endpointId, setEnvironmentId]);
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-06-28 07:42:42 +00:00
|
|
|
return { query: useEnvironment(environmentId), clearEnvironment };
|
|
|
|
|
|
|
|
function clearEnvironment() {
|
2022-09-06 23:08:45 +00:00
|
|
|
const $injector = angular.element(document).injector();
|
|
|
|
$injector.invoke(
|
2022-11-02 11:29:26 +00:00
|
|
|
/* @ngInject */ (EndpointProvider: EndpointProviderInterface) => {
|
|
|
|
EndpointProvider.setCurrentEndpoint(null);
|
2022-09-06 23:08:45 +00:00
|
|
|
if (!params.endpointId) {
|
|
|
|
document.title = 'Portainer';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-06-28 07:42:42 +00:00
|
|
|
if (params.endpointId) {
|
|
|
|
router.stateService.go('portainer.home');
|
|
|
|
}
|
|
|
|
|
|
|
|
setEnvironmentId(undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2022-07-06 05:09:14 +00:00
|
|
|
<div className="w-8 flex justify-center -ml-3" 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">
|
|
|
|
<EnvironmentIcon className="text-2xl mr-3" />
|
|
|
|
<span className="text-white text-ellipsis overflow-hidden whitespace-nowrap">
|
|
|
|
{environment.Name}
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<button
|
|
|
|
title="Clear environment"
|
|
|
|
type="button"
|
|
|
|
onClick={onClear}
|
2022-09-09 00:47:06 +00:00
|
|
|
className={clsx(
|
|
|
|
styles.closeBtn,
|
|
|
|
'flex items-center justify-center transition-colors duration-200 rounded border-0 text-sm h-5 w-5 p-1 ml-auto mr-2 text-gray-5 be:text-gray-6 hover:text-white be:hover:text-white'
|
|
|
|
)}
|
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
|
|
|
}
|