File size: 3,016 Bytes
74aacd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { useEffect, useState } from 'react'
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
import Button from '../shared/Button'
import { fileState, gifImageState, toastState } from '../../store/Atoms'
import { makeGif } from '../../adapters/inpainting'
import Modal from '../shared/Modal'
import { LoadingIcon } from '../shared/Toast'
import { downloadImage } from '../../utils'
import emitter from '../../event'
import { PluginName } from '../Plugins/Plugins'

interface Props {
  renders: HTMLImageElement[]
}

const MakeGIF = (props: Props) => {
  const { renders } = props
  const [gifImg, setGifImg] = useRecoilState(gifImageState)
  const file = useRecoilValue(fileState)
  const setToastState = useSetRecoilState(toastState)
  const [show, setShow] = useState(false)

  const handleOnClose = () => {
    setShow(false)
  }

  const handleDownload = () => {
    if (gifImg) {
      const name = file.name.replace(/\.[^/.]+$/, '.gif')
      downloadImage(gifImg.src, name)
    }
  }

  useEffect(() => {
    emitter.on(PluginName.MakeGIF, async () => {
      if (renders.length === 0) {
        setToastState({
          open: true,
          desc: 'No render found',
          state: 'error',
          duration: 2000,
        })
        return
      }

      setShow(true)
      setGifImg(null)
      try {
        const gif = await makeGif(
          file,
          renders[renders.length - 1],
          file.name,
          file.type
        )
        if (gif) {
          setGifImg(gif)
        }
      } catch (e: any) {
        setToastState({
          open: true,
          desc: e.message ? e.message : e.toString(),
          state: 'error',
          duration: 2000,
        })
        setShow(false)
      }
    })
    return () => {
      emitter.off(PluginName.MakeGIF)
    }
  }, [setGifImg, renders, file, setShow])

  return (
    <Modal
      onClose={handleOnClose}
      title="GIF"
      className="modal-setting"
      show={show}
    >
      <div
        style={{
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          flexDirection: 'column',
          gap: 16,
        }}
      >
        {gifImg ? (
          <img src={gifImg.src} style={{ borderRadius: 8 }} alt="gif" />
        ) : (
          <div
            style={{
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              gap: 8,
            }}
          >
            <LoadingIcon />
            Generating GIF...
          </div>
        )}

        {gifImg && (
          <div
            style={{
              display: 'flex',
              width: '100%',
              justifyContent: 'flex-end',
              alignItems: 'center',
              gap: '12px',
            }}
          >
            <Button onClick={handleDownload} border>
              Download
            </Button>
          </div>
        )}
      </div>
    </Modal>
  )
}

export default MakeGIF