2023-06-22 10:52:02 -05:00
|
|
|
import { formatMm } from 'shared/utils.mjs'
|
2023-05-29 13:35:04 -05:00
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
/*********************************************************************************************************
|
|
|
|
* This file contains the base components to be used for displaying values in menu titles in the workbench
|
|
|
|
* Values that deal with more specific use cases should wrap one of the below components
|
|
|
|
*********************************************************************************************************/
|
|
|
|
|
|
|
|
/** The basis of it all. Handles the changed/unchanged styling for the wrapped value */
|
2023-05-29 13:35:04 -05:00
|
|
|
export const HighlightedValue = ({ changed, children }) => (
|
2023-05-29 22:34:33 -05:00
|
|
|
<span className={changed ? 'text-info' : ''}> {children} </span>
|
2023-05-29 13:35:04 -05:00
|
|
|
)
|
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
/**
|
|
|
|
* A wrapper for displaying the correct value based on whether or not the value has changed
|
|
|
|
* @param {Number|String|Boolean} options.current the current value, if it has been changed
|
|
|
|
* @param {Number|String|Boolean} options.dflt the default value
|
|
|
|
* @param {Boolean} options.changed has the value been changed?
|
|
|
|
*/
|
2023-05-29 22:34:33 -05:00
|
|
|
export const PlainValue = ({ current, dflt, changed }) => (
|
|
|
|
<HighlightedValue changed={changed}> {changed ? current : dflt} </HighlightedValue>
|
2023-05-29 13:35:04 -05:00
|
|
|
)
|
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
/**
|
|
|
|
* Displays the correct, translated value for a list
|
|
|
|
* @param {String|Boolean} options.current the current value, if it has been changed
|
|
|
|
* @param {Function} options.t a translation function
|
|
|
|
* @param {Object} options.config the item config
|
|
|
|
* @param {Boolean} options.changed has the value been changed?
|
|
|
|
*/
|
2023-05-29 22:34:33 -05:00
|
|
|
export const ListValue = ({ current, t, config, changed }) => {
|
2023-05-31 17:42:16 -05:00
|
|
|
// get the values
|
2023-05-29 22:34:33 -05:00
|
|
|
const val = changed ? current : config.dflt
|
2023-05-31 17:42:16 -05:00
|
|
|
|
|
|
|
// key will be based on a few factors
|
2023-05-29 22:34:33 -05:00
|
|
|
let key
|
2023-05-31 17:42:16 -05:00
|
|
|
// are valueTitles configured?
|
2023-05-29 22:34:33 -05:00
|
|
|
if (config.valueTitles) key = config.valueTitles[val]
|
2023-05-31 17:42:16 -05:00
|
|
|
// if not, is the value a string
|
2023-05-29 22:34:33 -05:00
|
|
|
else if (typeof val === 'string') key = val
|
2023-05-31 17:42:16 -05:00
|
|
|
// otherwise stringify booleans
|
2023-05-29 22:34:33 -05:00
|
|
|
else if (val) key = 'yes'
|
|
|
|
else key = 'no'
|
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
const translated = config.doNotTranslate ? key : t(key)
|
|
|
|
|
|
|
|
return <HighlightedValue changed={changed}>{translated}</HighlightedValue>
|
2023-05-29 22:34:33 -05:00
|
|
|
}
|
2023-05-29 13:35:04 -05:00
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
/** Displays the corrent, translated value for a boolean */
|
2023-05-29 23:22:26 -05:00
|
|
|
export const BoolValue = ListValue
|
|
|
|
|
2023-05-31 17:42:16 -05:00
|
|
|
/** Displays a formated mm value based on the current units */
|
2023-05-29 23:22:26 -05:00
|
|
|
export const MmValue = ({ current, config, units, changed }) => (
|
2023-05-29 13:35:04 -05:00
|
|
|
<HighlightedValue changed={changed}>
|
|
|
|
<span
|
|
|
|
dangerouslySetInnerHTML={{
|
2023-06-22 10:32:01 -05:00
|
|
|
__html: formatMm(changed ? current : config.dflt, units),
|
2023-05-29 13:35:04 -05:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</HighlightedValue>
|
|
|
|
)
|