2022-06-01 04:28:31 +00:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { useAgentDetails } from '@/react/portainer/environments/queries/useAgentDetails';
|
2022-06-01 04:28:31 +00:00
|
|
|
|
2022-06-17 16:18:42 +00:00
|
|
|
import { Code } from '@@/Code';
|
|
|
|
import { CopyButton } from '@@/buttons/CopyButton';
|
|
|
|
import { NavTabs } from '@@/NavTabs';
|
2023-10-19 11:45:50 +00:00
|
|
|
import { NavContainer } from '@@/NavTabs/NavContainer';
|
2022-06-17 16:18:42 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
import { ScriptFormValues, Platform } from './types';
|
|
|
|
import { CommandTab } from './scripts';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
values: ScriptFormValues;
|
|
|
|
edgeKey: string;
|
|
|
|
edgeId?: string;
|
|
|
|
commands: CommandTab[];
|
|
|
|
platform?: Platform;
|
|
|
|
onPlatformChange?(platform: Platform): void;
|
2023-03-01 18:33:05 +00:00
|
|
|
asyncMode?: boolean;
|
2022-06-01 04:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ScriptTabs({
|
|
|
|
values,
|
|
|
|
edgeKey,
|
|
|
|
edgeId,
|
|
|
|
commands,
|
|
|
|
platform,
|
2023-03-01 18:33:05 +00:00
|
|
|
asyncMode = false,
|
2022-06-01 04:28:31 +00:00
|
|
|
onPlatformChange = () => {},
|
|
|
|
}: Props) {
|
|
|
|
const agentDetails = useAgentDetails();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (commands.length > 0 && commands.every((p) => p.id !== platform)) {
|
|
|
|
onPlatformChange(commands[0].id);
|
|
|
|
}
|
|
|
|
}, [platform, onPlatformChange, commands]);
|
|
|
|
|
|
|
|
if (!agentDetails) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:33:05 +00:00
|
|
|
const { agentSecret, agentVersion } = agentDetails;
|
2022-06-01 04:28:31 +00:00
|
|
|
|
|
|
|
const options = commands.map((c) => {
|
2022-11-21 07:51:55 +00:00
|
|
|
const cmd = c.command(
|
|
|
|
agentVersion,
|
|
|
|
edgeKey,
|
|
|
|
values,
|
2023-03-01 18:33:05 +00:00
|
|
|
asyncMode,
|
2022-11-21 07:51:55 +00:00
|
|
|
edgeId,
|
|
|
|
agentSecret
|
|
|
|
);
|
2022-06-01 04:28:31 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: c.id,
|
|
|
|
label: c.label,
|
|
|
|
children: (
|
|
|
|
<>
|
|
|
|
<Code>{cmd}</Code>
|
2023-02-22 20:13:33 +00:00
|
|
|
<div className="mt-2">
|
|
|
|
<CopyButton copyText={cmd}>Copy</CopyButton>
|
|
|
|
</div>
|
2022-06-01 04:28:31 +00:00
|
|
|
</>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2023-10-19 11:45:50 +00:00
|
|
|
<NavContainer>
|
|
|
|
<NavTabs
|
|
|
|
selectedId={platform}
|
|
|
|
options={options}
|
|
|
|
onSelect={(id: Platform) => onPlatformChange(id)}
|
|
|
|
/>
|
|
|
|
</NavContainer>
|
2022-06-01 04:28:31 +00:00
|
|
|
);
|
|
|
|
}
|