|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { useRevalidator } from "react-router"; |
| 3 | +import { faScaleBalanced } from "@fortawesome/free-solid-svg-icons"; |
| 4 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 5 | +import { ApiAction } from "@thunderstore/ts-api-react-actions"; |
| 6 | +import { |
| 7 | + Modal, |
| 8 | + NewAlert, |
| 9 | + NewButton, |
| 10 | + NewIcon, |
| 11 | + NewTag, |
| 12 | + NewTextInput, |
| 13 | + useToast, |
| 14 | +} from "@thunderstore/cyberstorm"; |
| 15 | +import { |
| 16 | + packageListingApprove, |
| 17 | + packageListingReject, |
| 18 | + type RequestConfig, |
| 19 | +} from "@thunderstore/thunderstore-api"; |
| 20 | + |
| 21 | +import { type PackageListingStatus } from "@thunderstore/dapper/types"; |
| 22 | + |
| 23 | +export interface ReviewPackageFormProps { |
| 24 | + communityId: string; |
| 25 | + namespaceId: string; |
| 26 | + packageId: string; |
| 27 | + packageListingStatus: PackageListingStatus; |
| 28 | + config: () => RequestConfig; |
| 29 | + toast: ReturnType<typeof useToast>; |
| 30 | +} |
| 31 | + |
| 32 | +const reviewStatusColorMap = { |
| 33 | + approved: "green", |
| 34 | + rejected: "red", |
| 35 | + unreviewed: "orange", |
| 36 | +} as const; |
| 37 | + |
| 38 | +export function ReviewPackageForm({ |
| 39 | + communityId, |
| 40 | + namespaceId, |
| 41 | + packageId, |
| 42 | + packageListingStatus, |
| 43 | + config, |
| 44 | + toast, |
| 45 | +}: ReviewPackageFormProps) { |
| 46 | + const [rejectionReason, setRejectionReason] = useState( |
| 47 | + packageListingStatus?.rejection_reason ?? "" |
| 48 | + ); |
| 49 | + |
| 50 | + const [internalNotes, setInternalNotes] = useState( |
| 51 | + packageListingStatus?.internal_notes ?? "" |
| 52 | + ); |
| 53 | + |
| 54 | + const reviewStatus = packageListingStatus?.review_status ?? "unreviewed"; |
| 55 | + const reviewStatusColor = |
| 56 | + reviewStatusColorMap[reviewStatus as keyof typeof reviewStatusColorMap] ?? |
| 57 | + "orange"; |
| 58 | + |
| 59 | + const { revalidate } = useRevalidator(); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + setRejectionReason(packageListingStatus?.rejection_reason ?? ""); |
| 63 | + setInternalNotes(packageListingStatus?.internal_notes ?? ""); |
| 64 | + }, [packageListingStatus]); |
| 65 | + |
| 66 | + const rejectPackageAction = ApiAction({ |
| 67 | + endpoint: packageListingReject, |
| 68 | + onSubmitSuccess: () => { |
| 69 | + toast.addToast({ |
| 70 | + csVariant: "success", |
| 71 | + children: `Package rejected`, |
| 72 | + duration: 4000, |
| 73 | + }); |
| 74 | + revalidate(); |
| 75 | + }, |
| 76 | + onSubmitError: (error) => { |
| 77 | + toast.addToast({ |
| 78 | + csVariant: "danger", |
| 79 | + children: `Error occurred: ${error.message || "Unknown error"}`, |
| 80 | + duration: 8000, |
| 81 | + }); |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + const approvePackageAction = ApiAction({ |
| 86 | + endpoint: packageListingApprove, |
| 87 | + onSubmitSuccess: () => { |
| 88 | + toast.addToast({ |
| 89 | + csVariant: "success", |
| 90 | + children: `Package approved`, |
| 91 | + duration: 4000, |
| 92 | + }); |
| 93 | + revalidate(); |
| 94 | + }, |
| 95 | + onSubmitError: (error) => { |
| 96 | + toast.addToast({ |
| 97 | + csVariant: "danger", |
| 98 | + children: `Error occurred: ${error.message || "Unknown error"}`, |
| 99 | + duration: 8000, |
| 100 | + }); |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + return ( |
| 105 | + <Modal |
| 106 | + csSize="small" |
| 107 | + trigger={ |
| 108 | + <NewButton |
| 109 | + csSize="small" |
| 110 | + popoverTarget="reviewPackage" |
| 111 | + popoverTargetAction="show" |
| 112 | + > |
| 113 | + <NewIcon csMode="inline" noWrapper> |
| 114 | + <FontAwesomeIcon icon={faScaleBalanced} /> |
| 115 | + </NewIcon> |
| 116 | + Review Package |
| 117 | + </NewButton> |
| 118 | + } |
| 119 | + titleContent="Review Package" |
| 120 | + > |
| 121 | + <Modal.Body className="review-package__body"> |
| 122 | + <NewAlert csVariant="info"> |
| 123 | + Changes might take several minutes to show publicly! Info shown below |
| 124 | + is always up to date. |
| 125 | + </NewAlert> |
| 126 | + |
| 127 | + <div className="review-package__block"> |
| 128 | + <p className="review-package__label">Review status</p> |
| 129 | + <NewTag csVariant={reviewStatusColor} csModifiers={["dark"]}> |
| 130 | + {reviewStatus} |
| 131 | + </NewTag> |
| 132 | + </div> |
| 133 | + |
| 134 | + <div className="review-package__block"> |
| 135 | + <p className="review-package__label"> |
| 136 | + Reject reason (saved on reject) |
| 137 | + </p> |
| 138 | + <NewTextInput |
| 139 | + value={rejectionReason} |
| 140 | + onChange={(e) => setRejectionReason(e.target.value)} |
| 141 | + placeholder="Invalid submission" |
| 142 | + csSize="textarea" |
| 143 | + rootClasses="review-package__textarea" |
| 144 | + /> |
| 145 | + </div> |
| 146 | + |
| 147 | + <div className="review-package__block"> |
| 148 | + <p className="review-package__label">Internal notes</p> |
| 149 | + <NewTextInput |
| 150 | + value={internalNotes} |
| 151 | + onChange={(e) => setInternalNotes(e.target.value)} |
| 152 | + placeholder=".exe requires manual review" |
| 153 | + csSize="textarea" |
| 154 | + rootClasses="review-package__textarea" |
| 155 | + /> |
| 156 | + </div> |
| 157 | + </Modal.Body> |
| 158 | + |
| 159 | + <Modal.Footer className="modal-content__footer review-package__footer"> |
| 160 | + <NewButton |
| 161 | + csVariant="danger" |
| 162 | + onClick={() => |
| 163 | + rejectPackageAction({ |
| 164 | + config, |
| 165 | + params: { |
| 166 | + community: communityId, |
| 167 | + namespace: namespaceId, |
| 168 | + package: packageId, |
| 169 | + }, |
| 170 | + queryParams: {}, |
| 171 | + data: { |
| 172 | + rejection_reason: rejectionReason, |
| 173 | + internal_notes: internalNotes ? internalNotes : null, |
| 174 | + }, |
| 175 | + }) |
| 176 | + } |
| 177 | + > |
| 178 | + Reject |
| 179 | + </NewButton> |
| 180 | + |
| 181 | + <NewButton |
| 182 | + csVariant="success" |
| 183 | + onClick={() => |
| 184 | + approvePackageAction({ |
| 185 | + config, |
| 186 | + params: { |
| 187 | + community: communityId, |
| 188 | + namespace: namespaceId, |
| 189 | + package: packageId, |
| 190 | + }, |
| 191 | + queryParams: {}, |
| 192 | + data: { |
| 193 | + internal_notes: internalNotes ? internalNotes : null, |
| 194 | + }, |
| 195 | + }) |
| 196 | + } |
| 197 | + > |
| 198 | + Approve |
| 199 | + </NewButton> |
| 200 | + </Modal.Footer> |
| 201 | + </Modal> |
| 202 | + ); |
| 203 | +} |
0 commit comments