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 "@/components/ui/label"; import { HelpCircleIcon, Trash2Icon } from "lucide-react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/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) const [error, setError] = useState(undefined) useEffect(() => { setTwitchRedemption(twitchRedemptions.find(r => r.id == redemptionId)) }, []) function Save() { setError(undefined) if (!isNew && !id) { setError('Something went wrong. Refresh the page.') return } if (!action) { setError('An action must be selected.') return } if (!actions.some(a => a == action)) { setError('Ensure the action selected still exists.') return } if (!twitchRedemption) { setError('A Twitch redemption must be selected.') return } if (!twitchRedemptions.some(r => r.id == twitchRedemption.id)) { setError('Ensure the action selected still exists.') return } if (isNaN(order)) { setError('The order cannot be NaN.') setOrder(0) 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 setError(undefined) 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) => ( { console.log('tr', value, redemption.id, redemption.title) setTwitchRedemption(redemption) 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(d => { let temp = order const v = parseInt(e.target.value) if (e.target.value.length == 0 || Number.isNaN(v)) { temp = 0 } else if (!Number.isNaN(v) && Number.isSafeInteger(v)) { temp = v } return temp })} 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.

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