1
1
import { Button , Modal , ModalSize , useModal } from '@openfun/cunningham-react' ;
2
+ import { Fragment , useMemo } from 'react' ;
2
3
import { useTranslation } from 'react-i18next' ;
3
4
import { createGlobalStyle } from 'styled-components' ;
4
5
5
6
import { Box , Text } from '@/components' ;
6
7
import { useCunninghamTheme } from '@/cunningham' ;
7
8
8
- import { Access , useDoc } from '../../doc-management' ;
9
+ import { Access , useDoc , useDocStore } from '../../doc-management' ;
9
10
import SimpleFileIcon from '../../docs-grid/assets/simple-document.svg' ;
10
11
11
12
import { DocShareMemberItem } from './DocShareMemberItem' ;
@@ -19,15 +20,48 @@ const ShareModalStyle = createGlobalStyle`
19
20
` ;
20
21
21
22
type Props = {
22
- accesses : Map < string , Access [ ] > ;
23
+ rawAccesses : Access [ ] ;
23
24
} ;
24
25
25
- export const DocInheritedShareContent = ( { accesses } : Props ) => {
26
+ export const DocInheritedShareContent = ( { rawAccesses } : Props ) => {
26
27
const { t } = useTranslation ( ) ;
27
28
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 ] ) ;
28
62
29
63
// Check if accesses map is empty
30
- const hasAccesses = accesses . size > 0 ;
64
+ const hasAccesses = rawAccesses . length > 0 ;
31
65
32
66
if ( ! hasAccesses ) {
33
67
return null ;
@@ -47,13 +81,13 @@ export const DocInheritedShareContent = ({ accesses }: Props) => {
47
81
{ t ( 'Inherited share' ) }
48
82
</ Text >
49
83
50
- { Array . from ( accesses . keys ( ) ) . map ( ( documentId ) => (
84
+ { inheritedData && (
51
85
< 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 ?? '' }
55
89
/>
56
- ) ) }
90
+ ) }
57
91
</ Box >
58
92
</ Box >
59
93
) ;
@@ -69,10 +103,11 @@ export const DocInheritedShareContentItem = ({
69
103
} : DocInheritedShareContentItemProps ) => {
70
104
const { t } = useTranslation ( ) ;
71
105
const { spacingsTokens } = useCunninghamTheme ( ) ;
72
- const { data : doc } = useDoc ( { id : document_id } ) ;
106
+ const { data : doc , isLoading } = useDoc ( { id : document_id } ) ;
107
+
73
108
const accessModal = useModal ( ) ;
74
109
75
- if ( ! doc ) {
110
+ if ( ! doc && ! isLoading ) {
76
111
return null ;
77
112
}
78
113
@@ -83,24 +118,36 @@ export const DocInheritedShareContentItem = ({
83
118
$width = "100%"
84
119
$direction = "row"
85
120
$align = "center"
121
+ $margin = { { bottom : spacingsTokens . sm } }
86
122
$justify = "space-between"
87
123
>
88
124
< Box $direction = "row" $align = "center" $gap = { spacingsTokens . sm } >
89
125
< SimpleFileIcon />
90
126
< 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
+ ) }
97
142
</ Box >
98
143
</ 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
+ ) }
102
149
</ Box >
103
- { accessModal . isOpen && (
150
+ { doc && accessModal . isOpen && (
104
151
< Modal
105
152
isOpen
106
153
closeOnClickOutside
@@ -117,12 +164,9 @@ export const DocInheritedShareContentItem = ({
117
164
< ShareModalStyle />
118
165
< Box $padding = { { top : spacingsTokens . sm } } >
119
166
{ 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 >
126
170
) ) }
127
171
</ Box >
128
172
</ Modal >
0 commit comments