Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.2 KB
Raw Blame
import type { SystemStyleObject } from '@invoke-ai/ui-library';
import { IconButton, spinAnimation } from '@invoke-ai/ui-library';
import { EMPTY_ARRAY } from 'app/store/constants';
import { toast } from 'features/toast/toast';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { PiDownloadSimpleBold, PiSpinner } from 'react-icons/pi';
import { useLazyExportStylePresetsQuery, useListStylePresetsQuery } from 'services/api/endpoints/stylePresets';

const loadingStyles: SystemStyleObject = {
  svg: { animation: spinAnimation },
};

export const StylePresetExportButton = () => {
  const [exportStylePresets, { isLoading }] = useLazyExportStylePresetsQuery();
  const { t } = useTranslation();
  const { presetCount } = useListStylePresetsQuery(undefined, {
    selectFromResult: ({ data }) => {
      const presetsToExport = data?.filter((preset) => preset.type !== 'default') ?? EMPTY_ARRAY;
      return {
        presetCount: presetsToExport.length,
      };
    },
  });
  const handleClickDownloadCsv = useCallback(async () => {
    let blob;
    try {
      const response = await exportStylePresets().unwrap();
      blob = new Blob([response], { type: 'text/csv' });
    } catch {
      toast({
        status: 'error',
        title: t('stylePresets.exportFailed'),
      });
      return;
    }

    if (blob) {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'data.csv';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
      toast({
        status: 'success',
        title: t('stylePresets.exportDownloaded'),
      });
    }
  }, [exportStylePresets, t]);

  return (
    <IconButton
      size="sm"
      variant="link"
      alignSelf="stretch"
      onClick={handleClickDownloadCsv}
      icon={!isLoading ? <PiDownloadSimpleBold /> : <PiSpinner />}
      tooltip={t('stylePresets.exportPromptTemplates')}
      aria-label={t('stylePresets.exportPromptTemplates')}
      sx={isLoading ? loadingStyles : undefined}
      isDisabled={isLoading || presetCount === 0}
    />
  );
};