import axios from "axios"; import { useState } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Label } from "../ui/label"; import { Maximize2, Minimize2, Trash2Icon } from "lucide-react"; import { ActionType } from "@prisma/client"; const actionTypes = [ { "name": "Overwrite local file content", "value": ActionType.WRITE_TO_FILE }, { "name": "Append to local file", "value": ActionType.APPEND_TO_FILE }, { "name": "Cause a transformation on OBS scene item", "value": ActionType.OBS_TRANSFORM }, { "name": "Play an audio file locally", "value": ActionType.AUDIO_FILE }, ] interface RedeemableAction { name: string type: string | undefined data: { [key: string]: string } edit?: boolean showEdit?: boolean isNew: boolean obsTransformations: { label: string, placeholder: string, description: string }[] adder: (name: string, type: ActionType, data: { [key: string]: string }) => void remover: (action: { name: string, type: string, data: any }) => void } const RedemptionAction = ({ name, type, data, edit, showEdit = true, isNew = false, obsTransformations = [], adder, remover }: RedeemableAction) => { const [open, setOpen] = useState(false) const [actionName, setActionName] = useState(name) const [actionType, setActionType] = useState<{ name: string, value: ActionType } | undefined>(actionTypes.find(a => a.value == type?.toUpperCase())) const [actionData, setActionData] = useState<{ [key: string]: string }>(data) const [isEditable, setIsEditable] = useState(edit) const [isMinimized, setIsMinimized] = useState(!isNew) const [oldData, setOldData] = useState<{ n: string, t: ActionType | undefined, d: { [k: string]: string } } | undefined>(undefined) function Save(name: string, type: ActionType | undefined, data: { [key: string]: string }, isNew: boolean) { // TODO: validation if (!name) { return } if (!type) { return } if (!data) { return } let info: any = { name, type } info = { ...info, ...data } if (isNew) { axios.post("/api/settings/redemptions/actions", info) .then(d => { adder(name, type, data) setActionName("") setActionType(undefined) setActionData({}) }) } else { axios.put("/api/settings/redemptions/actions", info) .then(d => { setIsEditable(false) }) } } function Cancel(data: { n: string, t: ActionType | undefined, d: { [k: string]: string } } | undefined) { if (!data) return setActionName(data.n) setActionType(actionTypes.find(a => a.value == data.t)) setActionData(data.d) setIsEditable(false) setOldData(undefined) } function Delete() { axios.delete("/api/settings/redemptions/actions?action_name=" + name) .then(d => { remover(d.data) }) } return (
{isMinimized &&
|| !isMinimized &&
setActionName(e.target.value)} value={actionName} readOnly={!isNew} /> {!isEditable && || isEditable && No action found. {actionTypes.map((action) => ( { setActionType(actionTypes.find(v => v.name.toLowerCase() == value.toLowerCase())) setOpen(false) }}> {action.name} ))} }
{actionType && (actionType.value == ActionType.WRITE_TO_FILE || actionType.value == ActionType.APPEND_TO_FILE) &&
setActionData({ ...actionData, "file_path": e.target.value })} readOnly={!isEditable} /> setActionData({ ...actionData, "file_content": e.target.value })} readOnly={!isEditable} />
} {actionType && actionType.value == ActionType.OBS_TRANSFORM &&
{obsTransformations.map(t =>
{ let c = { ...actionData } c[t.label] = e.target.value setActionData(c) }} readOnly={!isEditable} />
)}
} {actionType && actionType.value == ActionType.AUDIO_FILE &&
setActionData({ ...actionData, "file_path": e.target.value })} readOnly={!isEditable} />
}
{isEditable && } {isEditable && !isNew && } {showEdit && !isEditable && } {!isEditable && } {!isNew && }
}
); } export default RedemptionAction;