|
| 1 | +import React, { useCallback, useContext, useEffect, useRef } from 'react'; |
| 2 | +import { Steps } from 'antd'; |
| 3 | +import { LoadingOutlined } from '@ant-design/icons'; |
| 4 | +import { useImmer } from 'use-immer'; |
| 5 | +import { noop } from 'lodash'; |
| 6 | + |
| 7 | +const { Step } = Steps; |
| 8 | + |
| 9 | +// Nav state |
| 10 | + |
| 11 | +export enum NotebookNavStep { |
| 12 | + CsvImport, |
| 13 | + DataPreview, |
| 14 | + ColumnSelection, |
| 15 | + AnonymizationSummary, |
| 16 | + AnonymizedResults, |
| 17 | + CsvExport, |
| 18 | +} |
| 19 | + |
| 20 | +export type NotebookNavStepStatus = 'inactive' | 'active' | 'loading' | 'done' | 'failed'; |
| 21 | + |
| 22 | +type NotebookNavStepState = { |
| 23 | + htmlElement: HTMLElement | null; |
| 24 | + status: NotebookNavStepStatus; |
| 25 | +}; |
| 26 | + |
| 27 | +type NotebookNavState = { |
| 28 | + steps: NotebookNavStepState[]; |
| 29 | +}; |
| 30 | + |
| 31 | +const defaultNavState: NotebookNavState = { |
| 32 | + steps: Array(NotebookNavStep.CsvExport + 1) |
| 33 | + .fill(null) |
| 34 | + .map(() => ({ status: 'inactive', htmlElement: null })), |
| 35 | +}; |
| 36 | + |
| 37 | +const NotebookNavStateContext = React.createContext<NotebookNavState>(defaultNavState); |
| 38 | + |
| 39 | +function useNavState(): NotebookNavState { |
| 40 | + return useContext(NotebookNavStateContext); |
| 41 | +} |
| 42 | + |
| 43 | +// Nav functions |
| 44 | + |
| 45 | +type NotebookNavStepPatch = |
| 46 | + | { htmlElement: null; status?: never } |
| 47 | + | { htmlElement: HTMLElement; status: 'active' | 'loading' | 'done' | 'failed' } |
| 48 | + | { htmlElement?: never; status: NotebookNavStepStatus }; |
| 49 | + |
| 50 | +type NotebookNavFunctions = { |
| 51 | + updateStep(step: NotebookNavStep, patch: NotebookNavStepPatch): void; |
| 52 | +}; |
| 53 | + |
| 54 | +const NotebookNavFunctionsContext = React.createContext<NotebookNavFunctions>({ |
| 55 | + updateStep: noop, |
| 56 | +}); |
| 57 | + |
| 58 | +function useNavFunctions(): NotebookNavFunctions { |
| 59 | + return useContext(NotebookNavFunctionsContext); |
| 60 | +} |
| 61 | + |
| 62 | +// Context provider |
| 63 | + |
| 64 | +export const NotebookNavProvider: React.FunctionComponent = ({ children }) => { |
| 65 | + const [navState, updateNavState] = useImmer(defaultNavState); |
| 66 | + const navFunctions = useRef<NotebookNavFunctions>({ |
| 67 | + updateStep(step, patch) { |
| 68 | + updateNavState((draft) => { |
| 69 | + const { steps } = draft as NotebookNavState; |
| 70 | + if (patch.htmlElement === null) { |
| 71 | + steps[step] = { |
| 72 | + htmlElement: null, |
| 73 | + status: 'inactive', |
| 74 | + }; |
| 75 | + } else if (patch.htmlElement) { |
| 76 | + steps[step] = patch; |
| 77 | + } else if (steps[step].htmlElement) { |
| 78 | + steps[step].status = patch.status; |
| 79 | + } |
| 80 | + }); |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + return ( |
| 85 | + <NotebookNavStateContext.Provider value={navState}> |
| 86 | + <NotebookNavFunctionsContext.Provider value={navFunctions.current}> |
| 87 | + {children} |
| 88 | + </NotebookNavFunctionsContext.Provider> |
| 89 | + </NotebookNavStateContext.Provider> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +// Context consumers |
| 94 | + |
| 95 | +export type NotebookNavAnchorProps = { |
| 96 | + step: NotebookNavStep; |
| 97 | + status?: NotebookNavStepStatus; |
| 98 | +}; |
| 99 | + |
| 100 | +export const NotebookNavAnchor: React.FunctionComponent<NotebookNavAnchorProps> = ({ step, status = 'active' }) => { |
| 101 | + const navFunctions = useNavFunctions(); |
| 102 | + |
| 103 | + const ref = useCallback( |
| 104 | + (htmlElement: HTMLElement | null) => { |
| 105 | + navFunctions.updateStep(step, { |
| 106 | + htmlElement, |
| 107 | + status, |
| 108 | + } as NotebookNavStepPatch); |
| 109 | + }, |
| 110 | + [step, status, navFunctions], |
| 111 | + ); |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + navFunctions.updateStep(step, { status }); |
| 115 | + }, [step, status, navFunctions]); |
| 116 | + |
| 117 | + return ( |
| 118 | + <div style={{ position: 'relative' }}> |
| 119 | + <div ref={ref} style={{ position: 'absolute', top: -60, left: 0 }}></div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +function mapStatus(status: NotebookNavStepStatus): 'error' | 'process' | 'finish' | 'wait' { |
| 125 | + switch (status) { |
| 126 | + case 'inactive': |
| 127 | + return 'wait'; |
| 128 | + case 'active': |
| 129 | + case 'loading': |
| 130 | + return 'process'; |
| 131 | + case 'done': |
| 132 | + return 'finish'; |
| 133 | + case 'failed': |
| 134 | + return 'error'; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +export const NotebookNav: React.FunctionComponent = () => { |
| 139 | + const { steps } = useNavState(); |
| 140 | + const status = (step: NotebookNavStep) => mapStatus(steps[step].status); |
| 141 | + |
| 142 | + return ( |
| 143 | + <Steps |
| 144 | + progressDot={(dot, { index }) => |
| 145 | + steps[index].status === 'loading' ? ( |
| 146 | + <span |
| 147 | + key="loading" |
| 148 | + className="ant-steps-icon-dot" |
| 149 | + style={{ |
| 150 | + backgroundColor: 'transparent', |
| 151 | + color: '#1890ff', |
| 152 | + left: -5, |
| 153 | + }} |
| 154 | + > |
| 155 | + <LoadingOutlined /> |
| 156 | + </span> |
| 157 | + ) : ( |
| 158 | + dot |
| 159 | + ) |
| 160 | + } |
| 161 | + direction="vertical" |
| 162 | + current={-1} |
| 163 | + onChange={(step) => { |
| 164 | + const { htmlElement } = steps[step]; |
| 165 | + if (htmlElement) { |
| 166 | + htmlElement.scrollIntoView({ |
| 167 | + behavior: 'smooth', |
| 168 | + block: 'start', |
| 169 | + }); |
| 170 | + } |
| 171 | + }} |
| 172 | + > |
| 173 | + <Step status={status(NotebookNavStep.CsvImport)} title="CSV Import" description="Load data from CSV" /> |
| 174 | + <Step status={status(NotebookNavStep.DataPreview)} title="Data Preview" description="Preview contents of file" /> |
| 175 | + <Step |
| 176 | + status={status(NotebookNavStep.ColumnSelection)} |
| 177 | + title="Column Selection" |
| 178 | + description="Select columns for anonymization" |
| 179 | + /> |
| 180 | + <Step |
| 181 | + status={status(NotebookNavStep.AnonymizationSummary)} |
| 182 | + title="Anonymization Summary" |
| 183 | + description="Review distortion statistics" |
| 184 | + /> |
| 185 | + <Step |
| 186 | + status={status(NotebookNavStep.AnonymizedResults)} |
| 187 | + title="Anonymized Results" |
| 188 | + description="Preview anonymized results" |
| 189 | + /> |
| 190 | + <Step status={status(NotebookNavStep.CsvExport)} title="CSV Export" description="Export anonymized data to CSV" /> |
| 191 | + </Steps> |
| 192 | + ); |
| 193 | +}; |
0 commit comments