webui: add open/closed issues count

This commit is contained in:
Quentin Gliech 2020-01-30 23:38:25 +01:00
parent ead5bad785
commit adb28885a3
No known key found for this signature in database
GPG Key ID: 22D62B84552719FC
2 changed files with 68 additions and 16 deletions

View File

@ -1,10 +1,17 @@
import { makeStyles } from '@material-ui/styles'; import { makeStyles } from '@material-ui/styles';
import { useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import React from 'react'; import React from 'react';
import Toolbar from '@material-ui/core/Toolbar'; import Toolbar from '@material-ui/core/Toolbar';
import ErrorOutline from '@material-ui/icons/ErrorOutline'; import ErrorOutline from '@material-ui/icons/ErrorOutline';
import CheckCircleOutline from '@material-ui/icons/CheckCircleOutline'; import CheckCircleOutline from '@material-ui/icons/CheckCircleOutline';
import Filter, { parse, stringify } from './Filter'; import Filter, { parse, stringify } from './Filter';
// simple pipe operator
// pipe(o, f, g, h) <=> h(g(f(o)))
// TODO: move this out?
const pipe = (initial, ...funcs) => funcs.reduce((acc, f) => f(acc), initial);
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles(theme => ({
toolbar: { toolbar: {
backgroundColor: theme.palette.grey['100'], backgroundColor: theme.palette.grey['100'],
@ -18,40 +25,85 @@ const useStyles = makeStyles(theme => ({
}, },
})); }));
const BUG_COUNT_QUERY = gql`
query($query: String) {
defaultRepository {
bugs: allBugs(query: $query) {
totalCount
}
}
}
`;
// This prepends the filter text with a count
function CountingFilter({ query, children, ...props }) {
const { data, loading, error } = useQuery(BUG_COUNT_QUERY, {
variables: { query },
});
var prefix;
if (loading) prefix = '...';
else if (error) prefix = '???';
// TODO: better prefixes & error handling
else prefix = data.defaultRepository.bugs.totalCount;
return (
<Filter {...props}>
{prefix} {children}
</Filter>
);
}
function FilterToolbar({ query, queryLocation }) { function FilterToolbar({ query, queryLocation }) {
const classes = useStyles(); const classes = useStyles();
const params = parse(query); const params = parse(query);
const hasKey = key => params[key] && params[key].length > 0; const hasKey = key => params[key] && params[key].length > 0;
const hasValue = (key, value) => hasKey(key) && params[key].includes(value); const hasValue = (key, value) => hasKey(key) && params[key].includes(value);
const replaceParam = (key, value) => { const loc = params => pipe(params, stringify, queryLocation);
const p = { const replaceParam = (key, value) => params => ({
...params, ...params,
[key]: [value], [key]: [value],
}; });
return queryLocation(stringify(p)); const clearParam = key => params => ({
}; ...params,
[key]: [],
});
// TODO: open/closed count
// TODO: author/label filters // TODO: author/label filters
return ( return (
<Toolbar className={classes.toolbar}> <Toolbar className={classes.toolbar}>
<Filter <CountingFilter
active={hasValue('status', 'open')} active={hasValue('status', 'open')}
to={replaceParam('status', 'open')} query={pipe(
params,
replaceParam('status', 'open'),
clearParam('sort'),
stringify
)}
to={pipe(params, replaceParam('status', 'open'), loc)}
icon={ErrorOutline} icon={ErrorOutline}
> >
open open
</Filter> </CountingFilter>
<Filter <CountingFilter
active={hasValue('status', 'closed')} active={hasValue('status', 'closed')}
to={replaceParam('status', 'closed')} query={pipe(
params,
replaceParam('status', 'closed'),
clearParam('sort'),
stringify
)}
to={pipe(params, replaceParam('status', 'closed'), loc)}
icon={CheckCircleOutline} icon={CheckCircleOutline}
> >
closed closed
</Filter> </CountingFilter>
<div className={classes.spacer} /> <div className={classes.spacer} />
{/*
<Filter active={hasKey('author')}>Author</Filter> <Filter active={hasKey('author')}>Author</Filter>
<Filter active={hasKey('label')}>Label</Filter> <Filter active={hasKey('label')}>Label</Filter>
*/}
<Filter <Filter
dropdown={[ dropdown={[
['id', 'ID'], ['id', 'ID'],
@ -62,7 +114,7 @@ function FilterToolbar({ query, queryLocation }) {
]} ]}
active={hasKey('sort')} active={hasKey('sort')}
itemActive={key => hasValue('sort', key)} itemActive={key => hasValue('sort', key)}
to={key => replaceParam('sort', key)} to={key => pipe(params, replaceParam('sort', key), loc)}
> >
Sort Sort
</Filter> </Filter>

View File

@ -187,7 +187,7 @@ function ListQuery() {
const location = useLocation(); const location = useLocation();
const history = useHistory(); const history = useHistory();
const params = new URLSearchParams(location.search); const params = new URLSearchParams(location.search);
const query = params.get('q'); const query = params.get('q') || '';
const [input, setInput] = useState(query); const [input, setInput] = useState(query);