|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { UserMinus, LogOut, Loader2 } from "lucide-react"; |
| 6 | +import { toast } from "sonner"; |
| 7 | +import { deleteContributor, leaveProject } from "../../../../../../actions/contributors"; |
| 8 | + |
| 9 | +interface ContributorActionsProps { |
| 10 | + contributorId: string; |
| 11 | + contributorProfileId: string; |
| 12 | + contributorName: string; |
| 13 | + projectId: string; |
| 14 | + currentUserProfileId?: string; |
| 15 | + authorProfileId: string; |
| 16 | +} |
| 17 | + |
| 18 | +export function ContributorActions({ |
| 19 | + contributorId, |
| 20 | + contributorProfileId, |
| 21 | + contributorName, |
| 22 | + projectId, |
| 23 | + currentUserProfileId, |
| 24 | + authorProfileId, |
| 25 | +}: ContributorActionsProps) { |
| 26 | + const [loading, setLoading] = useState(false); |
| 27 | + const [showConfirm, setShowConfirm] = useState(false); |
| 28 | + const router = useRouter(); |
| 29 | + |
| 30 | + const isAuthor = currentUserProfileId === authorProfileId; |
| 31 | + const isCurrentUserContributor = currentUserProfileId === contributorProfileId; |
| 32 | + |
| 33 | + if (!isAuthor && !isCurrentUserContributor) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const handleRemoveContributor = async () => { |
| 38 | + if (!currentUserProfileId) return; |
| 39 | + |
| 40 | + setLoading(true); |
| 41 | + try { |
| 42 | + const result = await deleteContributor(contributorId, currentUserProfileId, projectId); |
| 43 | + |
| 44 | + if (result.success) { |
| 45 | + toast.success("Contributor removed successfully"); |
| 46 | + router.refresh(); |
| 47 | + } else { |
| 48 | + toast.error(result.message || "Failed to remove contributor"); |
| 49 | + } |
| 50 | + } catch { |
| 51 | + toast.error("An error occurred while removing the contributor"); |
| 52 | + } finally { |
| 53 | + setLoading(false); |
| 54 | + setShowConfirm(false); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + const handleLeaveProject = async () => { |
| 59 | + if (!currentUserProfileId) return; |
| 60 | + |
| 61 | + setLoading(true); |
| 62 | + try { |
| 63 | + const result = await leaveProject(contributorId, currentUserProfileId, projectId); |
| 64 | + |
| 65 | + if (result.success) { |
| 66 | + toast.success("Successfully left the project"); |
| 67 | + router.refresh(); |
| 68 | + } else { |
| 69 | + toast.error(result.message || "Failed to leave project"); |
| 70 | + } |
| 71 | + } catch { |
| 72 | + toast.error("An error occurred while leaving the project"); |
| 73 | + } finally { |
| 74 | + setLoading(false); |
| 75 | + setShowConfirm(false); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + if (showConfirm) { |
| 80 | + return ( |
| 81 | + <div className="flex items-center gap-2 ml-auto"> |
| 82 | + <span className="text-xs text-muted-foreground"> |
| 83 | + {isAuthor ? "Remove?" : "Leave?"} |
| 84 | + </span> |
| 85 | + <button |
| 86 | + onClick={isAuthor ? handleRemoveContributor : handleLeaveProject} |
| 87 | + disabled={loading} |
| 88 | + className="px-2 py-1 text-xs font-medium rounded-md bg-red-500/10 text-red-600 dark:text-red-400 hover:bg-red-500/20 transition-colors disabled:opacity-50" |
| 89 | + > |
| 90 | + {loading ? ( |
| 91 | + <Loader2 className="h-3 w-3 animate-spin" /> |
| 92 | + ) : ( |
| 93 | + "Yes" |
| 94 | + )} |
| 95 | + </button> |
| 96 | + <button |
| 97 | + onClick={() => setShowConfirm(false)} |
| 98 | + disabled={loading} |
| 99 | + className="px-2 py-1 text-xs font-medium rounded-md bg-muted text-muted-foreground hover:bg-muted/80 transition-colors disabled:opacity-50" |
| 100 | + > |
| 101 | + No |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + if (isAuthor) { |
| 108 | + return ( |
| 109 | + <button |
| 110 | + onClick={() => setShowConfirm(true)} |
| 111 | + className="ml-auto p-2 rounded-lg text-muted-foreground hover:text-red-500 hover:bg-red-500/10 transition-all group" |
| 112 | + title={`Remove ${contributorName} from project`} |
| 113 | + > |
| 114 | + <UserMinus className="h-4 w-4" /> |
| 115 | + </button> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + if (isCurrentUserContributor) { |
| 120 | + return ( |
| 121 | + <button |
| 122 | + onClick={() => setShowConfirm(true)} |
| 123 | + className="ml-auto p-2 rounded-lg text-muted-foreground hover:text-orange-500 hover:bg-orange-500/10 transition-all group" |
| 124 | + title="Leave this project" |
| 125 | + > |
| 126 | + <LogOut className="h-4 w-4" /> |
| 127 | + </button> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return null; |
| 132 | +} |
0 commit comments