diff options
author | Michael Muré <batolettre@gmail.com> | 2020-02-09 20:08:12 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-02-09 20:23:38 +0100 |
commit | b3d3612393387c83fa43f908dbb8e2a71068c834 (patch) | |
tree | e4609e21dc74e535d45b38cd7d0504681c544160 /webui/src | |
parent | dca85b309a0a82e9993a345964d0831ab2876fb4 (diff) | |
parent | 3caffeef4d2ed25d4eb5d4bfd262f4fc3b92561f (diff) | |
download | git-bug-b3d3612393387c83fa43f908dbb8e2a71068c834.tar.gz |
Merge remote-tracking branch 'origin/master' into cheshirekow-jira
Diffstat (limited to 'webui/src')
-rw-r--r-- | webui/src/.gitignore | 1 | ||||
-rw-r--r-- | webui/src/App.js | 32 | ||||
-rw-r--r-- | webui/src/Content.js | 9 | ||||
-rw-r--r-- | webui/src/CurrentIdentity.js | 45 | ||||
-rw-r--r-- | webui/src/__tests__/query.js | 62 | ||||
-rw-r--r-- | webui/src/bug/Bug.js | 1 | ||||
-rw-r--r-- | webui/src/bug/Message.js | 1 | ||||
-rw-r--r-- | webui/src/index.js | 10 | ||||
-rw-r--r-- | webui/src/list/BugRow.js | 4 | ||||
-rw-r--r-- | webui/src/list/Filter.js | 154 | ||||
-rw-r--r-- | webui/src/list/FilterToolbar.js | 125 | ||||
-rw-r--r-- | webui/src/list/List.js | 48 | ||||
-rw-r--r-- | webui/src/list/ListQuery.js | 314 | ||||
-rw-r--r-- | webui/src/tag/ImageTag.js | 19 | ||||
-rw-r--r-- | webui/src/tag/PreTag.js | 16 |
15 files changed, 771 insertions, 70 deletions
diff --git a/webui/src/.gitignore b/webui/src/.gitignore new file mode 100644 index 00000000..5134e469 --- /dev/null +++ b/webui/src/.gitignore @@ -0,0 +1 @@ +fragmentTypes.js diff --git a/webui/src/App.js b/webui/src/App.js index 4a52eca1..b9c57327 100644 --- a/webui/src/App.js +++ b/webui/src/App.js @@ -1,5 +1,6 @@ import AppBar from '@material-ui/core/AppBar'; import CssBaseline from '@material-ui/core/CssBaseline'; +import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/styles'; import Toolbar from '@material-ui/core/Toolbar'; import React from 'react'; @@ -8,12 +9,31 @@ import { Link } from 'react-router-dom'; import BugQuery from './bug/BugQuery'; import ListQuery from './list/ListQuery'; +import CurrentIdentity from './CurrentIdentity'; + +const theme = createMuiTheme({ + palette: { + primary: { + main: '#263238', + }, + }, +}); const useStyles = makeStyles(theme => ({ + offset: theme.mixins.toolbar, + filler: { + flexGrow: 1, + }, appTitle: { ...theme.typography.h6, color: 'white', textDecoration: 'none', + display: 'flex', + alignItems: 'center', + }, + logo: { + height: '42px', + marginRight: theme.spacing(2), }, })); @@ -21,19 +41,23 @@ export default function App() { const classes = useStyles(); return ( - <> + <ThemeProvider theme={theme}> <CssBaseline /> - <AppBar position="static" color="primary"> + <AppBar position="fixed" color="primary"> <Toolbar> <Link to="/" className={classes.appTitle}> - git-bug webui + <img src="logo.svg" className={classes.logo} alt="git-bug" /> + git-bug </Link> + <div className={classes.filler}></div> + <CurrentIdentity /> </Toolbar> </AppBar> + <div className={classes.offset} /> <Switch> <Route path="/" exact component={ListQuery} /> <Route path="/bug/:id" exact component={BugQuery} /> </Switch> - </> + </ThemeProvider> ); } diff --git a/webui/src/Content.js b/webui/src/Content.js index 19f57631..3a6900bc 100644 --- a/webui/src/Content.js +++ b/webui/src/Content.js @@ -2,12 +2,19 @@ import unified from 'unified'; import parse from 'remark-parse'; import html from 'remark-html'; import remark2react from 'remark-react'; +import ImageTag from './tag/ImageTag'; +import PreTag from './tag/PreTag'; const Content = ({ markdown }) => { const processor = unified() .use(parse) .use(html) - .use(remark2react); + .use(remark2react, { + remarkReactComponents: { + img: ImageTag, + pre: PreTag, + }, + }); return processor.processSync(markdown).contents; }; diff --git a/webui/src/CurrentIdentity.js b/webui/src/CurrentIdentity.js new file mode 100644 index 00000000..451979fb --- /dev/null +++ b/webui/src/CurrentIdentity.js @@ -0,0 +1,45 @@ +import React from 'react'; +import gql from 'graphql-tag'; +import { Query } from 'react-apollo'; +import Avatar from '@material-ui/core/Avatar'; +import { makeStyles } from '@material-ui/styles'; + +const useStyles = makeStyles(theme => ({ + displayName: { + marginLeft: theme.spacing(2), + }, +})); + +const QUERY = gql` + { + defaultRepository { + userIdentity { + displayName + avatarUrl + } + } + } +`; + +const CurrentIdentity = () => { + const classes = useStyles(); + return ( + <Query query={QUERY}> + {({ loading, error, data }) => { + if (error || loading || !data.defaultRepository.userIdentity) + return null; + const user = data.defaultRepository.userIdentity; + return ( + <> + <Avatar src={user.avatarUrl}> + {user.displayName.charAt(0).toUpperCase()} + </Avatar> + <div className={classes.displayName}>{user.displayName}</div> + </> + ); + }} + </Query> + ); +}; + +export default CurrentIdentity; diff --git a/webui/src/__tests__/query.js b/webui/src/__tests__/query.js new file mode 100644 index 00000000..5f4b58eb --- /dev/null +++ b/webui/src/__tests__/query.js @@ -0,0 +1,62 @@ +import { parse, stringify, quote } from '../list/Filter'; + +it('parses a simple query', () => { + expect(parse('foo:bar')).toEqual({ + foo: ['bar'], + }); +}); + +it('parses a query with multiple filters', () => { + expect(parse('foo:bar baz:foo-bar')).toEqual({ + foo: ['bar'], + baz: ['foo-bar'], + }); +}); + +it('parses a quoted query', () => { + expect(parse('foo:"bar"')).toEqual({ + foo: ['bar'], + }); + + expect(parse("foo:'bar'")).toEqual({ + foo: ['bar'], + }); + + expect(parse('foo:\'bar "nested" quotes\'')).toEqual({ + foo: ['bar "nested" quotes'], + }); + + expect(parse("foo:'escaped\\' quotes'")).toEqual({ + foo: ["escaped' quotes"], + }); +}); + +it('parses a query with repetitions', () => { + expect(parse('foo:bar foo:baz')).toEqual({ + foo: ['bar', 'baz'], + }); +}); + +it('parses a complex query', () => { + expect(parse('foo:bar foo:baz baz:"foobar" idont:\'know\'')).toEqual({ + foo: ['bar', 'baz'], + baz: ['foobar'], + idont: ['know'], + }); +}); + +it('quotes values', () => { + expect(quote('foo')).toEqual('foo'); + expect(quote('foo bar')).toEqual('"foo bar"'); + expect(quote('foo "bar"')).toEqual(`'foo "bar"'`); + expect(quote(`foo "bar" 'baz'`)).toEqual(`"foo \\"bar\\" 'baz'"`); +}); + +it('stringifies params', () => { + expect(stringify({ foo: ['bar'] })).toEqual('foo:bar'); + expect(stringify({ foo: ['bar baz'] })).toEqual('foo:"bar baz"'); + expect(stringify({ foo: ['bar', 'baz'] })).toEqual('foo:bar foo:baz'); + expect(stringify({ foo: ['bar'], baz: ['foobar'] })).toEqual( + 'foo:bar baz:foobar' + ); +}); diff --git a/webui/src/bug/Bug.js b/webui/src/bug/Bug.js index 19b8b9ce..5a159f0f 100644 --- a/webui/src/bug/Bug.js +++ b/webui/src/bug/Bug.js @@ -31,6 +31,7 @@ const useStyles = makeStyles(theme => ({ flex: 1, marginTop: theme.spacing(2), marginRight: theme.spacing(2), + minWidth: 0, }, sidebar: { marginTop: theme.spacing(2), diff --git a/webui/src/bug/Message.js b/webui/src/bug/Message.js index db67a3f5..06c12815 100644 --- a/webui/src/bug/Message.js +++ b/webui/src/bug/Message.js @@ -20,6 +20,7 @@ const useStyles = makeStyles(theme => ({ bubble: { flex: 1, marginLeft: theme.spacing(1), + minWidth: 0, }, header: { ...theme.typography.body1, diff --git a/webui/src/index.js b/webui/src/index.js index f71e82f3..6f838c69 100644 --- a/webui/src/index.js +++ b/webui/src/index.js @@ -1,17 +1,27 @@ import ThemeProvider from '@material-ui/styles/ThemeProvider'; import { createMuiTheme } from '@material-ui/core/styles'; import ApolloClient from 'apollo-boost'; +import { + IntrospectionFragmentMatcher, + InMemoryCache, +} from 'apollo-cache-inmemory'; import React from 'react'; import { ApolloProvider } from 'react-apollo'; import ReactDOM from 'react-dom'; import { BrowserRouter } from 'react-router-dom'; +import introspectionQueryResultData from './fragmentTypes'; import App from './App'; const theme = createMuiTheme(); const client = new ApolloClient({ uri: '/graphql', + cache: new InMemoryCache({ + fragmentMatcher: new IntrospectionFragmentMatcher({ + introspectionQueryResultData, + }), + }), }); ReactDOM.render( diff --git a/webui/src/list/BugRow.js b/webui/src/list/BugRow.js index 7b601916..add5c12f 100644 --- a/webui/src/list/BugRow.js +++ b/webui/src/list/BugRow.js @@ -2,8 +2,8 @@ import { makeStyles } from '@material-ui/styles'; import TableCell from '@material-ui/core/TableCell/TableCell'; import TableRow from '@material-ui/core/TableRow/TableRow'; import Tooltip from '@material-ui/core/Tooltip/Tooltip'; -import Typography from '@material-ui/core/Typography'; import ErrorOutline from '@material-ui/icons/ErrorOutline'; +import CheckCircleOutline from '@material-ui/icons/CheckCircleOutline'; import gql from 'graphql-tag'; import React from 'react'; import { Link } from 'react-router-dom'; @@ -19,7 +19,7 @@ const Open = ({ className }) => ( const Closed = ({ className }) => ( <Tooltip title="Closed"> - <ErrorOutline htmlColor="#cb2431" className={className} /> + <CheckCircleOutline htmlColor="#cb2431" className={className} /> </Tooltip> ); diff --git a/webui/src/list/Filter.js b/webui/src/list/Filter.js new file mode 100644 index 00000000..a6cf3633 --- /dev/null +++ b/webui/src/list/Filter.js @@ -0,0 +1,154 @@ +import React, { useState, useRef } from 'react'; +import { Link } from 'react-router-dom'; +import { makeStyles } from '@material-ui/styles'; +import Menu from '@material-ui/core/Menu'; +import MenuItem from '@material-ui/core/MenuItem'; +import ArrowDropDown from '@material-ui/icons/ArrowDropDown'; + +function parse(query) { + // TODO: extract the rest of the query? + const params = {}; + + // TODO: support escaping without quotes + const re = /(\w+):([A-Za-z0-9-]+|(["'])(([^\3]|\\.)*)\3)+/g; + let matches; + while ((matches = re.exec(query)) !== null) { + if (!params[matches[1]]) { + params[matches[1]] = []; + } + + let value; + if (matches[4]) { + value = matches[4]; + } else { + value = matches[2]; + } + value = value.replace(/\\(.)/g, '$1'); + params[matches[1]].push(value); + } + return params; +} + +function quote(value) { + const hasSingle = value.includes("'"); + const hasDouble = value.includes('"'); + const hasSpaces = value.includes(' '); + if (!hasSingle && !hasDouble && !hasSpaces) { + return value; + } + + if (!hasDouble) { + return `"${value}"`; + } + + if (!hasSingle) { + return `'${value}'`; + } + + value = value.replace(/"/g, '\\"'); + return `"${value}"`; +} + +function stringify(params) { + const parts = Object.entries(params).map(([key, values]) => { + return values.map(value => `${key}:${quote(value)}`); + }); + return [].concat(...parts).join(' '); +} + +const useStyles = makeStyles(theme => ({ + element: { + ...theme.typography.body2, + color: ({ active }) => (active ? '#333' : '#444'), + padding: theme.spacing(0, 1), + fontWeight: ({ active }) => (active ? 600 : 400), + textDecoration: 'none', + display: 'flex', + background: 'none', + border: 'none', + }, + itemActive: { + fontWeight: 600, + }, + icon: { + paddingRight: theme.spacing(0.5), + }, +})); + +function Dropdown({ children, dropdown, itemActive, to, ...props }) { + const [open, setOpen] = useState(false); + const buttonRef = useRef(); + const classes = useStyles(); + + return ( + <> + <button ref={buttonRef} onClick={() => setOpen(!open)} {...props}> + {children} + <ArrowDropDown fontSize="small" /> + </button> + <Menu + getContentAnchorEl={null} + anchorOrigin={{ + vertical: 'bottom', + horizontal: 'left', + }} + transformOrigin={{ + vertical: 'top', + horizontal: 'left', + }} + open={open} + onClose={() => setOpen(false)} + anchorEl={buttonRef.current} + > + {dropdown.map(([key, value]) => ( + <MenuItem + component={Link} + to={to(key)} + className={itemActive(key) ? classes.itemActive : null} + onClick={() => setOpen(false)} + key={key} + > + {value} + </MenuItem> + ))} + </Menu> + </> + ); +} + +function Filter({ active, to, children, icon: Icon, dropdown, ...props }) { + const classes = useStyles({ active }); + + const content = ( + <> + {Icon && <Icon fontSize="small" classes={{ root: classes.icon }} />} + <div>{children}</div> + </> + ); + + if (dropdown) { + return ( + <Dropdown + {...props} + to={to} + dropdown={dropdown} + className={classes.element} + > + {content} + </Dropdown> + ); + } + + if (to) { + return ( + <Link to={to} {...props} className={classes.element}> + {content} + </Link> + ); + } + + return <div className={classes.element}>{content}</div>; +} + +export default Filter; +export { parse, stringify, quote }; diff --git a/webui/src/list/FilterToolbar.js b/webui/src/list/FilterToolbar.js new file mode 100644 index 00000000..4d0b52b1 --- /dev/null +++ b/webui/src/list/FilterToolbar.js @@ -0,0 +1,125 @@ +import { makeStyles } from '@material-ui/styles'; +import { useQuery } from '@apollo/react-hooks'; +import gql from 'graphql-tag'; +import React from 'react'; +import Toolbar from '@material-ui/core/Toolbar'; +import ErrorOutline from '@material-ui/icons/ErrorOutline'; +import CheckCircleOutline from '@material-ui/icons/CheckCircleOutline'; +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 => ({ + toolbar: { + backgroundColor: theme.palette.grey['100'], + borderColor: theme.palette.grey['300'], + borderWidth: '1px 0', + borderStyle: 'solid', + margin: theme.spacing(0, -1), + }, + spacer: { + flex: 1, + }, +})); + +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 }) { + const classes = useStyles(); + const params = parse(query); + + const hasKey = key => params[key] && params[key].length > 0; + const hasValue = (key, value) => hasKey(key) && params[key].includes(value); + const loc = params => pipe(params, stringify, queryLocation); + const replaceParam = (key, value) => params => ({ + ...params, + [key]: [value], + }); + const clearParam = key => params => ({ + ...params, + [key]: [], + }); + + // TODO: author/label filters + return ( + <Toolbar className={classes.toolbar}> + <CountingFilter + active={hasValue('status', 'open')} + query={pipe( + params, + replaceParam('status', 'open'), + clearParam('sort'), + stringify + )} + to={pipe(params, replaceParam('status', 'open'), loc)} + icon={ErrorOutline} + > + open + </CountingFilter> + <CountingFilter + active={hasValue('status', 'closed')} + query={pipe( + params, + replaceParam('status', 'closed'), + clearParam('sort'), + stringify + )} + to={pipe(params, replaceParam('status', 'closed'), loc)} + icon={CheckCircleOutline} + > + closed + </CountingFilter> + <div className={classes.spacer} /> + {/* + <Filter active={hasKey('author')}>Author</Filter> + <Filter active={hasKey('label')}>Label</Filter> + */} + <Filter + dropdown={[ + ['id', 'ID'], + ['creation', 'Newest'], + ['creation-asc', 'Oldest'], + ['edit', 'Recently updated'], + ['edit-asc', 'Least recently updated'], + ]} + active={hasKey('sort')} + itemActive={key => hasValue('sort', key)} + to={key => pipe(params, replaceParam('sort', key), loc)} + > + Sort + </Filter> + </Toolbar> + ); +} + +export default FilterToolbar; diff --git a/webui/src/list/List.js b/webui/src/list/List.js index 54b2fe97..63b73545 100644 --- a/webui/src/list/List.js +++ b/webui/src/list/List.js @@ -1,49 +1,17 @@ -import { makeStyles } from '@material-ui/styles'; -import IconButton from '@material-ui/core/IconButton'; import Table from '@material-ui/core/Table/Table'; import TableBody from '@material-ui/core/TableBody/TableBody'; -import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft'; -import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight'; import React from 'react'; import BugRow from './BugRow'; -const useStyles = makeStyles(theme => ({ - main: { - maxWidth: 600, - margin: 'auto', - marginTop: theme.spacing(4), - }, - pagination: { - ...theme.typography.overline, - display: 'flex', - alignItems: 'center', - justifyContent: 'flex-end', - }, -})); - -function List({ bugs, nextPage, prevPage }) { - const classes = useStyles(); - const { hasNextPage, hasPreviousPage } = bugs.pageInfo; +function List({ bugs }) { return ( - <main className={classes.main}> - <Table className={classes.table}> - <TableBody> - {bugs.edges.map(({ cursor, node }) => ( - <BugRow bug={node} key={cursor} /> - ))} - </TableBody> - </Table> - - <div className={classes.pagination}> - <div>Total: {bugs.totalCount}</div> - <IconButton onClick={prevPage} disabled={!hasPreviousPage}> - <KeyboardArrowLeft /> - </IconButton> - <IconButton onClick={nextPage} disabled={!hasNextPage}> - <KeyboardArrowRight /> - </IconButton> - </div> - </main> + <Table> + <TableBody> + {bugs.edges.map(({ cursor, node }) => ( + <BugRow bug={node} key={cursor} /> + ))} + </TableBody> + </Table> ); } diff --git a/webui/src/list/ListQuery.js b/webui/src/list/ListQuery.js index 869bca79..8eeec240 100644 --- a/webui/src/list/ListQuery.js +++ b/webui/src/list/ListQuery.js @@ -1,19 +1,111 @@ -// @flow -import CircularProgress from '@material-ui/core/CircularProgress'; +import { fade, makeStyles } from '@material-ui/core/styles'; +import IconButton from '@material-ui/core/IconButton'; +import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft'; +import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight'; +import ErrorOutline from '@material-ui/icons/ErrorOutline'; +import Paper from '@material-ui/core/Paper'; +import InputBase from '@material-ui/core/InputBase'; +import Skeleton from '@material-ui/lab/Skeleton'; import gql from 'graphql-tag'; -import React, { useState } from 'react'; -import { Query } from 'react-apollo'; +import React, { useState, useEffect, useRef } from 'react'; +import { useQuery } from '@apollo/react-hooks'; +import { useLocation, useHistory, Link } from 'react-router-dom'; import BugRow from './BugRow'; import List from './List'; +import FilterToolbar from './FilterToolbar'; + +const useStyles = makeStyles(theme => ({ + main: { + maxWidth: 800, + margin: 'auto', + marginTop: theme.spacing(4), + marginBottom: theme.spacing(4), + overflow: 'hidden', + }, + pagination: { + ...theme.typography.overline, + display: 'flex', + alignItems: 'center', + justifyContent: 'center', + }, + header: { + display: 'flex', + padding: theme.spacing(2), + '& > h1': { + ...theme.typography.h6, + margin: theme.spacing(0, 2), + }, + alignItems: 'center', + justifyContent: 'space-between', + }, + search: { + borderRadius: theme.shape.borderRadius, + borderColor: fade(theme.palette.primary.main, 0.2), + borderStyle: 'solid', + borderWidth: '1px', + backgroundColor: fade(theme.palette.primary.main, 0.05), + padding: theme.spacing(0, 1), + width: ({ searching }) => (searching ? '20rem' : '15rem'), + transition: theme.transitions.create(), + }, + searchFocused: { + borderColor: fade(theme.palette.primary.main, 0.4), + backgroundColor: theme.palette.background.paper, + width: '20rem!important', + }, + placeholderRow: { + padding: theme.spacing(1), + borderBottomColor: theme.palette.grey['300'], + borderBottomWidth: '1px', + borderBottomStyle: 'solid', + display: 'flex', + alignItems: 'center', + }, + placeholderRowStatus: { + margin: theme.spacing(1, 2), + }, + placeholderRowText: { + flex: 1, + }, + message: { + ...theme.typography.h5, + padding: theme.spacing(8), + textAlign: 'center', + borderBottomColor: theme.palette.grey['300'], + borderBottomWidth: '1px', + borderBottomStyle: 'solid', + '& > p': { + margin: '0', + }, + }, + errorBox: { + color: theme.palette.error.main, + '& > pre': { + fontSize: '1rem', + textAlign: 'left', + backgroundColor: theme.palette.grey['900'], + color: theme.palette.common.white, + marginTop: theme.spacing(4), + padding: theme.spacing(2, 3), + }, + }, +})); const QUERY = gql` - query($first: Int, $last: Int, $after: String, $before: String) { + query( + $first: Int + $last: Int + $after: String + $before: String + $query: String + ) { defaultRepository { bugs: allBugs( first: $first last: $last after: $after before: $before + query: $query ) { totalCount edges { @@ -35,30 +127,206 @@ const QUERY = gql` ${BugRow.fragment} `; +function editParams(params, callback) { + const cloned = new URLSearchParams(params.toString()); + callback(cloned); + return cloned; +} + +// TODO: factor this out +const Placeholder = ({ count }) => { + const classes = useStyles(); + return ( + <> + {new Array(count).fill(null).map((_, i) => ( + <div key={i} className={classes.placeholderRow}> + <Skeleton + className={classes.placeholderRowStatus} + variant="circle" + width={20} + height={20} + /> + <div className={classes.placeholderRowText}> + <Skeleton height={22} /> + <Skeleton height={24} width="60%" /> + </div> + </div> + ))} + </> + ); +}; + +// TODO: factor this out +const NoBug = () => { + const classes = useStyles(); + return ( + <div className={classes.message}> + <ErrorOutline fontSize="large" /> + <p>No results matched your search.</p> + </div> + ); +}; + +const Error = ({ error }) => { + const classes = useStyles(); + return ( + <div className={[classes.errorBox, classes.message].join(' ')}> + <ErrorOutline fontSize="large" /> + <p>There was an error while fetching bug.</p> + <p> + <em>{error.message}</em> + </p> + <pre> + <code>{JSON.stringify(error, null, 2)}</code> + </pre> + </div> + ); +}; + function ListQuery() { - const [page, setPage] = useState({ first: 10, after: null }); + const location = useLocation(); + const history = useHistory(); + const params = new URLSearchParams(location.search); + const query = params.get('q') || ''; + + const [input, setInput] = useState(query); + + const classes = useStyles({ searching: !!input }); + + // TODO is this the right way to do it? + const lastQuery = useRef(); + useEffect(() => { + if (query !== lastQuery.current) { + setInput(query); + } + lastQuery.current = query; + }, [query, input, lastQuery]); + + const page = { + first: params.get('first'), + last: params.get('last'), + after: params.get('after'), + before: params.get('before'), + }; + + // If nothing set, show the first 10 items + if (!page.first && !page.last) { + page.first = 10; + } const perPage = page.first || page.last; - const nextPage = pageInfo => - setPage({ first: perPage, after: pageInfo.endCursor }); - const prevPage = pageInfo => - setPage({ last: perPage, before: pageInfo.startCursor }); + + const { loading, error, data } = useQuery(QUERY, { + variables: { + ...page, + query, + }, + }); + + let nextPage = null; + let previousPage = null; + let hasNextPage = false; + let hasPreviousPage = false; + let count = 0; + if (!loading && !error && data.defaultRepository.bugs) { + const bugs = data.defaultRepository.bugs; + hasNextPage = bugs.pageInfo.hasNextPage; + hasPreviousPage = bugs.pageInfo.hasPreviousPage; + count = bugs.totalCount; + // This computes the URL for the next page + nextPage = { + ...location, + search: editParams(params, p => { + p.delete('last'); + p.delete('before'); + p.set('first', perPage); + p.set('after', bugs.pageInfo.endCursor); + }).toString(), + }; + // and this for the previous page + previousPage = { + ...location, + search: editParams(params, p => { + p.delete('first'); + p.delete('after'); + p.set('last', perPage); + p.set('before', bugs.pageInfo.startCursor); + }).toString(), + }; + } + + // Prepare params without paging for editing filters + const paramsWithoutPaging = editParams(params, p => { + p.delete('first'); + p.delete('last'); + p.delete('before'); + p.delete('after'); + }); + // Returns a new location with the `q` param edited + const queryLocation = query => ({ + ...location, + search: editParams(paramsWithoutPaging, p => p.set('q', query)).toString(), + }); + + let content; + if (loading) { + content = <Placeholder count={10} />; + } else if (error) { + content = <Error error={error} />; + } else { + const bugs = data.defaultRepository.bugs; + + if (bugs.totalCount === 0) { + content = <NoBug />; + } else { + content = <List bugs={bugs} />; + } + } + + const formSubmit = e => { + e.preventDefault(); + history.push(queryLocation(input)); + }; return ( - <Query query={QUERY} variables={page}> - {({ loading, error, data }) => { - if (loading) return <CircularProgress />; - if (error) return <p>Error: {error}</p>; - const bugs = data.defaultRepository.bugs; - return ( - <List - bugs={bugs} - nextPage={() => nextPage(bugs.pageInfo)} - prevPage={() => prevPage(bugs.pageInfo)} + <Paper className={classes.main}> + <header className={classes.header}> + <h1>Issues</h1> + <form onSubmit={formSubmit}> + <InputBase + placeholder="Filter" + value={input} + onInput={e => setInput(e.target.value)} + classes={{ + root: classes.search, + focused: classes.searchFocused, + }} /> - ); - }} - </Query> + <button type="submit" hidden> + Search + </button> + </form> + </header> + <FilterToolbar query={query} queryLocation={queryLocation} /> + {content} + <div className={classes.pagination}> + <IconButton + component={hasPreviousPage ? Link : 'button'} + to={previousPage} + disabled={!hasPreviousPage} + > + <KeyboardArrowLeft /> + </IconButton> + <div>{loading ? 'Loading' : `Total: ${count}`}</div> + <IconButton + component={hasNextPage ? Link : 'button'} + to={nextPage} + disabled={!hasNextPage} + > + <KeyboardArrowRight /> + </IconButton> + </div> + </Paper> ); } diff --git a/webui/src/tag/ImageTag.js b/webui/src/tag/ImageTag.js new file mode 100644 index 00000000..b0f0c1c8 --- /dev/null +++ b/webui/src/tag/ImageTag.js @@ -0,0 +1,19 @@ +import React from 'react'; +import { makeStyles } from '@material-ui/styles'; + +const useStyles = makeStyles({ + tag: { + maxWidth: '100%', + }, +}); + +const ImageTag = ({ alt, ...props }) => { + const classes = useStyles(); + return ( + <a href={props.src} target="_blank" rel="noopener noreferrer nofollow"> + <img className={classes.tag} alt={alt} {...props} /> + </a> + ); +}; + +export default ImageTag; diff --git a/webui/src/tag/PreTag.js b/webui/src/tag/PreTag.js new file mode 100644 index 00000000..c2440df9 --- /dev/null +++ b/webui/src/tag/PreTag.js @@ -0,0 +1,16 @@ +import React from 'react'; +import { makeStyles } from '@material-ui/styles'; + +const useStyles = makeStyles({ + tag: { + maxWidth: '100%', + overflowX: 'auto', + }, +}); + +const PreTag = props => { + const classes = useStyles(); + return <pre className={classes.tag} {...props}></pre>; +}; + +export default PreTag; |