Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
feat(ui): drawer. replace statuses pie with inline indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
s-r-x committed Sep 17, 2021
1 parent 70ea23f commit 20fec69
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 65 deletions.
21 changes: 14 additions & 7 deletions packages/ui/src/components/Drawer/Queues/JobsCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import React from 'react';
import type { JobStatus } from '@/typings/gql';
import JobStatusChip from '@/components/JobStatusChip';
import { makeStyles } from '@material-ui/core/styles';
import StatusesIndicator from './QueueStatusesIndicator';

const useStyles = makeStyles({
root: {
position: 'relative',
},
list: {
display: 'flex',
flexWrap: 'wrap',
alignItems: 'center',
Expand All @@ -24,12 +28,15 @@ type TProps = {
export default function QueueJobsCount(props: TProps) {
const cls = useStyles();
return (
<ul className={cls.root}>
{props.count.map(({ status, count }, idx) => (
<li key={idx} title={status}>
<JobStatusChip size="small" status={status} label={count} />
</li>
))}
</ul>
<div className={cls.root}>
<ul className={cls.list}>
{props.count.map(({ status, count }, idx) => (
<li key={idx} title={status}>
<JobStatusChip size="small" status={status} label={count} />
</li>
))}
</ul>
<StatusesIndicator count={props.count} />
</div>
);
}
25 changes: 8 additions & 17 deletions packages/ui/src/components/Drawer/Queues/Queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ import PauseIcon from '@material-ui/icons/Pause';
import { makeStyles } from '@material-ui/core/styles';
import Badge from '@material-ui/core/Badge';
import Typography from '@material-ui/core/Typography';
import {
useJobsCountArray,
useQueueWorkspaceLabel,
useShouldRenderStatusesPie,
} from './hooks';
import StatusesPie from './QueueStatusesPie';
import { useJobsCountArray, useQueueWorkspaceLabel } from './hooks';

const useStyles = makeStyles((theme) => ({
listItem: {
position: 'relative',
paddingRight: theme.spacing(1),
'& .MuiListItemIcon-root': {
minWidth: 32,
Expand All @@ -38,8 +34,6 @@ const DrawerQueue = (props: TProps) => {
props.onSelect(id, workspaceLabel);
}, [id, workspaceLabel]);
const countArray = useJobsCountArray(props.queue.jobsCounts);
const shouldRenderStatusesPie = useShouldRenderStatusesPie(props.queue);
const shouldRenderListIcon = isPaused || shouldRenderStatusesPie;
return (
<ListItem
onClick={onSelect}
Expand All @@ -48,16 +42,13 @@ const DrawerQueue = (props: TProps) => {
dense
button
>
{shouldRenderListIcon && (
{isPaused && (
<ListItemIcon>
{isPaused && (
<ListItemIcon>
<Badge color="secondary" showZero>
<PauseIcon />
</Badge>
</ListItemIcon>
)}
{shouldRenderStatusesPie && <StatusesPie count={countArray} />}
<ListItemIcon>
<Badge color="secondary" showZero>
<PauseIcon />
</Badge>
</ListItemIcon>
</ListItemIcon>
)}
<ListItemText
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import type { JobStatus } from '@/typings/gql';
import { useJobStatusesPalette } from '@/components/JobStatusChip/hooks';
import { MathUtils } from '@/services/math-utils';
import sum from 'lodash/sum';
import { useMemo } from 'react';

const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
height: 3,
overflow: 'hidden',
marginTop: theme.spacing(0.5),
},
}));

type TProps = {
count: { status: JobStatus; count: number }[];
};
export default function QueueStatusesIndicator({ count }: TProps) {
const cls = useStyles();
const palette = useJobStatusesPalette();
const mapped = useMemo(() => {
const allJobsCount = sum(count.map(({ count }) => count));
return count.map(({ count, status }) => ({
width: MathUtils.mapNumberToPercent(count, 0, allJobsCount) + '%',
color: palette[status],
}));
}, [palette, count]);
return (
<div className={cls.root}>
{mapped.map(({ width, color }, idx) => (
<div style={{ width, backgroundColor: color }} key={idx} />
))}
</div>
);
}
29 changes: 0 additions & 29 deletions packages/ui/src/components/Drawer/Queues/QueueStatusesPie.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions packages/ui/src/components/Drawer/Queues/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,3 @@ export const useMaybeGroupQueuesByPrefix = (queues: QueueFromQuery[]) => {
}
return groupBy(queues, 'keyPrefix');
};

export const useShouldRenderStatusesPie = (queue: QueueFromQuery): boolean => {
const pref = usePreferencesStore((state) => state.showStatusesPieInDrawer);
return useMemo(() => {
if (!pref || queue.isPaused) return false;
// show pie only if there are at least two different non zero statuses
const [firstCount, secondCount] = Object.values(queue.jobsCounts).sort(
(a, b) => b - a
);
return firstCount > 0 && secondCount > 0;
}, [pref, queue.isPaused, queue.jobsCounts]);
};

0 comments on commit 20fec69

Please sign in to comment.