|
| 1 | +import './App.css'; |
| 2 | +import { |
| 3 | + Page, |
| 4 | + Button, |
| 5 | + Drawer, |
| 6 | + DrawerContent, |
| 7 | + Tabs, |
| 8 | + Tab, |
| 9 | + TabTitleText, |
| 10 | + DrawerContentBody, |
| 11 | + DrawerPanelContent, |
| 12 | + DrawerHead, |
| 13 | + DrawerActions, |
| 14 | + DrawerCloseButton, |
| 15 | + DrawerPanelDescription, |
| 16 | + DrawerPanelBody, |
| 17 | +} from '@patternfly/react-core'; |
| 18 | +import { |
| 19 | + LoadingBox, |
| 20 | + QuickStartContainer, |
| 21 | + QuickStartContainerProps, |
| 22 | + QuickStartController, |
| 23 | + QuickStartCloseModal, |
| 24 | + QuickStartStatus, |
| 25 | + useLocalStorage, |
| 26 | + setQueryArgument, |
| 27 | + removeQueryArgument, |
| 28 | + QUICKSTART_ID_FILTER_KEY, |
| 29 | +} from '@patternfly/quickstarts'; |
| 30 | +import { allQuickStarts as yamlQuickStarts } from './quickstarts-data/quick-start-test-data'; |
| 31 | +import React from 'react'; |
| 32 | +import i18n from './i18n/i18n'; |
| 33 | +import { AppHeader, AppSidebar } from './common/Page'; |
| 34 | + |
| 35 | +interface AppProps { |
| 36 | + children?: React.ReactNode; |
| 37 | + showCardFooters?: boolean; |
| 38 | +} |
| 39 | + |
| 40 | +const App: React.FC<AppProps> = ({ children, showCardFooters }) => { |
| 41 | + const [activeQuickStartID, setActiveQuickStartID] = useLocalStorage('quickstartId', ''); |
| 42 | + const [allQuickStartStates, setAllQuickStartStates] = useLocalStorage('quickstarts', {}); |
| 43 | + const language = localStorage.getItem('bridge/language') || 'en'; |
| 44 | + const resourceBundle = i18n.getResourceBundle(language, 'quickstart'); |
| 45 | + |
| 46 | + // eslint-disable-next-line no-console |
| 47 | + React.useEffect(() => console.log(activeQuickStartID), [activeQuickStartID]); |
| 48 | + React.useEffect(() => { |
| 49 | + // callback on state change |
| 50 | + // eslint-disable-next-line no-console |
| 51 | + console.log(allQuickStartStates); |
| 52 | + }, [allQuickStartStates]); |
| 53 | + |
| 54 | + const withQueryParams = true; |
| 55 | + |
| 56 | + const containerProps: QuickStartContainerProps = { |
| 57 | + quickStarts: yamlQuickStarts, |
| 58 | + activeQuickStartID, |
| 59 | + allQuickStartStates, |
| 60 | + setActiveQuickStartID, |
| 61 | + setAllQuickStartStates, |
| 62 | + resourceBundle, |
| 63 | + showCardFooters, |
| 64 | + language, |
| 65 | + useQueryParams: withQueryParams, |
| 66 | + alwaysShowTaskReview: true, |
| 67 | + markdown: { |
| 68 | + extensions: [ |
| 69 | + // variable substitution |
| 70 | + { |
| 71 | + type: 'output', |
| 72 | + filter(html: string) { |
| 73 | + html = html.replace(/\[APPLICATION\]/g, 'Mercury'); |
| 74 | + html = html.replace(/\[PRODUCT\]/g, 'Lightning'); |
| 75 | + |
| 76 | + return html; |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + const toggleQuickStart = (quickStartId: string) => { |
| 84 | + if (activeQuickStartID !== quickStartId) { |
| 85 | + // activate |
| 86 | + setActiveQuickStartID(quickStartId); |
| 87 | + // optionally add the query param |
| 88 | + withQueryParams && setQueryArgument(QUICKSTART_ID_FILTER_KEY, quickStartId); |
| 89 | + } else { |
| 90 | + // deactivate |
| 91 | + setActiveQuickStartID(''); |
| 92 | + // optionally remove the query param |
| 93 | + withQueryParams && removeQueryArgument(QUICKSTART_ID_FILTER_KEY); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + const [modalOpen, setModalOpen] = React.useState<boolean>(false); |
| 98 | + const onClose = () => { |
| 99 | + setActiveQuickStartID(''); |
| 100 | + setIsDrawerOpen(false); |
| 101 | + }; |
| 102 | + const handleClose = (activeQuickStartStatus: string | number) => { |
| 103 | + // need to hook up to modal close button |
| 104 | + if (activeQuickStartStatus === QuickStartStatus.IN_PROGRESS) { |
| 105 | + setModalOpen(true); |
| 106 | + } else { |
| 107 | + onClose(); |
| 108 | + } |
| 109 | + onClose(); |
| 110 | + }; |
| 111 | + const onModalConfirm = () => { |
| 112 | + setModalOpen(false); |
| 113 | + onClose(); |
| 114 | + }; |
| 115 | + const onModalCancel = () => setModalOpen(false); |
| 116 | + |
| 117 | + const [isDrawerOpen, setIsDrawerOpen] = React.useState(false); |
| 118 | + const [activeTabKey, setActiveTabKey] = React.useState<string | number>(0); |
| 119 | + // Toggle currently active tab |
| 120 | + const handleTabClick = ( |
| 121 | + event: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent, |
| 122 | + tabIndex: string | number, |
| 123 | + ) => { |
| 124 | + setActiveTabKey(tabIndex); |
| 125 | + }; |
| 126 | + |
| 127 | + // needed for QuickStartController and metadata filling out |
| 128 | + // we're basically rendering the QS on the user code opposed to QuickStartPanelContent |
| 129 | + const quickStart = yamlQuickStarts.find((qs) => qs.metadata.name === activeQuickStartID); |
| 130 | + const nextQuickStart = yamlQuickStarts.filter((qs) => |
| 131 | + quickStart?.spec.nextQuickStart?.includes(qs.metadata.name), |
| 132 | + ); |
| 133 | + const panelContent = ( |
| 134 | + <DrawerPanelContent isResizable> |
| 135 | + <DrawerHead> |
| 136 | + <span>{activeQuickStartID !== '' ? quickStart?.spec.displayName : 'No QS title'}</span> |
| 137 | + <DrawerActions> |
| 138 | + <DrawerCloseButton onClick={() => setIsDrawerOpen(false)} /> |
| 139 | + </DrawerActions> |
| 140 | + </DrawerHead> |
| 141 | + <DrawerPanelDescription>Drawer panel description</DrawerPanelDescription> |
| 142 | + <DrawerPanelBody |
| 143 | + style={{ display: 'flex', flexDirection: 'column' } /** need for footer spacing */} |
| 144 | + > |
| 145 | + <Tabs |
| 146 | + activeKey={activeTabKey} |
| 147 | + onSelect={handleTabClick} |
| 148 | + aria-label="Tabs in a drawer" |
| 149 | + role="region" |
| 150 | + > |
| 151 | + {activeQuickStartID !== '' && ( |
| 152 | + <Tab |
| 153 | + eventKey={0} |
| 154 | + title={<TabTitleText>Quickstart</TabTitleText>} |
| 155 | + aria-label="Default content - users" |
| 156 | + style={{ flex: '1 1', display: 'flex', flexDirection: 'column' }} // not currently spread to tab - react issue opened https://github.com/patternfly/patternfly-react/issues/11342 |
| 157 | + > |
| 158 | + <QuickStartController quickStart={quickStart} nextQuickStarts={nextQuickStart} /> |
| 159 | + </Tab> |
| 160 | + )} |
| 161 | + |
| 162 | + <Tab eventKey={1} title={<TabTitleText>Chatbot</TabTitleText>}> |
| 163 | + Chatbot |
| 164 | + <Button |
| 165 | + variant="secondary" |
| 166 | + onClick={() => { |
| 167 | + toggleQuickStart('getting-started-with-quick-starts'); |
| 168 | + }} |
| 169 | + > |
| 170 | + Toggle QS |
| 171 | + </Button> |
| 172 | + </Tab> |
| 173 | + </Tabs> |
| 174 | + </DrawerPanelBody> |
| 175 | + </DrawerPanelContent> |
| 176 | + ); |
| 177 | + |
| 178 | + return ( |
| 179 | + <React.Suspense fallback={<LoadingBox />}> |
| 180 | + <QuickStartContainer {...containerProps} isManagedDrawer={false}> |
| 181 | + <Drawer isExpanded={isDrawerOpen} isInline> |
| 182 | + <DrawerContent panelContent={panelContent}> |
| 183 | + <DrawerContentBody> |
| 184 | + <Page masthead={AppHeader} sidebar={AppSidebar} isManagedSidebar> |
| 185 | + <Button |
| 186 | + variant="secondary" |
| 187 | + onClick={() => { |
| 188 | + setIsDrawerOpen(!isDrawerOpen); |
| 189 | + }} |
| 190 | + > |
| 191 | + Toggle Drawer |
| 192 | + </Button> |
| 193 | + <Button |
| 194 | + variant="secondary" |
| 195 | + onClick={() => { |
| 196 | + toggleQuickStart('getting-started-with-quick-starts'); |
| 197 | + }} |
| 198 | + > |
| 199 | + Toggle QS |
| 200 | + </Button> |
| 201 | + {children} |
| 202 | + </Page> |
| 203 | + </DrawerContentBody> |
| 204 | + </DrawerContent> |
| 205 | + </Drawer> |
| 206 | + <QuickStartCloseModal |
| 207 | + isOpen={modalOpen} |
| 208 | + onConfirm={onModalConfirm} |
| 209 | + onCancel={onModalCancel} |
| 210 | + /> |
| 211 | + </QuickStartContainer> |
| 212 | + </React.Suspense> |
| 213 | + ); |
| 214 | +}; |
| 215 | +export default App; |
0 commit comments