| 1 | import { LoadingView } from "@/components/ui/loading-view"; |
| 2 | import { UserContext } from "@/lib/current-user-context"; |
| 3 | import { AudioFile, Status } from "@/models/audio-file"; |
| 4 | import { CountdownTimerIcon, LapTimerIcon, RocketIcon } from "@radix-ui/react-icons"; |
| 5 | import { useRouter } from "next/navigation"; |
| 6 | import { useContext, useState } from "react"; |
| 7 | |
| 8 | enum State { |
| 9 | LOADING, |
| 10 | LOADED, |
| 11 | ERROR, |
| 12 | } |
| 13 | |
| 14 | export default function ButtonGenerateBackingTracks({ |
| 15 | songProviderId, |
| 16 | audioFile, |
| 17 | aggregateVotes |
| 18 | }: { |
| 19 | songProviderId: number; |
| 20 | audioFile: AudioFile | null; |
| 21 | aggregateVotes: number; |
| 22 | }) { |
| 23 | |
| 24 | const [state, setState] = useState(State.LOADED); |
| 25 | const { user } = useContext(UserContext); |
| 26 | const [goToLogin, setGoToLogin] = useState(false); |
| 27 | const router = useRouter(); |
| 28 | |
| 29 | const splitRequest = () => { |
| 30 | if (!user || !user.accessToken) { |
| 31 | setGoToLogin(true); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | setState(State.LOADING); |
| 36 | // Split request logic here. |
| 37 | |
| 38 | }; |
| 39 | |
| 40 | // Default to production value. |
| 41 | const splitTreshold = process.env.REACT_APP_SPLIT_THRESHOLD |
| 42 | ? parseInt(process.env.REACT_APP_SPLIT_THRESHOLD) |
| 43 | : 5; |
| 44 | |
| 45 | const isDoneSplitting = audioFile && audioFile.status === Status.DONE; |
| 46 | const isCurrentlySplitting = |
| 47 | audioFile && |
| 48 | (audioFile.status === Status.SPLITTING || |
| 49 | audioFile.status === Status.DOWNLOADING); |
| 50 | const isReadyToSplit = aggregateVotes > splitTreshold; |
| 51 | |
| 52 | if (goToLogin) { |
| 53 | router.push("/login"); |
| 54 | return <></>; |
| 55 | } |
| 56 | |
| 57 | if (state === State.LOADING) { |
| 58 | return ( |
| 59 | <div className="mb-3 mt-3"> |
| 60 | <LoadingView /> |
| 61 | </div> |
| 62 | ); |
| 63 | } |
| 64 | // Check if we have processed the split request. |
| 65 | if (isDoneSplitting) { |
| 66 | return ( |
| 67 | <div className="mb-3 mt-3"> |
| 68 | <h1 className="">Let's Play!</h1> |
| 69 | </div> |
| 70 | ); |
| 71 | } else if (isCurrentlySplitting) { |
| 72 | return ( |
| 73 | <div className="mb-3 mt-3" title="Split request in progress..."> |
| 74 | <LapTimerIcon |
| 75 | width={40} |
| 76 | height={40} |
| 77 | className="mb-3" |
| 78 | color="red" |
| 79 | /> |
| 80 | </div> |
| 81 | ); |
| 82 | } else if (isReadyToSplit) { |
| 83 | return ( |
| 84 | <div className="mb-3 mt-3" |
| 85 | title="Split request..."> |
| 86 | <RocketIcon |
| 87 | width={40} |
| 88 | height={40} |
| 89 | className="mb-3 cursor-pointer" |
| 90 | onClick={splitRequest} |
| 91 | color="blue" |
| 92 | /> |
| 93 | </div> |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | return ( |
| 98 | <div className="" title="Not enough votes to generate backing tracks..."> |
| 99 | <CountdownTimerIcon width={40} height={40} className="mb-3" color="red" /> |
| 100 | </div> |
| 101 | ); |
| 102 | } |