|
| 1 | +import './App.css'; |
| 2 | +import { Page, Button, Drawer } from '@patternfly/react-core'; |
| 3 | +import { |
| 4 | + LoadingBox, |
| 5 | + QuickStartContainer, |
| 6 | + QuickStartContainerProps, |
| 7 | + QuickStartDrawerContent, |
| 8 | + QuickStartCloseModal, |
| 9 | + QuickStartStatus, |
| 10 | + useLocalStorage, |
| 11 | + setQueryArgument, |
| 12 | + removeQueryArgument, |
| 13 | + QUICKSTART_ID_FILTER_KEY, |
| 14 | +} from '@patternfly/quickstarts'; |
| 15 | +import { allQuickStarts as yamlQuickStarts } from './quickstarts-data/quick-start-test-data'; |
| 16 | +import React from 'react'; |
| 17 | +import i18n from './i18n/i18n'; |
| 18 | +import { AppHeader, AppSidebar } from './common/Page'; |
| 19 | + |
| 20 | +interface AppProps { |
| 21 | + children?: React.ReactNode; |
| 22 | + showCardFooters?: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +const App: React.FC<AppProps> = ({ children, showCardFooters }) => { |
| 26 | + const [activeQuickStartID, setActiveQuickStartID] = useLocalStorage('quickstartId', ''); |
| 27 | + const [allQuickStartStates, setAllQuickStartStates] = useLocalStorage('quickstarts', {}); |
| 28 | + const language = localStorage.getItem('bridge/language') || 'en'; |
| 29 | + const resourceBundle = i18n.getResourceBundle(language, 'quickstart'); |
| 30 | + |
| 31 | + // eslint-disable-next-line no-console |
| 32 | + React.useEffect(() => console.log(activeQuickStartID), [activeQuickStartID]); |
| 33 | + React.useEffect(() => { |
| 34 | + // callback on state change |
| 35 | + // eslint-disable-next-line no-console |
| 36 | + console.log(allQuickStartStates); |
| 37 | + }, [allQuickStartStates]); |
| 38 | + |
| 39 | + const withQueryParams = true; |
| 40 | + |
| 41 | + const containerProps: QuickStartContainerProps = { |
| 42 | + quickStarts: yamlQuickStarts, |
| 43 | + activeQuickStartID, |
| 44 | + allQuickStartStates, |
| 45 | + setActiveQuickStartID, |
| 46 | + setAllQuickStartStates, |
| 47 | + resourceBundle, |
| 48 | + showCardFooters, |
| 49 | + language, |
| 50 | + useQueryParams: withQueryParams, |
| 51 | + alwaysShowTaskReview: true, |
| 52 | + markdown: { |
| 53 | + extensions: [ |
| 54 | + // variable substitution |
| 55 | + { |
| 56 | + type: 'output', |
| 57 | + filter(html: string) { |
| 58 | + html = html.replace(/\[APPLICATION\]/g, 'Mercury'); |
| 59 | + html = html.replace(/\[PRODUCT\]/g, 'Lightning'); |
| 60 | + |
| 61 | + return html; |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + }; |
| 67 | + |
| 68 | + const toggleQuickStart = (quickStartId: string) => { |
| 69 | + if (activeQuickStartID !== quickStartId) { |
| 70 | + // activate |
| 71 | + setActiveQuickStartID(quickStartId); |
| 72 | + // optionally add the query param |
| 73 | + withQueryParams && setQueryArgument(QUICKSTART_ID_FILTER_KEY, quickStartId); |
| 74 | + } else { |
| 75 | + // deactivate |
| 76 | + setActiveQuickStartID(''); |
| 77 | + // optionally remove the query param |
| 78 | + withQueryParams && removeQueryArgument(QUICKSTART_ID_FILTER_KEY); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + const [modalOpen, setModalOpen] = React.useState<boolean>(false); |
| 83 | + const onClose = () => setActiveQuickStartID(''); |
| 84 | + const handleClose = (activeQuickStartStatus: string | number) => { |
| 85 | + if (activeQuickStartStatus === QuickStartStatus.IN_PROGRESS) { |
| 86 | + setModalOpen(true); |
| 87 | + } else { |
| 88 | + onClose(); |
| 89 | + } |
| 90 | + onClose(); |
| 91 | + }; |
| 92 | + const onModalConfirm = () => { |
| 93 | + setModalOpen(false); |
| 94 | + onClose(); |
| 95 | + }; |
| 96 | + const onModalCancel = () => setModalOpen(false); |
| 97 | + |
| 98 | + return ( |
| 99 | + <React.Suspense fallback={<LoadingBox />}> |
| 100 | + <QuickStartContainer {...containerProps} isManagedDrawer={false}> |
| 101 | + <Drawer isExpanded={activeQuickStartID !== ''} isInline> |
| 102 | + <QuickStartDrawerContent handleDrawerClose={handleClose}> |
| 103 | + <Page masthead={AppHeader} sidebar={AppSidebar} isManagedSidebar> |
| 104 | + <Button |
| 105 | + variant="secondary" |
| 106 | + onClick={() => toggleQuickStart('getting-started-with-quick-starts')} |
| 107 | + > |
| 108 | + Getting started with quick starts |
| 109 | + </Button> |
| 110 | + {children} |
| 111 | + </Page> |
| 112 | + </QuickStartDrawerContent> |
| 113 | + </Drawer> |
| 114 | + <QuickStartCloseModal |
| 115 | + isOpen={modalOpen} |
| 116 | + onConfirm={onModalConfirm} |
| 117 | + onCancel={onModalCancel} |
| 118 | + /> |
| 119 | + </QuickStartContainer> |
| 120 | + </React.Suspense> |
| 121 | + ); |
| 122 | +}; |
| 123 | +export default App; |
0 commit comments