Files
pterodactyl-panel/resources/scripts/components/admin/eggs/ExportEggButton.tsx
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

47 lines
1.5 KiB
TypeScript

import type { AdminEgg } from '@/api/admin/eggs/queries';
import { useExportAdminEgg } from '@/api/admin/eggs/queries';
import Button from '@/components/elements/Button';
import { notifyHttpError } from '@/plugins/notifications';
interface Props {
egg: AdminEgg;
}
const kebab = (value: string): string =>
value
.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
.toLowerCase()
.replace(/^-+|-+$/g, '');
export default function ExportEggButton({ egg }: Props) {
const exportEgg = useExportAdminEgg(egg.attributes.id);
const download = async () => {
try {
const { data: json } = await exportEgg.refetch({ throwOnError: true });
if (!json) {
return;
}
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `egg-${kebab(egg.attributes.name)}.json`;
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
} catch (error) {
notifyHttpError(error, 'Unable to export egg');
}
};
return (
<Button isSecondary disabled={exportEgg.isFetching} isLoading={exportEgg.isFetching} onClick={download}>
Export
</Button>
);
}