Skip to content

Commit 6e1b431

Browse files
feat: support id_aliases for PluginSlots
1 parent 78ddff8 commit 6e1b431

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/plugins/PluginSlot.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function BasePluginSlot({
1414
as = React.Fragment,
1515
children = null,
1616
id,
17+
id_aliases = [],
1718
pluginProps = {},
1819
slotOptions = {},
1920
slotErrorFallbackComponent,
@@ -23,7 +24,7 @@ function BasePluginSlot({
2324
for an example of how PluginSlot is populated, and example/src/index.jsx for a dummy JS config that holds all plugins
2425
*/
2526

26-
const { keepDefault, plugins } = usePluginSlot(id);
27+
const { keepDefault, plugins } = usePluginSlot(id, id_aliases);
2728
const { formatMessage } = useIntl();
2829

2930
const defaultContents = React.useMemo(() => {
@@ -118,6 +119,8 @@ BasePluginSlot.propTypes = {
118119
children: PropTypes.node,
119120
/** ID of the PluginSlot configuration */
120121
id: PropTypes.string.isRequired,
122+
/** Aliases (additional IDs for the PluginSlot) */
123+
id_aliases: PropTypes.arrayOf(PropTypes.string),
121124
/** Props that are passed down to each Plugin in the Slot */
122125
pluginProps: PropTypes.shape(),
123126
/** Options passed to the PluginSlot */

src/plugins/data/hooks.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,37 @@ import { PLUGIN_MOUNTED, PLUGIN_READY, PLUGIN_UNMOUNTED } from './constants';
1414
* @param {String} id - Name of PluginSlot
1515
* @returns {Object} - JS configuration for the PluginSlot
1616
*/
17-
export function usePluginSlot(id) {
18-
const configSlots = getConfigSlots()?.[id];
19-
if (configSlots) {
20-
return configSlots;
17+
export function usePluginSlot(id, id_aliases = []) {
18+
const notFound = { keepDefault: true, plugins: [] };
19+
20+
const allConfigSlots = getConfigSlots();
21+
22+
if (!allConfigSlots) {
23+
return notFound;
24+
}
25+
26+
// When defining a JS object with multiple entries
27+
// that have the same key, only the last one is kept
28+
//
29+
// We want to treat all entries in the pluginSlots object
30+
// in env.config.jsx that have either "id" or any of the
31+
// "id_aliases" as if they have the same id
32+
//
33+
// To do so, we grab the last entry in the object that with
34+
// a key that matches either the "id" or any of the "id_aliases"
35+
const allSlotIds = [id].concat(id_aliases);
36+
const lastMatchingId = Object.keys(allConfigSlots).reverse().find(slotId => allSlotIds.includes(slotId));
37+
38+
if (!lastMatchingId) {
39+
return notFound;
40+
}
41+
42+
const configSlots = allConfigSlots[lastMatchingId];
43+
44+
if (!configSlots) {
45+
return notFound;
2146
}
22-
return { keepDefault: true, plugins: [] };
47+
return configSlots;
2348
}
2449

2550
/* Listening for events */

0 commit comments

Comments
 (0)