|
| 1 | +import CloseIcon from '@material-ui/icons/Close' |
| 2 | +import { useState, useEffect, useCallback } from 'react' |
| 3 | +import { makeStyles, Snackbar } from '@material-ui/core' |
| 4 | +import CheckCircleIcon from '@material-ui/icons/CheckCircle' |
| 5 | +import { Button } from '@postgres.ai/shared/components/MenuButton' |
| 6 | +import { Spinner } from '@postgres.ai/shared/components/Spinner' |
| 7 | +import { capitalizeFirstLetter } from './utils' |
| 8 | + |
| 9 | +import { getBillingStatus } from 'api/configs/getBillingStatus' |
| 10 | +import { activateBilling } from 'api/configs/activateBilling' |
| 11 | + |
| 12 | +import styles from './styles.module.scss' |
| 13 | + |
| 14 | +const AUTO_HIDE_DURATION = 1500 |
| 15 | + |
| 16 | +export const StickyTopBar = () => { |
| 17 | + const useStyles = makeStyles( |
| 18 | + { |
| 19 | + errorNotification: { |
| 20 | + '& > div.MuiSnackbarContent-root': { |
| 21 | + backgroundColor: '#f44336!important', |
| 22 | + minWidth: '100%', |
| 23 | + }, |
| 24 | + |
| 25 | + '& div.MuiSnackbarContent-message': { |
| 26 | + display: 'flex', |
| 27 | + alignItems: 'center', |
| 28 | + justifyContent: 'center', |
| 29 | + padding: '4px 0', |
| 30 | + gap: '10px', |
| 31 | + fontSize: '13px', |
| 32 | + }, |
| 33 | + }, |
| 34 | + successNotification: { |
| 35 | + '& > div.MuiSnackbarContent-root': { |
| 36 | + backgroundColor: '#4caf50!important', |
| 37 | + minWidth: '100%', |
| 38 | + }, |
| 39 | + |
| 40 | + '& div.MuiSnackbarContent-message': { |
| 41 | + display: 'flex', |
| 42 | + alignItems: 'center', |
| 43 | + justifyContent: 'center', |
| 44 | + padding: '4px 0', |
| 45 | + gap: '10px', |
| 46 | + fontSize: '13px', |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { index: 1 }, |
| 51 | + ) |
| 52 | + |
| 53 | + const classes = useStyles() |
| 54 | + const [isLoading, setIsLoading] = useState(false) |
| 55 | + const [snackbarState, setSnackbarState] = useState<{ |
| 56 | + isOpen: boolean |
| 57 | + message: string | null |
| 58 | + type: 'error' | 'success' | null |
| 59 | + }>({ |
| 60 | + isOpen: false, |
| 61 | + message: null, |
| 62 | + type: null, |
| 63 | + }) |
| 64 | + const [state, setState] = useState<{ |
| 65 | + type: 'billingInactive' | 'missingOrgKey' | 'noConnection' | null |
| 66 | + pageUrl?: string |
| 67 | + message: string |
| 68 | + }>({ |
| 69 | + type: null, |
| 70 | + pageUrl: '', |
| 71 | + message: '', |
| 72 | + }) |
| 73 | + |
| 74 | + const handleReset = useCallback(() => { |
| 75 | + setState({ |
| 76 | + type: null, |
| 77 | + message: '', |
| 78 | + }) |
| 79 | + }, []) |
| 80 | + |
| 81 | + const handleResetSnackbar = useCallback(() => { |
| 82 | + setTimeout(() => { |
| 83 | + setSnackbarState({ |
| 84 | + isOpen: false, |
| 85 | + message: null, |
| 86 | + type: null, |
| 87 | + }) |
| 88 | + }, AUTO_HIDE_DURATION) |
| 89 | + }, []) |
| 90 | + |
| 91 | + const handleActivate = useCallback(() => { |
| 92 | + setIsLoading(true) |
| 93 | + activateBilling() |
| 94 | + .then((res) => { |
| 95 | + setIsLoading(false) |
| 96 | + if (res.response) { |
| 97 | + handleReset() |
| 98 | + setSnackbarState({ |
| 99 | + isOpen: true, |
| 100 | + message: 'All DLE SE features are now active.', |
| 101 | + type: 'success', |
| 102 | + }) |
| 103 | + } else { |
| 104 | + setSnackbarState({ |
| 105 | + isOpen: true, |
| 106 | + message: capitalizeFirstLetter(res.error.message), |
| 107 | + type: 'error', |
| 108 | + }) |
| 109 | + } |
| 110 | + handleResetSnackbar() |
| 111 | + }) |
| 112 | + .finally(() => { |
| 113 | + setIsLoading(false) |
| 114 | + }) |
| 115 | + }, [setIsLoading, handleReset, handleResetSnackbar]) |
| 116 | + |
| 117 | + useEffect(() => { |
| 118 | + getBillingStatus().then((res) => { |
| 119 | + if (!res.response?.billing_active && res.response?.recognized_org) { |
| 120 | + setState({ |
| 121 | + type: 'billingInactive', |
| 122 | + pageUrl: res.response?.recognized_org.billing_page, |
| 123 | + message: |
| 124 | + 'No active payment methods are found for your organization on the Postgres.ai Platform; please, visit the', |
| 125 | + }) |
| 126 | + } else if (!res.response?.recognized_org) { |
| 127 | + setState({ |
| 128 | + type: 'missingOrgKey', |
| 129 | + message: capitalizeFirstLetter(res.error.message), |
| 130 | + }) |
| 131 | + } |
| 132 | + }) |
| 133 | + }, [handleResetSnackbar]) |
| 134 | + |
| 135 | + useEffect(() => { |
| 136 | + const handleNoConnection = () => { |
| 137 | + setState({ |
| 138 | + type: 'noConnection', |
| 139 | + message: 'No internet connection', |
| 140 | + }) |
| 141 | + } |
| 142 | + |
| 143 | + const handleConnectionRestored = () => { |
| 144 | + setState({ |
| 145 | + type: null, |
| 146 | + message: '', |
| 147 | + }) |
| 148 | + handleActivate() |
| 149 | + } |
| 150 | + |
| 151 | + window.addEventListener('offline', handleNoConnection) |
| 152 | + window.addEventListener('online', handleConnectionRestored) |
| 153 | + |
| 154 | + return () => { |
| 155 | + window.removeEventListener('offline', handleNoConnection) |
| 156 | + window.removeEventListener('online', handleConnectionRestored) |
| 157 | + } |
| 158 | + }, [handleActivate]) |
| 159 | + |
| 160 | + return ( |
| 161 | + <> |
| 162 | + {state.type && ( |
| 163 | + <div className={styles.container}> |
| 164 | + <p>{state.message}</p> |
| 165 | + |
| 166 | + {state.type === 'billingInactive' ? ( |
| 167 | + <> |
| 168 | + <a href={state.pageUrl} target="_blank" rel="noreferrer"> |
| 169 | + billing page |
| 170 | + </a> |
| 171 | + . Once resolved, |
| 172 | + <Button |
| 173 | + type="button" |
| 174 | + className={styles.activateBtn} |
| 175 | + onClick={handleActivate} |
| 176 | + disabled={isLoading} |
| 177 | + > |
| 178 | + re-activate DLE |
| 179 | + {isLoading && <Spinner size="sm" className={styles.spinner} />} |
| 180 | + </Button> |
| 181 | + </> |
| 182 | + ) : state.type === 'missingOrgKey' ? ( |
| 183 | + <> |
| 184 | + Once resolved, |
| 185 | + <Button |
| 186 | + type="button" |
| 187 | + className={styles.activateBtn} |
| 188 | + onClick={handleActivate} |
| 189 | + disabled={isLoading} |
| 190 | + > |
| 191 | + re-activate DLE |
| 192 | + {isLoading && <Spinner size="sm" className={styles.spinner} />} |
| 193 | + </Button> |
| 194 | + </> |
| 195 | + ) : null} |
| 196 | + </div> |
| 197 | + )} |
| 198 | + <Snackbar |
| 199 | + open={snackbarState.isOpen} |
| 200 | + className={ |
| 201 | + snackbarState.type === 'error' |
| 202 | + ? classes.errorNotification |
| 203 | + : snackbarState.type === 'success' |
| 204 | + ? classes.successNotification |
| 205 | + : '' |
| 206 | + } |
| 207 | + autoHideDuration={AUTO_HIDE_DURATION} |
| 208 | + message={ |
| 209 | + <> |
| 210 | + {snackbarState.type === 'error' ? ( |
| 211 | + <CloseIcon /> |
| 212 | + ) : ( |
| 213 | + snackbarState.type === 'success' && <CheckCircleIcon /> |
| 214 | + )} |
| 215 | + {snackbarState.message} |
| 216 | + </> |
| 217 | + } |
| 218 | + /> |
| 219 | + </> |
| 220 | + ) |
| 221 | +} |
0 commit comments