5
5
useMemo ,
6
6
useState ,
7
7
} from 'react' ;
8
+ import { useStateWithUrlSearchParam } from '../../../hooks' ;
8
9
9
10
export enum SidebarBodyComponentId {
10
11
AddContent = 'add-content' ,
@@ -32,29 +33,36 @@ export const isComponentInfoTab = (tab: string): tab is ComponentInfoTab => (
32
33
Object . values < string > ( COMPONENT_INFO_TABS ) . includes ( tab )
33
34
) ;
34
35
36
+ type SidebarInfoTab = ComponentInfoTab | CollectionInfoTab ;
37
+ const toSidebarInfoTab = ( tab : string ) : SidebarInfoTab | undefined => (
38
+ isComponentInfoTab ( tab ) || isCollectionInfoTab ( tab )
39
+ ? tab : undefined
40
+ ) ;
41
+
35
42
export interface SidebarComponentInfo {
36
43
type : SidebarBodyComponentId ;
37
44
id : string ;
38
- /** Additional action on Sidebar display */
39
- additionalAction ?: SidebarAdditionalActions ;
40
- /** Current tab in the sidebar */
41
- currentTab ?: CollectionInfoTab | ComponentInfoTab ;
42
45
}
43
46
44
- export enum SidebarAdditionalActions {
47
+ export enum SidebarActions {
45
48
JumpToAddCollections = 'jump-to-add-collections' ,
49
+ ManageTeam = 'manage-team' ,
50
+ None = '' ,
46
51
}
47
52
48
53
export type SidebarContextData = {
49
54
closeLibrarySidebar : ( ) => void ;
50
55
openAddContentSidebar : ( ) => void ;
51
56
openInfoSidebar : ( componentId ?: string , collectionId ?: string ) => void ;
52
57
openLibrarySidebar : ( ) => void ;
53
- openCollectionInfoSidebar : ( collectionId : string , additionalAction ?: SidebarAdditionalActions ) => void ;
54
- openComponentInfoSidebar : ( usageKey : string , additionalAction ?: SidebarAdditionalActions ) => void ;
58
+ openCollectionInfoSidebar : ( collectionId : string ) => void ;
59
+ openComponentInfoSidebar : ( usageKey : string ) => void ;
55
60
sidebarComponentInfo ?: SidebarComponentInfo ;
56
- resetSidebarAdditionalActions : ( ) => void ;
57
- setSidebarCurrentTab : ( tab : CollectionInfoTab | ComponentInfoTab ) => void ;
61
+ sidebarAction : SidebarActions ;
62
+ setSidebarAction : ( action : SidebarActions ) => void ;
63
+ resetSidebarAction : ( ) => void ;
64
+ sidebarTab : SidebarInfoTab ;
65
+ setSidebarTab : ( tab : CollectionInfoTab | ComponentInfoTab ) => void ;
58
66
} ;
59
67
60
68
/**
@@ -82,12 +90,22 @@ export const SidebarProvider = ({
82
90
initialSidebarComponentInfo ,
83
91
) ;
84
92
85
- /** Helper function to consume additional action once performed.
86
- Required to redo the action.
87
- */
88
- const resetSidebarAdditionalActions = useCallback ( ( ) => {
89
- setSidebarComponentInfo ( ( prev ) => ( prev && { ...prev , additionalAction : undefined } ) ) ;
90
- } , [ ] ) ;
93
+ const [ sidebarTab , setSidebarTab ] = useStateWithUrlSearchParam < SidebarInfoTab > (
94
+ COMPONENT_INFO_TABS . Preview ,
95
+ 'st' ,
96
+ ( value : string ) => toSidebarInfoTab ( value ) ,
97
+ ( value : SidebarInfoTab ) => value . toString ( ) ,
98
+ ) ;
99
+
100
+ const [ sidebarAction , setSidebarAction ] = useStateWithUrlSearchParam < SidebarActions > (
101
+ SidebarActions . None ,
102
+ 'sa' ,
103
+ ( value : string ) => Object . values ( SidebarActions ) . find ( ( enumValue ) => value === enumValue ) ,
104
+ ( value : SidebarActions ) => value . toString ( ) ,
105
+ ) ;
106
+ const resetSidebarAction = useCallback ( ( ) => {
107
+ setSidebarAction ( SidebarActions . None ) ;
108
+ } , [ setSidebarAction ] ) ;
91
109
92
110
const closeLibrarySidebar = useCallback ( ( ) => {
93
111
setSidebarComponentInfo ( undefined ) ;
@@ -99,25 +117,18 @@ export const SidebarProvider = ({
99
117
setSidebarComponentInfo ( { id : '' , type : SidebarBodyComponentId . Info } ) ;
100
118
} , [ ] ) ;
101
119
102
- const openComponentInfoSidebar = useCallback ( ( usageKey : string , additionalAction ?: SidebarAdditionalActions ) => {
103
- setSidebarComponentInfo ( ( prev ) => ( {
104
- ...prev ,
120
+ const openComponentInfoSidebar = useCallback ( ( usageKey : string ) => {
121
+ setSidebarComponentInfo ( {
105
122
id : usageKey ,
106
123
type : SidebarBodyComponentId . ComponentInfo ,
107
- additionalAction,
108
- } ) ) ;
124
+ } ) ;
109
125
} , [ ] ) ;
110
126
111
- const openCollectionInfoSidebar = useCallback ( (
112
- newCollectionId : string ,
113
- additionalAction ?: SidebarAdditionalActions ,
114
- ) => {
115
- setSidebarComponentInfo ( ( prev ) => ( {
116
- ...prev ,
127
+ const openCollectionInfoSidebar = useCallback ( ( newCollectionId : string ) => {
128
+ setSidebarComponentInfo ( {
117
129
id : newCollectionId ,
118
130
type : SidebarBodyComponentId . CollectionInfo ,
119
- additionalAction,
120
- } ) ) ;
131
+ } ) ;
121
132
} , [ ] ) ;
122
133
123
134
const openInfoSidebar = useCallback ( ( componentId ?: string , collectionId ?: string ) => {
@@ -130,10 +141,6 @@ export const SidebarProvider = ({
130
141
}
131
142
} , [ ] ) ;
132
143
133
- const setSidebarCurrentTab = useCallback ( ( tab : CollectionInfoTab | ComponentInfoTab ) => {
134
- setSidebarComponentInfo ( ( prev ) => ( prev && { ...prev , currentTab : tab } ) ) ;
135
- } , [ ] ) ;
136
-
137
144
const context = useMemo < SidebarContextData > ( ( ) => {
138
145
const contextValue = {
139
146
closeLibrarySidebar,
@@ -143,8 +150,11 @@ export const SidebarProvider = ({
143
150
openComponentInfoSidebar,
144
151
sidebarComponentInfo,
145
152
openCollectionInfoSidebar,
146
- resetSidebarAdditionalActions,
147
- setSidebarCurrentTab,
153
+ sidebarAction,
154
+ setSidebarAction,
155
+ resetSidebarAction,
156
+ sidebarTab,
157
+ setSidebarTab,
148
158
} ;
149
159
150
160
return contextValue ;
@@ -156,8 +166,11 @@ export const SidebarProvider = ({
156
166
openComponentInfoSidebar ,
157
167
sidebarComponentInfo ,
158
168
openCollectionInfoSidebar ,
159
- resetSidebarAdditionalActions ,
160
- setSidebarCurrentTab ,
169
+ sidebarAction ,
170
+ setSidebarAction ,
171
+ resetSidebarAction ,
172
+ sidebarTab ,
173
+ setSidebarTab ,
161
174
] ) ;
162
175
163
176
return (
@@ -178,8 +191,11 @@ export function useSidebarContext(): SidebarContextData {
178
191
openLibrarySidebar : ( ) => { } ,
179
192
openComponentInfoSidebar : ( ) => { } ,
180
193
openCollectionInfoSidebar : ( ) => { } ,
181
- resetSidebarAdditionalActions : ( ) => { } ,
182
- setSidebarCurrentTab : ( ) => { } ,
194
+ sidebarAction : SidebarActions . None ,
195
+ setSidebarAction : ( ) => { } ,
196
+ resetSidebarAction : ( ) => { } ,
197
+ sidebarTab : COMPONENT_INFO_TABS . Preview ,
198
+ setSidebarTab : ( ) => { } ,
183
199
sidebarComponentInfo : undefined ,
184
200
} ;
185
201
}
0 commit comments