-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: Add ClusterGroupErrorMessage for displaying clusters with e…
…rrors Signed-off-by: René Dudfield <renedudfield@microsoft.com> Co-authored-by: Joaquim Rocha <renedudfield@microsoft.com> Signed-off-by: Joaquim Rocha <joaquim.rocha@microsoft.com>
- Loading branch information
Showing
10 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
frontend/src/components/cluster/ClusterGroupErrorMessage.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { | ||
ClusterGroupErrorMessage, | ||
ClusterGroupErrorMessageProps, | ||
} from './ClusterGroupErrorMessage'; | ||
|
||
const meta: Meta<typeof ClusterGroupErrorMessage> = { | ||
component: ClusterGroupErrorMessage, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<ClusterGroupErrorMessageProps>; | ||
|
||
export const WithClusterErrors: Story = { | ||
args: { | ||
clusterErrors: { | ||
cluster1: 'Error in cluster 1', | ||
cluster3: 'Error in cluster 3', | ||
}, | ||
}, | ||
}; | ||
|
||
export const WithMessageUsed: Story = { | ||
args: { | ||
message: 'This message is used and not clusterErrors.', | ||
clusterErrors: { | ||
cluster1: 'Error in cluster 1', | ||
cluster3: 'Error in cluster 3', | ||
}, | ||
}, | ||
}; | ||
|
||
export const WithDetailedClusterErrors: Story = { | ||
args: { | ||
clusterErrors: { | ||
cluster1: 'Error in cluster 1', | ||
cluster3: null, | ||
}, | ||
}, | ||
}; |
63 changes: 63 additions & 0 deletions
63
frontend/src/components/cluster/ClusterGroupErrorMessage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { InlineIcon } from '@iconify/react'; | ||
import { Box, Typography, useTheme } from '@mui/material'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ApiError } from '../../lib/k8s/apiProxy'; | ||
|
||
export interface ClusterGroupErrorMessageProps { | ||
/** | ||
* A message to display when clusters fail to load resources. | ||
* This is used if it is passed in, otherwise a message made from clusterErrors is used. | ||
*/ | ||
message?: string; | ||
/** | ||
* Either an array of errors, or keyed by cluster name, valued by the error for that cluster. | ||
*/ | ||
clusterErrors?: string[] | { [cluster: string]: string | ApiError | null }; | ||
} | ||
|
||
/** | ||
* filter out null errors | ||
* @returns errors, but without any that have null values. | ||
*/ | ||
function cleanNullErrors(errors: ClusterGroupErrorMessageProps['clusterErrors']) { | ||
if (!errors) { | ||
return {}; | ||
} | ||
const cleanedErrors: ClusterGroupErrorMessageProps['clusterErrors'] = {}; | ||
Object.entries(errors).forEach(([cluster, error]) => { | ||
if (error !== null) { | ||
cleanedErrors[cluster] = error; | ||
} | ||
}); | ||
|
||
return cleanedErrors; | ||
} | ||
|
||
export function ClusterGroupErrorMessage({ | ||
clusterErrors: clusters, | ||
message, | ||
}: ClusterGroupErrorMessageProps) { | ||
const { t } = useTranslation(); | ||
const theme = useTheme(); | ||
const clusterObj = cleanNullErrors(typeof clusters === 'object' ? clusters : {}); | ||
|
||
if ((!clusters && !message) || Object.keys(clusterObj).length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box p={1} style={{ background: theme.palette.warning.light }}> | ||
<Typography style={{ color: theme.palette.warning.main }}> | ||
<InlineIcon icon="mdi:alert" color={theme.palette.warning.main} /> | ||
| ||
<>{message}</> | ||
{!message && | ||
(Object.keys(clusterObj).length > 2 | ||
? t('Failed to load resources from some of the clusters in the group.') | ||
: t('Failed to load resources from the following clusters: {{ clusterList }}', { | ||
clusterList: Object.keys(clusterObj).join(', '), | ||
}))} | ||
</Typography> | ||
</Box> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
...onents/cluster/__snapshots__/ClusterGroupErrorMessage.WithClusterErrors.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<body> | ||
<div> | ||
<div | ||
class="MuiBox-root css-hpgf8j" | ||
style="background: rgb(255, 243, 224);" | ||
> | ||
<p | ||
class="MuiTypography-root MuiTypography-body1 css-1ezega9-MuiTypography-root" | ||
style="color: rgb(196, 69, 0);" | ||
> | ||
|
||
Failed to load resources from the following clusters: cluster1, cluster3 | ||
</p> | ||
</div> | ||
</div> | ||
</body> |
16 changes: 16 additions & 0 deletions
16
...luster/__snapshots__/ClusterGroupErrorMessage.WithDetailedClusterErrors.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<body> | ||
<div> | ||
<div | ||
class="MuiBox-root css-hpgf8j" | ||
style="background: rgb(255, 243, 224);" | ||
> | ||
<p | ||
class="MuiTypography-root MuiTypography-body1 css-1ezega9-MuiTypography-root" | ||
style="color: rgb(196, 69, 0);" | ||
> | ||
|
||
Failed to load resources from the following clusters: cluster1 | ||
</p> | ||
</div> | ||
</div> | ||
</body> |
16 changes: 16 additions & 0 deletions
16
...mponents/cluster/__snapshots__/ClusterGroupErrorMessage.WithMessageUsed.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<body> | ||
<div> | ||
<div | ||
class="MuiBox-root css-hpgf8j" | ||
style="background: rgb(255, 243, 224);" | ||
> | ||
<p | ||
class="MuiTypography-root MuiTypography-body1 css-1ezega9-MuiTypography-root" | ||
style="color: rgb(196, 69, 0);" | ||
> | ||
|
||
This message is used and not clusterErrors. | ||
</p> | ||
</div> | ||
</div> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters