11import { Button , Modal , ModalSize , useModal } from '@openfun/cunningham-react' ;
2+ import { Fragment , useMemo } from 'react' ;
23import { useTranslation } from 'react-i18next' ;
34import { createGlobalStyle } from 'styled-components' ;
45
56import { Box , Text } from '@/components' ;
67import { useCunninghamTheme } from '@/cunningham' ;
78
8- import { Access , useDoc } from '../../doc-management' ;
9+ import { Access , useDoc , useDocStore } from '../../doc-management' ;
910import SimpleFileIcon from '../../docs-grid/assets/simple-document.svg' ;
1011
1112import { DocShareMemberItem } from './DocShareMemberItem' ;
@@ -19,15 +20,48 @@ const ShareModalStyle = createGlobalStyle`
1920` ;
2021
2122type Props = {
22- accesses : Map < string , Access [ ] > ;
23+ rawAccesses : Access [ ] ;
2324} ;
2425
25- export const DocInheritedShareContent = ( { accesses } : Props ) => {
26+ export const DocInheritedShareContent = ( { rawAccesses } : Props ) => {
2627 const { t } = useTranslation ( ) ;
2728 const { spacingsTokens } = useCunninghamTheme ( ) ;
29+ const { currentDoc } = useDocStore ( ) ;
30+
31+ const inheritedData = useMemo ( ( ) => {
32+ if ( ! currentDoc || rawAccesses . length === 0 ) {
33+ return null ;
34+ }
35+
36+ let parentId = null ;
37+ let parentPathLength = 0 ;
38+ const members : Access [ ] = [ ] ;
39+
40+ // Find the parent document with the longest path that is different from currentDoc
41+ for ( const access of rawAccesses ) {
42+ const docPath = access . document . path ;
43+
44+ // Skip if it's the current document
45+ if ( access . document . id === currentDoc . id ) {
46+ continue ;
47+ }
48+
49+ if ( ! members . some ( ( member ) => member . user . id === access . user . id ) ) {
50+ members . push ( access ) ;
51+ }
52+
53+ // Check if this document has a longer path than our current candidate
54+ if ( docPath && ( ! parentId || docPath . length > parentPathLength ) ) {
55+ parentId = access . document . id ;
56+ parentPathLength = docPath . length ;
57+ }
58+ }
59+
60+ return { parentId, members } ;
61+ } , [ currentDoc , rawAccesses ] ) ;
2862
2963 // Check if accesses map is empty
30- const hasAccesses = accesses . size > 0 ;
64+ const hasAccesses = rawAccesses . length > 0 ;
3165
3266 if ( ! hasAccesses ) {
3367 return null ;
@@ -47,13 +81,13 @@ export const DocInheritedShareContent = ({ accesses }: Props) => {
4781 { t ( 'Inherited share' ) }
4882 </ Text >
4983
50- { Array . from ( accesses . keys ( ) ) . map ( ( documentId ) => (
84+ { inheritedData && (
5185 < DocInheritedShareContentItem
52- key = { documentId }
53- accesses = { accesses . get ( documentId ) ?? [ ] }
54- document_id = { documentId }
86+ key = { inheritedData ?. parentId }
87+ accesses = { inheritedData ?. members ?? [ ] }
88+ document_id = { inheritedData ?. parentId ?? '' }
5589 />
56- ) ) }
90+ ) }
5791 </ Box >
5892 </ Box >
5993 ) ;
@@ -69,10 +103,11 @@ export const DocInheritedShareContentItem = ({
69103} : DocInheritedShareContentItemProps ) => {
70104 const { t } = useTranslation ( ) ;
71105 const { spacingsTokens } = useCunninghamTheme ( ) ;
72- const { data : doc } = useDoc ( { id : document_id } ) ;
106+ const { data : doc , isLoading } = useDoc ( { id : document_id } ) ;
107+
73108 const accessModal = useModal ( ) ;
74109
75- if ( ! doc ) {
110+ if ( ! doc && ! isLoading ) {
76111 return null ;
77112 }
78113
@@ -83,24 +118,36 @@ export const DocInheritedShareContentItem = ({
83118 $width = "100%"
84119 $direction = "row"
85120 $align = "center"
121+ $margin = { { bottom : spacingsTokens . sm } }
86122 $justify = "space-between"
87123 >
88124 < Box $direction = "row" $align = "center" $gap = { spacingsTokens . sm } >
89125 < SimpleFileIcon />
90126 < Box >
91- < Text $variation = "1000" $weight = "bold" $size = "sm" >
92- { doc . title ?? t ( 'Untitled document' ) }
93- </ Text >
94- < Text $variation = "600" $weight = "400" $size = "xs" >
95- { t ( 'Members of this page have access' ) }
96- </ Text >
127+ { isLoading ? (
128+ < Box $direction = "column" $gap = "2px" >
129+ < Box className = "skeleton" $width = "150px" $height = "20px" />
130+ < Box className = "skeleton" $width = "200px" $height = "17px" />
131+ </ Box >
132+ ) : (
133+ < >
134+ < Text $variation = "1000" $weight = "bold" $size = "sm" >
135+ { doc ?. title ?? t ( 'Untitled document' ) }
136+ </ Text >
137+ < Text $variation = "600" $weight = "400" $size = "xs" >
138+ { t ( 'Members of this page have access' ) }
139+ </ Text >
140+ </ >
141+ ) }
97142 </ Box >
98143 </ Box >
99- < Button color = "primary-text" size = "small" onClick = { accessModal . open } >
100- { t ( 'See access' ) }
101- </ Button >
144+ { ! isLoading && (
145+ < Button color = "primary-text" size = "small" onClick = { accessModal . open } >
146+ { t ( 'See access' ) }
147+ </ Button >
148+ ) }
102149 </ Box >
103- { accessModal . isOpen && (
150+ { doc && accessModal . isOpen && (
104151 < Modal
105152 isOpen
106153 closeOnClickOutside
@@ -117,12 +164,9 @@ export const DocInheritedShareContentItem = ({
117164 < ShareModalStyle />
118165 < Box $padding = { { top : spacingsTokens . sm } } >
119166 { accesses . map ( ( access ) => (
120- < DocShareMemberItem
121- key = { access . id }
122- doc = { doc }
123- access = { access }
124- isInherited
125- />
167+ < Fragment key = { access . id } >
168+ < DocShareMemberItem doc = { doc } access = { access } isInherited />
169+ </ Fragment >
126170 ) ) }
127171 </ Box >
128172 </ Modal >
0 commit comments