-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTool.tsx
More file actions
123 lines (105 loc) · 3.58 KB
/
Tool.tsx
File metadata and controls
123 lines (105 loc) · 3.58 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
120
121
122
123
import React, { memo, useCallback, useEffect, useState } from 'react';
import { useGlobals, useStorybookState } from 'storybook/manager-api';
import { IconButton } from 'storybook/internal/components';
import { type API_LeafEntry as LeafEntry } from 'storybook/internal/types';
import { FRAMEWORK, KEY, ROUTES } from '../constants';
export const Tool = memo(() => {
const { index: allStories, storyId: currentStoryId } = useStorybookState();
const [globals, updateGlobals, storyGlobals] = useGlobals();
const [twigEnabled, setTwigEnabled] = useState(false);
const isLocked = KEY in storyGlobals;
const isTwigActive = globals[KEY] === FRAMEWORK.TWIG;
const toggle = useCallback((frameworkId: string) => {
updateGlobals({
[KEY]: frameworkId,
});
}, []);
const getStoryId = () => {
if (allStories === undefined) {
return '';
}
const currentStory = allStories[currentStoryId];
if (currentStory.type !== 'story' && currentStory.type !== 'docs') {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return (allStories[currentStoryId] as LeafEntry).title;
};
const isTwigStoryAvailable = () => {
return getStoryId().startsWith('components/src/components/');
};
const openTwigPreview = useCallback(() => {
const storybookIframe = window.document.querySelector<HTMLIFrameElement>('#storybook-preview-iframe');
if (!storybookIframe?.contentWindow?.document) {
return;
}
const twigIframe = storybookIframe.contentWindow.document.querySelector<HTMLIFrameElement>('.twig-preview');
if (!twigIframe) {
return;
}
window.open(twigIframe.src, '_blank');
}, []);
const renderTwigLink = useCallback(() => {
if (!isTwigActive) {
return null;
}
return (
<IconButton
disabled={isLocked}
key="twig-link"
onClick={() => {
openTwigPreview();
}}
title="Open Twig preview"
>
Open Twig preview
</IconButton>
);
}, [isTwigActive, isLocked]);
const showFrameworkSelectorTools = twigEnabled && isTwigStoryAvailable();
useEffect(() => {
const baseUrl = process.env.STORYBOOK_TWIG_COMPONENTS_BASE_URL;
if (baseUrl === undefined || baseUrl === '') {
return;
}
const statusUrl = new URL(ROUTES.STATUS, baseUrl);
fetch(statusUrl)
.then(() => {
setTwigEnabled(true);
})
.catch(() => {
setTwigEnabled(false);
});
}, []);
if (!showFrameworkSelectorTools) {
return null;
}
return (
<>
<IconButton
active={globals[KEY] === FRAMEWORK.REACT}
disabled={isLocked}
key={FRAMEWORK.REACT}
onClick={() => {
toggle(FRAMEWORK.REACT);
}}
title="React"
>
React
</IconButton>
<IconButton
active={isTwigActive}
disabled={isLocked}
key={FRAMEWORK.TWIG}
onClick={() => {
toggle(FRAMEWORK.TWIG);
}}
title="Twig"
>
Twig
</IconButton>
{renderTwigLink()}
</>
);
});
Tool.displayName = 'Tool';