|
1 | | -import React from 'react'; |
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { useSelector } from 'react-redux'; |
2 | 3 | import { useTranslation } from 'react-i18next'; |
| 4 | +import { CATEGORY } from './FilterByCategory'; |
| 5 | +import { MGDL_UNITS } from '../../../core/constants'; |
| 6 | +import mapValues from 'lodash/mapValues'; |
| 7 | +import { utils as vizUtils } from '@tidepool/viz'; |
| 8 | +const { DEFAULT_BG_BOUNDS } = vizUtils.constants; |
3 | 9 |
|
4 | 10 | import { |
5 | 11 | PatientCell, |
@@ -115,21 +121,78 @@ const getColumnTypes = (t, category, thresholds) => ({ |
115 | 121 | }, // More |
116 | 122 | }); |
117 | 123 |
|
| 124 | +const getFormattedThresholds = (clinicBgUnits) => { |
| 125 | + const thresholds = DEFAULT_BG_BOUNDS[clinicBgUnits]; |
| 126 | + const precision = clinicBgUnits === MGDL_UNITS ? 0 : 1; |
| 127 | + |
| 128 | + return mapValues(thresholds, value => value.toFixed(precision)); |
| 129 | +}; |
| 130 | + |
118 | 131 | const useTableColumns = (category) => { |
119 | 132 | const { t } = useTranslation(); |
| 133 | + const selectedClinicId = useSelector((state) => state.blip.selectedClinicId); |
| 134 | + const clinic = useSelector(state => state.blip.clinics?.[selectedClinicId]); |
| 135 | + const clinicBgUnits = clinic?.preferredBgUnits || MGDL_UNITS; |
| 136 | + |
| 137 | + const columns = useMemo(() => { |
| 138 | + const thresholds = getFormattedThresholds(clinicBgUnits); |
| 139 | + const columnTypes = getColumnTypes(t, category, thresholds); |
| 140 | + |
| 141 | + const standardColumnSet = [ |
| 142 | + columnTypes.patientDetails, |
| 143 | + columnTypes.flag, |
| 144 | + columnTypes.avgGlucose, |
| 145 | + columnTypes.timeInRangeBarChart, |
| 146 | + columnTypes.changeInTIR, |
| 147 | + columnTypes.gmi, |
| 148 | + columnTypes.cgmUse, |
| 149 | + columnTypes.tags, |
| 150 | + columnTypes.lastReviewed, |
| 151 | + columnTypes.moreMenu, |
| 152 | + ]; |
| 153 | + |
| 154 | + const lowColumnSet = [ |
| 155 | + columnTypes.patientDetails, |
| 156 | + columnTypes.flag, |
| 157 | + columnTypes.avgGlucose, |
| 158 | + columnTypes.timeInVeryLow, |
| 159 | + columnTypes.timeInAnyLow, |
| 160 | + columnTypes.timeInTarget, |
| 161 | + columnTypes.timeInRangeBarChart, |
| 162 | + columnTypes.changeInTIR, |
| 163 | + columnTypes.tags, |
| 164 | + columnTypes.lastReviewed, |
| 165 | + columnTypes.moreMenu, |
| 166 | + ]; |
| 167 | + |
| 168 | + const highColumnSet = [ |
| 169 | + columnTypes.patientDetails, |
| 170 | + columnTypes.flag, |
| 171 | + columnTypes.avgGlucose, |
| 172 | + columnTypes.timeInVeryHigh, |
| 173 | + columnTypes.timeInAnyHigh, |
| 174 | + columnTypes.timeInTarget, |
| 175 | + columnTypes.timeInRangeBarChart, |
| 176 | + columnTypes.changeInTIR, |
| 177 | + columnTypes.tags, |
| 178 | + columnTypes.lastReviewed, |
| 179 | + columnTypes.moreMenu, |
| 180 | + ]; |
120 | 181 |
|
121 | | - const columnTypes = getColumnTypes(t, category); |
| 182 | + switch(category) { |
| 183 | + case CATEGORY.DEFAULT: return standardColumnSet; |
| 184 | + case CATEGORY.VERY_LOW: return lowColumnSet; |
| 185 | + case CATEGORY.ANY_LOW: return lowColumnSet; |
| 186 | + case CATEGORY.DROP_IN_TIR: return standardColumnSet; |
| 187 | + case CATEGORY.ANY_HIGH: return highColumnSet; |
| 188 | + case CATEGORY.VERY_HIGH: return highColumnSet; |
| 189 | + case CATEGORY.LOW_CGM_WEAR: return standardColumnSet; |
| 190 | + case CATEGORY.TARGET: return standardColumnSet; |
| 191 | + default: return standardColumnSet; |
| 192 | + } |
| 193 | + }, [category, clinicBgUnits]); |
122 | 194 |
|
123 | | - return [ |
124 | | - columnTypes.patientDetails, |
125 | | - columnTypes.placeholder, |
126 | | - columnTypes.placeholder, |
127 | | - columnTypes.placeholder, |
128 | | - columnTypes.placeholder, |
129 | | - columnTypes.placeholder, |
130 | | - columnTypes.placeholder, |
131 | | - columnTypes.placeholder, |
132 | | - ]; |
| 195 | + return columns; |
133 | 196 | }; |
134 | 197 |
|
135 | 198 | export default useTableColumns; |
0 commit comments