|
| 1 | +import {useEffect, useRef, useState} from 'react'; |
| 2 | +import {Format} from 'common/types'; |
| 3 | +import useEditorOptions from 'hooks/editor/use-editor-options'; |
| 4 | +import useEditorWindowState from 'hooks/editor/use-editor-window-state'; |
| 5 | +import OptionsContainer from '../options-container'; |
| 6 | +import VideoTimeContainer from '../video-time-container'; |
| 7 | + |
| 8 | +const estimateDelay = 500; |
| 9 | + |
| 10 | +const GifSizeEstimate = () => { |
| 11 | + const {format, width, height, fps} = OptionsContainer.useContainer(); |
| 12 | + const {startTime, endTime} = VideoTimeContainer.useContainer(); |
| 13 | + const {filePath} = useEditorWindowState(); |
| 14 | + const {estimateGifSize} = useEditorOptions(); |
| 15 | + |
| 16 | + const [size, setSize] = useState<string>(); |
| 17 | + const [isEstimating, setIsEstimating] = useState(false); |
| 18 | + const requestId = useRef(0); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + const canEstimate = ( |
| 22 | + format === Format.gif && |
| 23 | + filePath && |
| 24 | + width && |
| 25 | + height && |
| 26 | + fps && |
| 27 | + endTime > startTime && |
| 28 | + estimateGifSize |
| 29 | + ); |
| 30 | + |
| 31 | + if (!canEstimate) { |
| 32 | + requestId.current++; |
| 33 | + setSize(undefined); |
| 34 | + setIsEstimating(false); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const id = ++requestId.current; |
| 39 | + setSize(undefined); |
| 40 | + setIsEstimating(true); |
| 41 | + |
| 42 | + const timer = window.setTimeout(() => { |
| 43 | + estimateGifSize({ |
| 44 | + filePath, |
| 45 | + conversionOptions: { |
| 46 | + width, |
| 47 | + height, |
| 48 | + startTime, |
| 49 | + endTime, |
| 50 | + fps, |
| 51 | + shouldCrop: true, |
| 52 | + shouldMute: true |
| 53 | + } |
| 54 | + }).then(estimatedSize => { |
| 55 | + if (id === requestId.current) { |
| 56 | + setSize(estimatedSize); |
| 57 | + } |
| 58 | + }).catch(() => { |
| 59 | + if (id === requestId.current) { |
| 60 | + setSize(undefined); |
| 61 | + } |
| 62 | + }).finally(() => { |
| 63 | + if (id === requestId.current) { |
| 64 | + setIsEstimating(false); |
| 65 | + } |
| 66 | + }); |
| 67 | + }, estimateDelay); |
| 68 | + |
| 69 | + return () => { |
| 70 | + window.clearTimeout(timer); |
| 71 | + }; |
| 72 | + }, [endTime, estimateGifSize, filePath, format, fps, height, startTime, width]); |
| 73 | + |
| 74 | + if (format !== Format.gif) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + const label = size ? `GIF ~${size}` : (isEstimating ? 'GIF ...' : 'GIF N/A'); |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="gif-size-estimate" title="Estimated GIF size"> |
| 82 | + {label} |
| 83 | + <style jsx>{` |
| 84 | + .gif-size-estimate { |
| 85 | + color: #aaaaaa; |
| 86 | + flex-shrink: 0; |
| 87 | + font-size: 12px; |
| 88 | + line-height: 24px; |
| 89 | + margin-right: 8px; |
| 90 | + min-width: 80px; |
| 91 | + overflow: hidden; |
| 92 | + text-align: right; |
| 93 | + text-overflow: ellipsis; |
| 94 | + white-space: nowrap; |
| 95 | + } |
| 96 | + `}</style> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +export default GifSizeEstimate; |
0 commit comments