1 | import React, { useState, useEffect } from 'react';
|
2 | import { addons, types } from '@storybook/addons';
|
3 | import { STORY_CHANGED } from '@storybook/core-events';
|
4 | import ActionLogger from './containers/ActionLogger';
|
5 | import { ADDON_ID, EVENT_ID, PANEL_ID, PARAM_KEY } from './constants';
|
6 | addons.register(ADDON_ID, api => {
|
7 | addons.addPanel(PANEL_ID, {
|
8 | title() {
|
9 | const [actionsCount, setActionsCount] = useState(0);
|
10 |
|
11 | const onEvent = () => setActionsCount(previous => previous + 1);
|
12 |
|
13 | const onChange = () => setActionsCount(0);
|
14 |
|
15 | useEffect(() => {
|
16 | api.on(EVENT_ID, onEvent);
|
17 | api.on(STORY_CHANGED, onChange);
|
18 | return () => {
|
19 | api.off(EVENT_ID, onEvent);
|
20 | api.off(STORY_CHANGED, onChange);
|
21 | };
|
22 | });
|
23 | const suffix = actionsCount === 0 ? '' : ` (${actionsCount})`;
|
24 | return `Actions${suffix}`;
|
25 | },
|
26 |
|
27 | type: types.PANEL,
|
28 | render: ({
|
29 | active,
|
30 | key
|
31 | }) => React.createElement(ActionLogger, {
|
32 | key: key,
|
33 | api: api,
|
34 | active: active
|
35 | }),
|
36 | paramKey: PARAM_KEY
|
37 | });
|
38 | }); |
\ | No newline at end of file |