-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmanager.jsx
More file actions
119 lines (99 loc) · 3.7 KB
/
Copy pathmanager.jsx
File metadata and controls
119 lines (99 loc) · 3.7 KB
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
import startCase from 'lodash/startCase';
import React from 'react';
import { addons, types } from 'storybook/manager-api';
import SidebarStatusTag from './components/SidebarStatusTag';
import StatusDot from './components/StatusDot';
import StatusTag from './components/StatusTag';
import { ADDON_ID } from './constants';
import { getStatusConfigs } from './getStatusConfigs';
addons.register(ADDON_ID, (api) => {
const addonsConfig = addons.getConfig();
const existingSidebarConfig = addonsConfig?.sidebar ?? {};
addons.add(ADDON_ID, {
title: 'Status',
type: types.TOOL,
render: () => <StatusTag />,
});
const statusAddonConfig = addonsConfig?.[ADDON_ID] ?? {};
addons.setConfig({
sidebar: {
...existingSidebarConfig,
renderLabel: (item) => {
const { name, tags } = item;
const isLeaf = ['root', 'group', 'story'].includes(item.type);
try {
const fallbackLabel = existingSidebarConfig?.renderLabel
? existingSidebarConfig.renderLabel(item)
: name;
const sidebarTagsConfig = statusAddonConfig?.sidebarTags;
const sidebarDotsConfig = statusAddonConfig?.sidebarDots;
// sidebarTags, when set, fully overrides sidebarDots.
const isTagMode =
sidebarTagsConfig === 'single' || sidebarTagsConfig === 'multiple';
if (sidebarTagsConfig === 'none') {
return fallbackLabel;
}
if (sidebarTagsConfig === undefined && sidebarDotsConfig === 'none') {
return fallbackLabel;
}
const parameters = api.getParameters(item.id, ADDON_ID);
// item can be a Root | Group | Story
if (!isLeaf || (tags.length === 0 && !parameters?.type)) {
return fallbackLabel;
}
// Get custom status configurations from the current story's parameters.
// This will include any custom statuses defined in manager.js, preview.js or story parameters.
// However custom statuses from story parameters will only be available in the sidebar
// when viewing that story. This is a storybook limitation:
// https://github.com/storybookjs/storybook/discussions/24022
const customConfigs =
statusAddonConfig?.statuses ||
api.getCurrentStoryData().parameters?.status?.statuses;
let statusConfigs = getStatusConfigs({
tags,
parameters,
customConfigs,
});
if (statusConfigs.length === 0) {
return fallbackLabel;
}
const showMultiple = isTagMode
? sidebarTagsConfig === 'multiple'
: sidebarDotsConfig === 'multiple';
if (!showMultiple) {
statusConfigs = [statusConfigs[0]];
}
return (
<>
{fallbackLabel}
{statusConfigs.map((statusConfig) => {
if (isTagMode) {
return (
<SidebarStatusTag
key={statusConfig.label}
statusConfig={statusConfig}
/>
);
}
const {
label: statusName,
status: { background, description },
} = statusConfig;
return (
<StatusDot
key={statusName}
type={statusName}
background={background}
title={`${startCase(statusName)}: ${description}`}
/>
);
})}
</>
);
} catch (error) {
return name;
}
},
},
});
});