import React, { FormEvent } from 'react'
import { useRecoilValue } from 'recoil'
import { CursorArrowRaysIcon, GifIcon } from '@heroicons/react/24/outline'
import {
BoxModelIcon,
ChevronRightIcon,
FaceIcon,
HobbyKnifeIcon,
MixIcon,
} from '@radix-ui/react-icons'
import { useToggle } from 'react-use'
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
import {
fileState,
isProcessingState,
serverConfigState,
} from '../../store/Atoms'
import emitter from '../../event'
import Button from '../shared/Button'
export enum PluginName {
RemoveBG = 'RemoveBG',
RealESRGAN = 'RealESRGAN',
GFPGAN = 'GFPGAN',
RestoreFormer = 'RestoreFormer',
InteractiveSeg = 'InteractiveSeg',
MakeGIF = 'MakeGIF',
}
const pluginMap = {
[PluginName.RemoveBG]: {
IconClass: HobbyKnifeIcon,
showName: 'RemoveBG',
},
[PluginName.RealESRGAN]: {
IconClass: BoxModelIcon,
showName: 'RealESRGAN 4x',
},
[PluginName.GFPGAN]: {
IconClass: FaceIcon,
showName: 'GFPGAN',
},
[PluginName.RestoreFormer]: {
IconClass: FaceIcon,
showName: 'RestoreFormer',
},
[PluginName.InteractiveSeg]: {
IconClass: CursorArrowRaysIcon,
showName: 'Interactive Seg',
},
[PluginName.MakeGIF]: {
IconClass: GifIcon,
showName: 'Make GIF',
},
}
const Plugins = () => {
// const [open, toggleOpen] = useToggle(true)
const serverConfig = useRecoilValue(serverConfigState)
const file = useRecoilValue(fileState)
const isProcessing = useRecoilValue(isProcessingState)
const disabled = !file || isProcessing
const onPluginClick = (pluginName: string) => {
if (!disabled) {
emitter.emit(pluginName)
}
}
const onRealESRGANClick = (upscale: number) => {
if (!disabled) {
emitter.emit(PluginName.RealESRGAN, { upscale })
}
}
const renderRealESRGANPlugin = () => {
return (
RealESRGAN
onRealESRGANClick(2)}
>
upscale 2x
onRealESRGANClick(4)}
disabled={disabled}
>
upscale 4x
)
}
const renderPlugins = () => {
return serverConfig.plugins.map((plugin: string) => {
const { IconClass } = pluginMap[plugin as PluginName]
if (plugin === PluginName.RealESRGAN) {
return renderRealESRGANPlugin()
}
return (
onPluginClick(plugin)}
disabled={disabled}
>
{plugin}
)
})
}
if (serverConfig.plugins.length === 0) {
return null
}
return (
} />
event.preventDefault()}
>
{renderPlugins()}
)
}
export default Plugins