|
| 1 | +import { Button, Modal, ModalSize, useModal } from '@openfun/cunningham-react'; |
| 2 | +import { Fragment, useMemo } from 'react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { createGlobalStyle } from 'styled-components'; |
| 5 | + |
| 6 | +import { Box, StyledLink, Text } from '@/components'; |
| 7 | +import { useCunninghamTheme } from '@/cunningham'; |
| 8 | + |
| 9 | +import { |
| 10 | + Access, |
| 11 | + RoleImportance, |
| 12 | + useDoc, |
| 13 | + useDocStore, |
| 14 | +} from '../../doc-management'; |
| 15 | +import SimpleFileIcon from '../../docs-grid/assets/simple-document.svg'; |
| 16 | + |
| 17 | +import { DocShareMemberItem } from './DocShareMemberItem'; |
| 18 | +const ShareModalStyle = createGlobalStyle` |
| 19 | + .c__modal__title { |
| 20 | + padding-bottom: 0 !important; |
| 21 | + } |
| 22 | + .c__modal__scroller { |
| 23 | + padding: 15px 15px !important; |
| 24 | + } |
| 25 | +`; |
| 26 | + |
| 27 | +type Props = { |
| 28 | + rawAccesses: Access[]; |
| 29 | +}; |
| 30 | + |
| 31 | +const getMaxRoleBetweenAccesses = (access1: Access, access2: Access) => { |
| 32 | + const role1 = access1.max_role; |
| 33 | + const role2 = access2.max_role; |
| 34 | + |
| 35 | + const roleImportance1 = RoleImportance[role1]; |
| 36 | + const roleImportance2 = RoleImportance[role2]; |
| 37 | + |
| 38 | + return roleImportance1 > roleImportance2 ? role1 : role2; |
| 39 | +}; |
| 40 | + |
| 41 | +export const DocInheritedShareContent = ({ rawAccesses }: Props) => { |
| 42 | + const { t } = useTranslation(); |
| 43 | + const { spacingsTokens } = useCunninghamTheme(); |
| 44 | + const { currentDoc } = useDocStore(); |
| 45 | + |
| 46 | + const inheritedData = useMemo(() => { |
| 47 | + if (!currentDoc || rawAccesses.length === 0) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + let parentId = null; |
| 52 | + let parentPathLength = 0; |
| 53 | + const members: Access[] = []; |
| 54 | + |
| 55 | + // Find the parent document with the longest path that is different from currentDoc |
| 56 | + for (const access of rawAccesses) { |
| 57 | + const docPath = access.document.path; |
| 58 | + |
| 59 | + // Skip if it's the current document |
| 60 | + if (access.document.id === currentDoc.id) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const findIndex = members.findIndex( |
| 65 | + (member) => member.user.id === access.user.id, |
| 66 | + ); |
| 67 | + if (findIndex === -1) { |
| 68 | + members.push(access); |
| 69 | + } else { |
| 70 | + const accessToUpdate = members[findIndex]; |
| 71 | + const currentRole = accessToUpdate.max_role; |
| 72 | + const maxRole = getMaxRoleBetweenAccesses(accessToUpdate, access); |
| 73 | + |
| 74 | + if (maxRole !== currentRole) { |
| 75 | + members[findIndex] = access; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Check if this document has a longer path than our current candidate |
| 80 | + if (docPath && (!parentId || docPath.length > parentPathLength)) { |
| 81 | + parentId = access.document.id; |
| 82 | + parentPathLength = docPath.length; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return { parentId, members }; |
| 87 | + }, [currentDoc, rawAccesses]); |
| 88 | + |
| 89 | + // Check if accesses map is empty |
| 90 | + const hasAccesses = rawAccesses.length > 0; |
| 91 | + |
| 92 | + if (!hasAccesses) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <Box $gap={spacingsTokens.sm}> |
| 98 | + <Box |
| 99 | + $gap={spacingsTokens.sm} |
| 100 | + $padding={{ |
| 101 | + horizontal: spacingsTokens.base, |
| 102 | + vertical: spacingsTokens.sm, |
| 103 | + bottom: '0px', |
| 104 | + }} |
| 105 | + > |
| 106 | + <Text $variation="1000" $weight="bold" $size="sm"> |
| 107 | + {t('Inherited share')} |
| 108 | + </Text> |
| 109 | + |
| 110 | + {inheritedData && ( |
| 111 | + <DocInheritedShareContentItem |
| 112 | + key={inheritedData?.parentId} |
| 113 | + accesses={inheritedData?.members ?? []} |
| 114 | + document_id={inheritedData?.parentId ?? ''} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </Box> |
| 118 | + </Box> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +type DocInheritedShareContentItemProps = { |
| 123 | + accesses: Access[]; |
| 124 | + document_id: string; |
| 125 | +}; |
| 126 | +export const DocInheritedShareContentItem = ({ |
| 127 | + accesses, |
| 128 | + document_id, |
| 129 | +}: DocInheritedShareContentItemProps) => { |
| 130 | + const { t } = useTranslation(); |
| 131 | + const { spacingsTokens } = useCunninghamTheme(); |
| 132 | + const { data: doc, error, isLoading } = useDoc({ id: document_id }); |
| 133 | + const errorCode = error?.status; |
| 134 | + |
| 135 | + const accessModal = useModal(); |
| 136 | + if ((!doc && !isLoading && !error) || (error && errorCode !== 403)) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + return ( |
| 141 | + <> |
| 142 | + <Box |
| 143 | + $gap={spacingsTokens.sm} |
| 144 | + $width="100%" |
| 145 | + $direction="row" |
| 146 | + $align="center" |
| 147 | + $margin={{ bottom: spacingsTokens.sm }} |
| 148 | + $justify="space-between" |
| 149 | + > |
| 150 | + <Box $direction="row" $align="center" $gap={spacingsTokens.sm}> |
| 151 | + <SimpleFileIcon /> |
| 152 | + <Box> |
| 153 | + {isLoading ? ( |
| 154 | + <Box $direction="column" $gap="2px"> |
| 155 | + <Box className="skeleton" $width="150px" $height="20px" /> |
| 156 | + <Box className="skeleton" $width="200px" $height="17px" /> |
| 157 | + </Box> |
| 158 | + ) : ( |
| 159 | + <> |
| 160 | + <StyledLink href={`/docs/${doc?.id}`}> |
| 161 | + <Text $variation="1000" $weight="bold" $size="sm"> |
| 162 | + {error && errorCode === 403 |
| 163 | + ? t('You do not have permission to view this document') |
| 164 | + : (doc?.title ?? t('Untitled document'))} |
| 165 | + </Text> |
| 166 | + </StyledLink> |
| 167 | + <Text $variation="600" $weight="400" $size="xs"> |
| 168 | + {t('Members of this page have access')} |
| 169 | + </Text> |
| 170 | + </> |
| 171 | + )} |
| 172 | + </Box> |
| 173 | + </Box> |
| 174 | + {!isLoading && ( |
| 175 | + <Button color="primary-text" size="small" onClick={accessModal.open}> |
| 176 | + {t('See access')} |
| 177 | + </Button> |
| 178 | + )} |
| 179 | + </Box> |
| 180 | + {accessModal.isOpen && ( |
| 181 | + <Modal |
| 182 | + isOpen |
| 183 | + closeOnClickOutside |
| 184 | + onClose={accessModal.close} |
| 185 | + title={ |
| 186 | + <Box $align="flex-start"> |
| 187 | + <Text $variation="1000" $weight="bold" $size="sm"> |
| 188 | + {t('Access inherited from the parent page')} |
| 189 | + </Text> |
| 190 | + </Box> |
| 191 | + } |
| 192 | + size={ModalSize.MEDIUM} |
| 193 | + > |
| 194 | + <ShareModalStyle /> |
| 195 | + <Box $padding={{ top: spacingsTokens.sm }}> |
| 196 | + {accesses.map((access) => ( |
| 197 | + <Fragment key={access.id}> |
| 198 | + <DocShareMemberItem doc={doc} access={access} isInherited /> |
| 199 | + </Fragment> |
| 200 | + ))} |
| 201 | + </Box> |
| 202 | + </Modal> |
| 203 | + )} |
| 204 | + </> |
| 205 | + ); |
| 206 | +}; |
0 commit comments