|
1 | | -import { useConnection } from "@diamondlightsource/cs-web-lib"; |
2 | | -import { Box } from "@mui/material"; |
| 1 | +import { Box, useTheme } from "@mui/material"; |
3 | 2 | import { ErrorBoundary } from "react-error-boundary"; |
4 | | -import { RawValue } from "./util"; |
| 3 | +import { PvComponentProps, PvDescription, PvItem } from "./types"; |
| 4 | +import { useParsedPvConnection, forceString } from "./util"; |
5 | 5 |
|
6 | | -type Transformer = (value: RawValue) => string | number; |
7 | | -export type PvDisplayTypes = string | number; |
8 | | -export type PvItem = { label: string; value: RawValue | PvDisplayTypes }; |
9 | | -export type PvItemComponent = ({ label, value }: PvItem) => JSX.Element; |
10 | | -export type PvItemHandler = ({ label, value }: PvItem) => void; |
11 | | -export type PvDescription = { |
12 | | - label: string; |
13 | | - pv: string; |
14 | | -}; |
15 | | -export type PvComponentProps = PvDescription & { |
16 | | - render: PvItemComponent; |
17 | | - transformValue?: Transformer; |
18 | | -}; |
19 | | - |
20 | | -export function useParsedPvConnection( |
21 | | - props: PvDescription & { transformValue?: Transformer } |
22 | | -) { |
23 | | - const [_effectivePvName, connected, _readonly, latestValue] = useConnection( |
24 | | - props.label, |
25 | | - props.pv |
26 | | - ); |
27 | | - const rawValue: RawValue = connected ? latestValue : "not connected"; |
28 | | - const returnValue = props.transformValue |
29 | | - ? props.transformValue(rawValue) |
30 | | - : rawValue; |
31 | | - console.log( |
32 | | - `fetched parsed value ${returnValue} for PV: ${props.pv} labeled ${props.label}` |
| 6 | +function defaultPvBox(label: string, value: number | string): JSX.Element { |
| 7 | + return ( |
| 8 | + <Box> |
| 9 | + <p> |
| 10 | + <b>{label}:</b> {value} |
| 11 | + </p> |
| 12 | + </Box> |
33 | 13 | ); |
34 | | - return returnValue; |
35 | 14 | } |
36 | 15 |
|
37 | | -function WsPvComponent(props: PvComponentProps) { |
| 16 | +function WsPvComponent(props: PvComponentProps): JSX.Element { |
38 | 17 | const latestValue = useParsedPvConnection(props); |
39 | | - return <Box>{props.render({ label: props.label, value: latestValue })}</Box>; |
| 18 | + const renderedPvBox = props.render ? ( |
| 19 | + <Box>{props.render({ label: props.label, value: latestValue })}</Box> |
| 20 | + ) : ( |
| 21 | + defaultPvBox(props.label, latestValue) |
| 22 | + ); |
| 23 | + return renderedPvBox; |
40 | 24 | } |
41 | 25 |
|
42 | 26 | export function PvComponent(props: PvComponentProps) { |
| 27 | + const theme = useTheme(); |
43 | 28 | return ( |
44 | | - <ErrorBoundary fallback={<p>Error Connecting!</p>}> |
| 29 | + <ErrorBoundary |
| 30 | + fallback={ |
| 31 | + <Box color={theme.palette.error.main}> |
| 32 | + <p>Error Connecting!</p> |
| 33 | + </Box> |
| 34 | + } |
| 35 | + > |
45 | 36 | {WsPvComponent(props)} |
46 | 37 | </ErrorBoundary> |
47 | 38 | ); |
48 | 39 | } |
| 40 | + |
| 41 | +export function RoPvBox(props: PvDescription) { |
| 42 | + return PvComponent({ |
| 43 | + ...props, |
| 44 | + transformValue: forceString, |
| 45 | + render: (props: PvItem) => { |
| 46 | + return ( |
| 47 | + <Box> |
| 48 | + <p> |
| 49 | + {props.label} : {props.value} |
| 50 | + </p> |
| 51 | + </Box> |
| 52 | + ); |
| 53 | + }, |
| 54 | + }); |
| 55 | +} |
0 commit comments