import { useMemo } from 'react';
import { GroupBase } from 'react-select';
import { useCustomTemplates } from '@/react/portainer/templates/custom-templates/queries/useCustomTemplates';
import { useAppTemplates } from '@/react/portainer/templates/app-templates/queries/useAppTemplates';
import { TemplateType } from '@/react/portainer/templates/app-templates/types';
import { TemplateViewModel } from '@/react/portainer/templates/app-templates/view-model';
import { CustomTemplate } from '@/react/portainer/templates/custom-templates/types';
import { FormControl } from '@@/form-components/FormControl';
import { Select as ReactSelect } from '@@/form-components/ReactSelect';
import { SelectedTemplateValue } from './types';
export function TemplateSelector({
value,
onChange,
error,
}: {
value: SelectedTemplateValue;
onChange: (
template: TemplateViewModel | CustomTemplate | undefined,
type: 'app' | 'custom' | undefined
) => void;
error?: string;
}) {
const { options, getTemplate, selectedValue } = useOptions(value);
return (
{
if (!value) {
onChange(undefined, undefined);
return;
}
const { templateId, type } = value;
if (!templateId || type === undefined) {
return;
}
onChange(getTemplate({ type, id: templateId }), type);
}}
data-cy="edge-stacks-create-template-selector"
/>
);
}
interface Option {
label: string;
templateId?: number;
type: 'app' | 'custom';
}
function useOptions(value: SelectedTemplateValue) {
const customTemplatesQuery = useCustomTemplates({
params: {
edge: true,
},
});
const appTemplatesQuery = useAppTemplates({
select: (templates) =>
templates.filter(
(template) =>
template.Categories.includes('edge') &&
template.Type !== TemplateType.Container
),
});
const appTemplateOptions: Array