|
| 1 | +import { filterNonNullItems } from '../helper'; |
| 2 | +import { useSuspendedBackendaiClient } from '../hooks'; |
| 3 | +import { useCurrentUserInfo } from '../hooks/backendai'; |
| 4 | +import { useTanMutation } from '../hooks/reactQueryAlias'; |
| 5 | +import { usePainKiller } from '../hooks/usePainKiller'; |
| 6 | +import UserUnionIcon from './BAIIcons/UserUnionIcon'; |
| 7 | +import BAIModal, { BAIModalProps } from './BAIModal'; |
| 8 | +import BAITable from './BAITable'; |
| 9 | +import Flex from './Flex'; |
| 10 | +import VFolderPermissionCell from './VFolderPermissionCell'; |
| 11 | +import { |
| 12 | + SharedFolderPermissionInfoModalFragment$data, |
| 13 | + SharedFolderPermissionInfoModalFragment$key, |
| 14 | +} from './__generated__/SharedFolderPermissionInfoModalFragment.graphql'; |
| 15 | +import { UserOutlined } from '@ant-design/icons'; |
| 16 | +import { |
| 17 | + Alert, |
| 18 | + App, |
| 19 | + Button, |
| 20 | + Descriptions, |
| 21 | + Popconfirm, |
| 22 | + Tooltip, |
| 23 | + Typography, |
| 24 | + theme, |
| 25 | +} from 'antd'; |
| 26 | +import graphql from 'babel-plugin-relay/macro'; |
| 27 | +import { LogOut } from 'lucide-react'; |
| 28 | +import { useTranslation } from 'react-i18next'; |
| 29 | +import { useFragment } from 'react-relay'; |
| 30 | + |
| 31 | +interface SharedFolderPermissionInfoModalProps extends BAIModalProps { |
| 32 | + vfolderFrgmt: SharedFolderPermissionInfoModalFragment$key | null; |
| 33 | + onRequestClose: (success?: boolean) => void; |
| 34 | +} |
| 35 | + |
| 36 | +type VFolder = NonNullable<SharedFolderPermissionInfoModalFragment$data>; |
| 37 | + |
| 38 | +const SharedFolderPermissionInfoModal: React.FC< |
| 39 | + SharedFolderPermissionInfoModalProps |
| 40 | +> = ({ vfolderFrgmt, onRequestClose, ...modalProps }) => { |
| 41 | + const { t } = useTranslation(); |
| 42 | + const { token } = theme.useToken(); |
| 43 | + const { message } = App.useApp(); |
| 44 | + const [currentUser] = useCurrentUserInfo(); |
| 45 | + const baiClient = useSuspendedBackendaiClient(); |
| 46 | + const painKiller = usePainKiller(); |
| 47 | + |
| 48 | + const vfolder = useFragment( |
| 49 | + graphql` |
| 50 | + fragment SharedFolderPermissionInfoModalFragment on VirtualFolderNode { |
| 51 | + id |
| 52 | + name |
| 53 | + creator |
| 54 | + ownership_type |
| 55 | + user_email |
| 56 | + permission |
| 57 | +
|
| 58 | + ...VFolderPermissionCellFragment |
| 59 | + } |
| 60 | + `, |
| 61 | + vfolderFrgmt, |
| 62 | + ); |
| 63 | + |
| 64 | + const leaveFolder = useTanMutation({ |
| 65 | + mutationFn: ({ folderName }: { folderName: string }) => { |
| 66 | + return baiClient.vfolder.leave_invited(folderName); |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + return ( |
| 71 | + <BAIModal |
| 72 | + title={t('data.SharedFolderPermission')} |
| 73 | + onCancel={() => onRequestClose()} |
| 74 | + footer={null} |
| 75 | + {...modalProps} |
| 76 | + > |
| 77 | + <Flex direction="column" align="stretch" gap="lg"> |
| 78 | + <Alert |
| 79 | + showIcon |
| 80 | + type="info" |
| 81 | + message={ |
| 82 | + vfolder?.ownership_type === 'user' |
| 83 | + ? t('data.folders.SharedFolderAlertDesc') |
| 84 | + : t('data.folders.ProjectFolderAlertDesc') |
| 85 | + } |
| 86 | + /> |
| 87 | + <Descriptions column={2} bordered title={t('data.FolderInfo')}> |
| 88 | + <Descriptions.Item label="Name"> |
| 89 | + <Typography.Text copyable>{vfolder?.name}</Typography.Text> |
| 90 | + </Descriptions.Item> |
| 91 | + <Descriptions.Item label="Type"> |
| 92 | + {vfolder?.ownership_type === 'user' ? ( |
| 93 | + <Flex gap={'xs'}> |
| 94 | + <Typography.Text>{t('data.User')}</Typography.Text> |
| 95 | + <UserOutlined style={{ color: token.colorTextTertiary }} /> |
| 96 | + </Flex> |
| 97 | + ) : ( |
| 98 | + <Flex gap={'xs'}> |
| 99 | + <Typography.Text>{t('data.Project')}</Typography.Text> |
| 100 | + <UserUnionIcon style={{ color: token.colorTextTertiary }} /> |
| 101 | + </Flex> |
| 102 | + )} |
| 103 | + </Descriptions.Item> |
| 104 | + <Descriptions.Item label="Owner"> |
| 105 | + {vfolder?.creator || vfolder?.user_email} |
| 106 | + </Descriptions.Item> |
| 107 | + </Descriptions> |
| 108 | + |
| 109 | + {vfolder?.ownership_type === 'user' ? ( |
| 110 | + <Flex direction="column" align="stretch"> |
| 111 | + <Typography.Title |
| 112 | + level={5} |
| 113 | + style={{ marginTop: 0, marginBottom: token.marginMD }} |
| 114 | + > |
| 115 | + {t('data.folders.Permission')} |
| 116 | + </Typography.Title> |
| 117 | + <BAITable<VFolder> |
| 118 | + bordered |
| 119 | + pagination={false} |
| 120 | + dataSource={filterNonNullItems([vfolder])} |
| 121 | + columns={[ |
| 122 | + { |
| 123 | + key: 'userName', |
| 124 | + title: t('general.E-Mail'), |
| 125 | + render: () => currentUser.email, |
| 126 | + }, |
| 127 | + { |
| 128 | + key: 'permissions', |
| 129 | + title: t('data.folders.MountPermission'), |
| 130 | + render: (perm: string, vfolder) => { |
| 131 | + return <VFolderPermissionCell vfolderFrgmt={vfolder} />; |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + key: 'control', |
| 136 | + title: t('data.folders.Control'), |
| 137 | + render: (data) => ( |
| 138 | + <Flex align="stretch" justify="center"> |
| 139 | + <Popconfirm |
| 140 | + title={t('data.invitation.LeaveSharedFolderDesc', { |
| 141 | + folderName: data?.name, |
| 142 | + })} |
| 143 | + onConfirm={() => { |
| 144 | + leaveFolder.mutate( |
| 145 | + { |
| 146 | + folderName: data?.name, |
| 147 | + }, |
| 148 | + { |
| 149 | + onSuccess: () => { |
| 150 | + message.success( |
| 151 | + t( |
| 152 | + 'data.invitation.SuccessfullyLeftSharedFolder', |
| 153 | + ), |
| 154 | + ); |
| 155 | + onRequestClose(true); |
| 156 | + }, |
| 157 | + onError: (err) => { |
| 158 | + message.error( |
| 159 | + painKiller.relieve(err?.message) || |
| 160 | + t('general.ErrorOccurred'), |
| 161 | + ); |
| 162 | + onRequestClose(); |
| 163 | + }, |
| 164 | + }, |
| 165 | + ); |
| 166 | + }} |
| 167 | + > |
| 168 | + <Tooltip |
| 169 | + title={t('data.invitation.LeaveSharedFolder')} |
| 170 | + placement="right" |
| 171 | + > |
| 172 | + <Button |
| 173 | + size="small" |
| 174 | + type="text" |
| 175 | + icon={<LogOut />} |
| 176 | + style={{ |
| 177 | + color: token.colorError, |
| 178 | + background: token.colorErrorBg, |
| 179 | + }} |
| 180 | + /> |
| 181 | + </Tooltip> |
| 182 | + </Popconfirm> |
| 183 | + </Flex> |
| 184 | + ), |
| 185 | + }, |
| 186 | + ]} |
| 187 | + /> |
| 188 | + </Flex> |
| 189 | + ) : null} |
| 190 | + </Flex> |
| 191 | + </BAIModal> |
| 192 | + ); |
| 193 | +}; |
| 194 | + |
| 195 | +export default SharedFolderPermissionInfoModal; |
0 commit comments