|
1 | | -import React from 'react'; |
2 | | -import { Outlet, useParams } from 'react-router-dom'; |
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { Outlet, useNavigate, useParams } from 'react-router-dom'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
3 | 4 |
|
4 | | -import { ContentLayout, DetailsHeader } from 'components'; |
| 5 | +import { Button, ContentLayout, DetailsHeader } from 'components'; |
| 6 | + |
| 7 | +import { useAppSelector, useNotifications } from 'hooks'; |
| 8 | +import { selectUserData } from 'App/slice'; |
| 9 | +import { ROUTES } from 'routes'; |
| 10 | +import { useGetProjectQuery, useAddProjectMemberMutation, useRemoveProjectMemberMutation } from 'services/project'; |
| 11 | +import { getProjectRoleByUserName } from '../utils'; |
5 | 12 |
|
6 | 13 | export const ProjectDetails: React.FC = () => { |
| 14 | + const { t } = useTranslation(); |
7 | 15 | const params = useParams(); |
| 16 | + const navigate = useNavigate(); |
8 | 17 | const paramProjectName = params.projectName ?? ''; |
| 18 | + const [pushNotification] = useNotifications(); |
| 19 | + const userData = useAppSelector(selectUserData); |
| 20 | + |
| 21 | + const { data: project } = useGetProjectQuery({ name: paramProjectName }); |
| 22 | + const [addMember, { isLoading: isAdding }] = useAddProjectMemberMutation(); |
| 23 | + const [removeMember, { isLoading: isRemoving }] = useRemoveProjectMemberMutation(); |
| 24 | + |
| 25 | + const currentUserRole = useMemo(() => { |
| 26 | + if (!userData?.username || !project) return null; |
| 27 | + return getProjectRoleByUserName(project, userData.username); |
| 28 | + }, [project, userData?.username]); |
| 29 | + |
| 30 | + const isProjectOwner = useMemo(() => { |
| 31 | + return userData?.username === project?.owner.username; |
| 32 | + }, [userData?.username, project?.owner.username]); |
| 33 | + |
| 34 | + const isMember = currentUserRole !== null; |
| 35 | + const isMemberActionLoading = isAdding || isRemoving; |
| 36 | + |
| 37 | + const handleJoinProject = async () => { |
| 38 | + if (!userData?.username || !project) return; |
| 39 | + |
| 40 | + try { |
| 41 | + await addMember({ |
| 42 | + project_name: project.project_name, |
| 43 | + username: userData.username, |
| 44 | + project_role: 'user', |
| 45 | + }).unwrap(); |
| 46 | + |
| 47 | + pushNotification({ |
| 48 | + type: 'success', |
| 49 | + content: t('projects.join_success'), |
| 50 | + }); |
| 51 | + } catch (error) { |
| 52 | + console.error('Failed to join project:', error); |
| 53 | + pushNotification({ |
| 54 | + type: 'error', |
| 55 | + content: t('projects.join_error'), |
| 56 | + }); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const handleLeaveProject = async () => { |
| 61 | + if (!userData?.username || !project) return; |
| 62 | + |
| 63 | + try { |
| 64 | + await removeMember({ |
| 65 | + project_name: project.project_name, |
| 66 | + username: userData.username, |
| 67 | + }).unwrap(); |
| 68 | + |
| 69 | + pushNotification({ |
| 70 | + type: 'success', |
| 71 | + content: t('projects.leave_success'), |
| 72 | + }); |
| 73 | + |
| 74 | + // Redirect to project list after successfully leaving |
| 75 | + navigate(ROUTES.PROJECT.LIST); |
| 76 | + } catch (error) { |
| 77 | + console.error('Failed to leave project:', error); |
| 78 | + pushNotification({ |
| 79 | + type: 'error', |
| 80 | + content: t('projects.leave_error'), |
| 81 | + }); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const renderJoinLeaveButton = () => { |
| 86 | + // Only show button if user is authenticated and project is loaded |
| 87 | + if (!userData?.username || !project) return null; |
| 88 | + |
| 89 | + if (!isMember) { |
| 90 | + return ( |
| 91 | + <Button |
| 92 | + onClick={handleJoinProject} |
| 93 | + disabled={isMemberActionLoading} |
| 94 | + variant="primary" |
| 95 | + > |
| 96 | + {isMemberActionLoading ? t('common.loading') : t('projects.join')} |
| 97 | + </Button> |
| 98 | + ); |
| 99 | + } else { |
| 100 | + // Prevent owners and admins from leaving their projects |
| 101 | + const canLeave = !isProjectOwner && currentUserRole !== 'admin'; |
| 102 | + |
| 103 | + return ( |
| 104 | + <Button |
| 105 | + onClick={handleLeaveProject} |
| 106 | + disabled={isMemberActionLoading || !canLeave} |
| 107 | + variant="normal" |
| 108 | + > |
| 109 | + {!canLeave |
| 110 | + ? t('projects.owner_cannot_leave') |
| 111 | + : isMemberActionLoading |
| 112 | + ? t('common.loading') |
| 113 | + : t('projects.leave') |
| 114 | + } |
| 115 | + </Button> |
| 116 | + ); |
| 117 | + } |
| 118 | + }; |
9 | 119 |
|
10 | 120 | return ( |
11 | | - <ContentLayout header={<DetailsHeader title={paramProjectName} />}> |
| 121 | + <ContentLayout |
| 122 | + header={ |
| 123 | + <DetailsHeader |
| 124 | + title={paramProjectName} |
| 125 | + actionButtons={renderJoinLeaveButton()} |
| 126 | + /> |
| 127 | + } |
| 128 | + > |
12 | 129 | <Outlet /> |
13 | 130 | </ContentLayout> |
14 | 131 | ); |
|
0 commit comments