Skip to content

Commit

Permalink
plugin(pod-counter): Make error message configurable
Browse files Browse the repository at this point in the history
this patch registers a Settings Detail Component
which allows the user to set the error message to
be displayed when the pods count cannot be
retrieved.

Signed-off-by: yolossn <[email protected]>
  • Loading branch information
yolossn committed Feb 1, 2024
1 parent f0f291a commit 067fa3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugins/examples/pod-counter/src/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConfigStore } from '@kinvolk/headlamp-plugin/lib';
import { Typography } from '@mui/material';
import { makeStyles } from '@mui/styles';

Expand Down Expand Up @@ -25,9 +26,13 @@ export interface MessageProps {
*/
export default function Message({ msg, error }: MessageProps) {
const classes = useStyle();
const config = new ConfigStore<{ errorMessage?: string }>('@kinvolk/headlamp-pod-counter');
const useConf = config.useConfig();
const conf = useConf();

return (
<Typography color="textPrimary" className={classes.pods}>
{!error ? `# Pods: ${msg}` : 'Uh, pods!?'}
{!error ? `# Pods: ${msg}` : conf?.errorMessage ? conf?.errorMessage : 'Uh, pods!?'}
</Typography>
);
}
35 changes: 35 additions & 0 deletions plugins/examples/pod-counter/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import {
AppBarActionsProcessorArgs,
DefaultAppBarAction,
K8s,
PluginSettingsDetailProps,
registerAppBarAction,
registerPluginSettings,
} from '@kinvolk/headlamp-plugin/lib';
import { NameValueTable } from '@kinvolk/headlamp-plugin/lib/CommonComponents';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';
import Message from './Message';

function PodCounter() {
Expand All @@ -30,3 +35,33 @@ registerAppBarAction(function reorderNotifications({ actions }: AppBarActionsPro

return newActions;
});

const Settings: React.FC<PluginSettingsDetailProps> = props => {
const { data, onDataChange } = props;

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onDataChange({ errorMessage: event.target.value });
};

const settingsRows = [
{
name: 'Custom Error message',
value: (
<TextField
fullWidth
helperText="Enter the custom error message to display when the pod count cannot be retrieved"
defaultValue={data?.errorMessage ? data.errorMessage : 'Uh.pods!?'}
onChange={handleChange}
variant="standard"
/>
),
},
];
return (
<Box width={'80%'}>
<NameValueTable rows={settingsRows} />
</Box>
);
};

registerPluginSettings('@kinvolk/headlamp-pod-counter', Settings, false);

0 comments on commit 067fa3c

Please sign in to comment.