File size: 2,213 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 (error) {
      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
      onClick={handleClickDownloadCsv}
      icon={!isLoading ? <PiDownloadSimpleBold /> : <PiSpinner />}
      tooltip={t('stylePresets.exportPromptTemplates')}
      aria-label={t('stylePresets.exportPromptTemplates')}
      size="md"
      variant="link"
      w={8}
      h={8}
      sx={isLoading ? loadingStyles : undefined}
      isDisabled={isLoading || presetCount === 0}
    />
  );
};