-
Notifications
You must be signed in to change notification settings - Fork 2
/
BackstageComposer.tsx
216 lines (198 loc) · 7.53 KB
/
BackstageComposer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Backstage
*/
import { ConditionalBooleanValue } from "@itwin/appui-abstract";
import * as React from "react";
import { Backstage as NZ_Backstage } from "../layout/backstage/Backstage";
import { BackstageSeparator } from "../layout/backstage/Separator";
import { SafeAreaContext } from "../safearea/SafeAreaContext";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher";
import { BackstageComposerItem } from "./BackstageComposerItem";
import { isBackstageStageLauncher } from "./BackstageItem";
import { BackstageItemsManager } from "./BackstageItemsManager";
import { useBackstageManager, useIsBackstageOpen } from "./BackstageManager";
import { useDefaultBackstageItems } from "./useDefaultBackstageItems";
import { useUiItemsProviderBackstageItems } from "./useUiItemsProviderBackstageItems";
import type { CommonProps } from "@itwin/core-react";
import type { UiSyncEventArgs } from "../syncui/UiSyncEvent";
import type { BackstageItem } from "./BackstageItem";
// cSpell:ignore safearea
/** Private function to set up sync event monitoring of backstage items */
function useBackstageItemSyncEffect(
itemsManager: BackstageItemsManager,
syncIdsOfInterest: string[]
) {
const isInitialMount = React.useRef(true);
React.useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
// initialize the display state of any items with syncIds/condition defined
itemsManager.refreshAffectedItems(new Set(syncIdsOfInterest));
}
return SyncUiEventDispatcher.onSyncUiEvent.addListener(
(
args: UiSyncEventArgs // eslint-disable-line deprecation/deprecation
) => {
if (0 === syncIdsOfInterest.length) return;
// istanbul ignore else
if (
syncIdsOfInterest.some((value: string): boolean =>
args.eventIds.has(value.toLowerCase())
)
) {
// process each item that has interest
itemsManager.refreshAffectedItems(args.eventIds);
}
}
);
}, [itemsManager, itemsManager.items, syncIdsOfInterest]);
}
/** local function to combine items from Stage and from Extensions */
function combineItems(
stageItems: ReadonlyArray<BackstageItem>,
addonItems: ReadonlyArray<BackstageItem>,
hideSoloStageEntry: boolean
): ReadonlyArray<BackstageItem> {
let items: Array<BackstageItem> = [];
if (stageItems.length) {
// Walk through each and ensure no duplicate ids are added.
stageItems.forEach((srcItem) => {
if (-1 === items.findIndex((item) => item.id === srcItem.id)) {
items.push(srcItem);
}
});
}
if (addonItems.length) {
// Walk through each and ensure no duplicate ids are added.
addonItems.forEach((srcItem) => {
if (-1 === items.findIndex((item) => item.id === srcItem.id)) {
items.push(srcItem);
}
});
}
if (hideSoloStageEntry) {
// per user request don't show stage launcher if only one stage is available
const numberOfFrontstageItems = items.reduce(
(accumulator, item) =>
accumulator + (isBackstageStageLauncher(item) ? 1 : 0),
0
);
if (1 === numberOfFrontstageItems)
items = items.filter((item) => !isBackstageStageLauncher(item));
}
return items;
}
/** @internal */
export type GroupedItems = ReadonlyArray<ReadonlyArray<BackstageItem>>;
/** @internal */
export const useGroupedItems = (
items: ReadonlyArray<BackstageItem>
): GroupedItems => {
return React.useMemo(() => {
const grouped = items.reduce<GroupedItems>((acc, item) => {
if (ConditionalBooleanValue.getValue(item.isHidden)) return acc;
const groupIndex = acc.findIndex(
(group) => group[0].groupPriority === item.groupPriority
);
if (groupIndex >= 0)
return [
...acc.slice(0, groupIndex),
[...acc[groupIndex], item],
...acc.slice(groupIndex + 1),
];
return [...acc, [item]];
}, []);
const sortedGroups = grouped.reduce<GroupedItems>((acc, group) => {
const sortedGroup = [...group].sort(
(a, b) => a.itemPriority - b.itemPriority
);
return [...acc, sortedGroup];
}, []);
return [...sortedGroups].sort(
(a, b) => a[0].groupPriority - b[0].groupPriority
);
}, [items]);
};
/** Props of [[BackstageComposer]] component.
* @public
*/
export interface BackstageComposerProps extends CommonProps {
/** React node for an optional header item */
readonly header?: React.ReactNode;
/** Indicates whether to place an overlay over the frontstage when the backstage is displayed */
readonly showOverlay?: boolean;
/** List of static backstage items to show. Can be undefined in UiItemsProviders are used to populate backstage. */
readonly items?: BackstageItem[];
/** If true and only one stage launcher item is found, do not show entry in backstage */
readonly hideSoloStageEntry?: boolean;
}
/** Backstage component composed from [[UiFramework.backstage]] items.
* @public
*/
export function BackstageComposer(props: BackstageComposerProps) {
const [defaultItemsManager, setDefaultItemsManager] = React.useState(
new BackstageItemsManager(props.items)
);
const initialItems = React.useRef(props.items);
React.useEffect(() => {
if (initialItems.current !== props.items) {
initialItems.current = props.items;
setDefaultItemsManager(new BackstageItemsManager(props.items));
}
}, [props.items]);
const manager = useBackstageManager();
const isOpen = useIsBackstageOpen(manager);
const safeAreaInsets = React.useContext(SafeAreaContext);
const handleClose = React.useCallback(() => {
manager.close();
}, [manager]);
const defaultItems = useDefaultBackstageItems(defaultItemsManager);
const syncIdsOfInterest = React.useMemo(
() => BackstageItemsManager.getSyncIdsOfInterest(defaultItems),
[defaultItems]
);
useBackstageItemSyncEffect(defaultItemsManager, syncIdsOfInterest);
const [addonItemsManager] = React.useState(new BackstageItemsManager());
const addonItems = useUiItemsProviderBackstageItems(addonItemsManager);
const addonSyncIdsOfInterest = React.useMemo(
() => BackstageItemsManager.getSyncIdsOfInterest(addonItems),
[addonItems]
);
useBackstageItemSyncEffect(addonItemsManager, addonSyncIdsOfInterest);
const combinedBackstageItems = React.useMemo(
() => combineItems(defaultItems, addonItems, !!props.hideSoloStageEntry),
[defaultItems, addonItems, props.hideSoloStageEntry]
);
const groups = useGroupedItems(combinedBackstageItems);
return (
<NZ_Backstage
className={props.className}
header={props.header}
isOpen={isOpen}
onClose={handleClose}
safeAreaInsets={safeAreaInsets}
showOverlay={props.showOverlay}
style={props.style}
>
{groups.map((group, groupIndex) =>
group.map((item, itemIndex) => {
const composerItem = (
<BackstageComposerItem item={item} key={item.id} />
);
return itemIndex === 0 && groupIndex > 0 ? (
<React.Fragment key={groupIndex}>
<BackstageSeparator />
{composerItem}
</React.Fragment>
) : (
composerItem
);
})
)}
</NZ_Backstage>
);
}