import axios from "axios"; import { useEffect, 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 { HelpCircleIcon, Trash2Icon } from "lucide-react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "../ui/tooltip" interface Redemption { id: string | undefined redemptionId: string | undefined actionName: string numbering: number, edit: boolean showEdit: boolean isNew: boolean actions: string[] twitchRedemptions: { id: string, title: string }[] adder: (id: string, actionName: string, redemptionId: string, order: number) => void remover: (redemption: { id: string, redemptionId: string, actionName: string, order: number }) => void } const OBSRedemption = ({ id, redemptionId, actionName, numbering, edit, showEdit, isNew, actions, twitchRedemptions, adder, remover }: Redemption) => { const [actionOpen, setActionOpen] = useState(false) const [redemptionOpen, setRedemptionOpen] = useState(false) const [twitchRedemption, setTwitchRedemption] = useState<{ id: string, title: string } | undefined>(undefined) const [action, setAction] = useState(actionName) const [order, setOrder] = useState(numbering) const [isEditable, setIsEditable] = useState(edit) const [oldData, setOldData] = useState<{ r: { id: string, title: string } | undefined, a: string | undefined, o: number } | undefined>(undefined) useEffect(() => { setTwitchRedemption(twitchRedemptions.find(r => r.id == redemptionId)) }, []) function Save() { // TODO: validation if (!isNew && !id) return if (!action || !twitchRedemption) return if (isNew) { axios.post("/api/settings/redemptions", { actionName: action, redemptionId: twitchRedemption?.id, order: order, state: true }).then(d => { adder(d.data.id, action, twitchRedemption.id, order) setAction(undefined) setTwitchRedemption(undefined) setOrder(0) }) } else { axios.put("/api/settings/redemptions", { id: id, actionName: action, redemptionId: twitchRedemption?.id, order: order, state: true }).then(d => { setIsEditable(false) }) } } function Cancel() { if (!oldData) return setAction(oldData.a) setTwitchRedemption(oldData.r) setOrder(oldData.o) setIsEditable(false) setOldData(undefined) } function Delete() { axios.delete("/api/settings/redemptions?id=" + id) .then(d => { remover(d.data) }) } return (
{!isEditable && || isEditable && No redemption found. {twitchRedemptions.map((redemption) => ( { setTwitchRedemption(twitchRedemptions.find(v => v.title.toLowerCase() == value.toLowerCase())) setRedemptionOpen(false) }}> {redemption.title} ))} } {!isEditable && || isEditable && No action found. {actions.map((action) => ( { let a = actions.find(v => v == action) if (a) setAction(a) setActionOpen(false) }}> {action} ))} }
setOrder(e.target.value.length == 0 ? 0 : parseInt(e.target.value))} value={order} readOnly={!isEditable} />

This decides when this action will be done relative to other actions for this Twitch redemption.
Action start from lowest to highest order number.
Equal order numbers cannot be guaranteed proper order.

{isEditable && } {isEditable && !isNew && } {showEdit && !isEditable && } {!isEditable && }
); } export default OBSRedemption;