|
| 1 | +import './App.css'; |
| 2 | +import { |
| 3 | + Page, |
| 4 | + Button, |
| 5 | + Drawer, |
| 6 | + DrawerContent, |
| 7 | + DrawerPanelContent, |
| 8 | + DrawerHead, |
| 9 | + DrawerActions, |
| 10 | + DrawerCloseButton, |
| 11 | + DrawerPanelDescription, |
| 12 | + DrawerPanelBody, |
| 13 | + DrawerContentBody, |
| 14 | +} from '@patternfly/react-core'; |
| 15 | +import { |
| 16 | + LoadingBox, |
| 17 | + QuickStartContainer, |
| 18 | + QuickStartContainerProps, |
| 19 | + QuickStartDrawerContent, |
| 20 | + QuickStartCloseModal, |
| 21 | + QuickStartStatus, |
| 22 | + useLocalStorage, |
| 23 | + setQueryArgument, |
| 24 | + removeQueryArgument, |
| 25 | + QUICKSTART_ID_FILTER_KEY, |
| 26 | +} from '@patternfly/quickstarts'; |
| 27 | +import { allQuickStarts as yamlQuickStarts } from './quickstarts-data/quick-start-test-data'; |
| 28 | +import React from 'react'; |
| 29 | +import i18n from './i18n/i18n'; |
| 30 | +import { AppHeader, AppSidebar } from './common/Page'; |
| 31 | + |
| 32 | +interface AppProps { |
| 33 | + children?: React.ReactNode; |
| 34 | + showCardFooters?: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +const App: React.FC<AppProps> = ({ children, showCardFooters }) => { |
| 38 | + const [activeQuickStartID, setActiveQuickStartID] = useLocalStorage('quickstartId', ''); |
| 39 | + const [allQuickStartStates, setAllQuickStartStates] = useLocalStorage('quickstarts', {}); |
| 40 | + const language = localStorage.getItem('bridge/language') || 'en'; |
| 41 | + const resourceBundle = i18n.getResourceBundle(language, 'quickstart'); |
| 42 | + |
| 43 | + // eslint-disable-next-line no-console |
| 44 | + React.useEffect(() => console.log(activeQuickStartID), [activeQuickStartID]); |
| 45 | + React.useEffect(() => { |
| 46 | + // callback on state change |
| 47 | + // eslint-disable-next-line no-console |
| 48 | + console.log(allQuickStartStates); |
| 49 | + }, [allQuickStartStates]); |
| 50 | + |
| 51 | + const withQueryParams = true; |
| 52 | + |
| 53 | + const containerProps: QuickStartContainerProps = { |
| 54 | + quickStarts: yamlQuickStarts, |
| 55 | + activeQuickStartID, |
| 56 | + allQuickStartStates, |
| 57 | + setActiveQuickStartID, |
| 58 | + setAllQuickStartStates, |
| 59 | + resourceBundle, |
| 60 | + showCardFooters, |
| 61 | + language, |
| 62 | + useQueryParams: withQueryParams, |
| 63 | + alwaysShowTaskReview: true, |
| 64 | + markdown: { |
| 65 | + extensions: [ |
| 66 | + // variable substitution |
| 67 | + { |
| 68 | + type: 'output', |
| 69 | + filter(html: string) { |
| 70 | + html = html.replace(/\[APPLICATION\]/g, 'Mercury'); |
| 71 | + html = html.replace(/\[PRODUCT\]/g, 'Lightning'); |
| 72 | + |
| 73 | + return html; |
| 74 | + }, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + const toggleQuickStart = (quickStartId: string) => { |
| 81 | + if (activeQuickStartID !== quickStartId) { |
| 82 | + // activate |
| 83 | + setActiveQuickStartID(quickStartId); |
| 84 | + // optionally add the query param |
| 85 | + withQueryParams && setQueryArgument(QUICKSTART_ID_FILTER_KEY, quickStartId); |
| 86 | + } else { |
| 87 | + // deactivate |
| 88 | + setActiveQuickStartID(''); |
| 89 | + // optionally remove the query param |
| 90 | + withQueryParams && removeQueryArgument(QUICKSTART_ID_FILTER_KEY); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const [modalOpen, setModalOpen] = React.useState<boolean>(false); |
| 95 | + const onClose = () => { |
| 96 | + setActiveQuickStartID(''); |
| 97 | + setDrawerContent('none'); |
| 98 | + }; |
| 99 | + const handleClose = (activeQuickStartStatus: string | number) => { |
| 100 | + if (activeQuickStartStatus === QuickStartStatus.IN_PROGRESS) { |
| 101 | + setModalOpen(true); |
| 102 | + } else { |
| 103 | + onClose(); |
| 104 | + } |
| 105 | + onClose(); |
| 106 | + }; |
| 107 | + const onModalConfirm = () => { |
| 108 | + setModalOpen(false); |
| 109 | + onClose(); |
| 110 | + }; |
| 111 | + const onModalCancel = () => setModalOpen(false); |
| 112 | + |
| 113 | + const [drawerContent, setDrawerContent] = React.useState('none'); |
| 114 | + |
| 115 | + const otherDrawerPanelContent = ( |
| 116 | + <DrawerPanelContent> |
| 117 | + <DrawerHead> |
| 118 | + <span tabIndex={drawerContent === 'custom' ? 0 : -1}>Drawer panel header</span> |
| 119 | + <DrawerActions> |
| 120 | + <DrawerCloseButton onClick={() => setDrawerContent('none')} /> |
| 121 | + </DrawerActions> |
| 122 | + </DrawerHead> |
| 123 | + <DrawerPanelDescription>Drawer panel description</DrawerPanelDescription> |
| 124 | + <DrawerPanelBody>Drawer panel body</DrawerPanelBody> |
| 125 | + </DrawerPanelContent> |
| 126 | + ); |
| 127 | + |
| 128 | + return ( |
| 129 | + <React.Suspense fallback={<LoadingBox />}> |
| 130 | + <QuickStartContainer {...containerProps} isManagedDrawer={false}> |
| 131 | + <Drawer isExpanded={drawerContent !== 'none'} isInline> |
| 132 | + <DrawerContent |
| 133 | + panelContent={ |
| 134 | + drawerContent === 'quickstart' || activeQuickStartID !== '' ? ( |
| 135 | + <QuickStartDrawerContent handleDrawerClose={handleClose} /> |
| 136 | + ) : ( |
| 137 | + otherDrawerPanelContent |
| 138 | + ) |
| 139 | + } |
| 140 | + > |
| 141 | + <DrawerContentBody> |
| 142 | + <Page masthead={AppHeader} sidebar={AppSidebar} isManagedSidebar> |
| 143 | + <Button |
| 144 | + variant="secondary" |
| 145 | + onClick={() => { |
| 146 | + toggleQuickStart('getting-started-with-quick-starts'); |
| 147 | + setDrawerContent('quickstart'); |
| 148 | + }} |
| 149 | + > |
| 150 | + Getting started with quick starts |
| 151 | + </Button> |
| 152 | + <Button |
| 153 | + variant="secondary" |
| 154 | + onClick={() => { |
| 155 | + setActiveQuickStartID(''); |
| 156 | + setDrawerContent('custom'); |
| 157 | + }} |
| 158 | + > |
| 159 | + Open a different drawer |
| 160 | + </Button> |
| 161 | + {children} |
| 162 | + </Page> |
| 163 | + </DrawerContentBody> |
| 164 | + </DrawerContent> |
| 165 | + </Drawer> |
| 166 | + <QuickStartCloseModal |
| 167 | + isOpen={modalOpen} |
| 168 | + onConfirm={onModalConfirm} |
| 169 | + onCancel={onModalCancel} |
| 170 | + /> |
| 171 | + </QuickStartContainer> |
| 172 | + </React.Suspense> |
| 173 | + ); |
| 174 | +}; |
| 175 | +export default App; |
0 commit comments