webui: convert more things to typescript

This commit is contained in:
Quentin Gliech 2020-02-04 22:03:21 +01:00
parent 6a502c145b
commit 022f510369
No known key found for this signature in database
GPG Key ID: 22D62B84552719FC
10 changed files with 79 additions and 58 deletions

View File

@ -2,7 +2,10 @@ import Tooltip from '@material-ui/core/Tooltip/Tooltip';
import MAvatar from '@material-ui/core/Avatar'; import MAvatar from '@material-ui/core/Avatar';
import React from 'react'; import React from 'react';
const Author = ({ author, ...props }) => { import { AuthoredFragment } from './Author.generated';
type Props = AuthoredFragment;
const Author = ({ author, ...props }: Props) => {
if (!author.email) { if (!author.email) {
return <span {...props}>{author.displayName}</span>; return <span {...props}>{author.displayName}</span>;
} }
@ -14,7 +17,7 @@ const Author = ({ author, ...props }) => {
); );
}; };
export const Avatar = ({ author, ...props }) => { export const Avatar = ({ author, ...props }: Props) => {
if (author.avatarUrl) { if (author.avatarUrl) {
return <MAvatar src={author.avatarUrl} {...props} />; return <MAvatar src={author.avatarUrl} {...props} />;
} }

View File

@ -2,10 +2,12 @@ import unified from 'unified';
import parse from 'remark-parse'; import parse from 'remark-parse';
import html from 'remark-html'; import html from 'remark-html';
import remark2react from 'remark-react'; import remark2react from 'remark-react';
import { ReactNode } from 'react';
import ImageTag from './tag/ImageTag'; import ImageTag from './tag/ImageTag';
import PreTag from './tag/PreTag'; import PreTag from './tag/PreTag';
const Content = ({ markdown }) => { type Props = { markdown: string };
const Content = ({ markdown }: Props) => {
const processor = unified() const processor = unified()
.use(parse) .use(parse)
.use(html) .use(html)
@ -16,7 +18,8 @@ const Content = ({ markdown }) => {
}, },
}); });
return processor.processSync(markdown).contents; const contents: ReactNode = processor.processSync(markdown).contents;
return contents;
}; };
export default Content; export default Content;

View File

@ -0,0 +1,8 @@
query CurrentIdentity {
defaultRepository {
userIdentity {
displayName
avatarUrl
}
}
}

View File

@ -1,45 +0,0 @@
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;

View File

@ -0,0 +1,31 @@
import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import { makeStyles } from '@material-ui/core/styles';
import { useCurrentIdentityQuery } from './CurrentIdentity.generated';
const useStyles = makeStyles(theme => ({
displayName: {
marginLeft: theme.spacing(2),
},
}));
const CurrentIdentity = () => {
const classes = useStyles();
const { loading, error, data } = useCurrentIdentityQuery();
if (error || loading || !data?.defaultRepository?.userIdentity)
return null;
const user = data.defaultRepository.userIdentity;
return (
<>
<Avatar src={user.avatarUrl ? user.avatarUrl : undefined}>
{user.displayName.charAt(0).toUpperCase()}
</Avatar>
<div className={classes.displayName}>{user.displayName}</div>
</>
);
};
export default CurrentIdentity;

View File

@ -1,8 +1,9 @@
import Tooltip from '@material-ui/core/Tooltip/Tooltip'; import Tooltip from '@material-ui/core/Tooltip/Tooltip';
import * as moment from 'moment'; import moment from 'moment';
import React from 'react'; import React from 'react';
const Date = ({ date }) => ( type Props = { date: string };
const Date = ({ date }: Props) => (
<Tooltip title={moment(date).format('MMMM D, YYYY, h:mm a')}> <Tooltip title={moment(date).format('MMMM D, YYYY, h:mm a')}>
<span> {moment(date).fromNow()} </span> <span> {moment(date).fromNow()} </span>
</Tooltip> </Tooltip>

View File

@ -1,24 +1,27 @@
import React from 'react'; import React from 'react';
import { makeStyles } from '@material-ui/styles'; import { makeStyles } from '@material-ui/core/styles';
import { import {
getContrastRatio, getContrastRatio,
darken, darken,
} from '@material-ui/core/styles/colorManipulator'; } from '@material-ui/core/styles/colorManipulator';
import { common } from '@material-ui/core/colors'; import { common } from '@material-ui/core/colors';
import { Color } from './gqlTypes';
import { LabelFragment } from './Label.generated';
// Minimum contrast between the background and the text color // Minimum contrast between the background and the text color
const contrastThreshold = 2.5; const contrastThreshold = 2.5;
// Guess the text color based on the background color // Guess the text color based on the background color
const getTextColor = background => const getTextColor = (background: string) =>
getContrastRatio(background, common.white) >= contrastThreshold getContrastRatio(background, common.white) >= contrastThreshold
? common.white // White on dark backgrounds ? common.white // White on dark backgrounds
: common.black; // And black on light ones : common.black; // And black on light ones
const _rgb = color => 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')'; const _rgb = (color: Color) => 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';
// Create a style object from the label RGB colors // Create a style object from the label RGB colors
const createStyle = color => ({ const createStyle = (color: Color) => ({
backgroundColor: _rgb(color), backgroundColor: _rgb(color),
color: getTextColor(_rgb(color)), color: getTextColor(_rgb(color)),
borderBottomColor: darken(_rgb(color), 0.2), borderBottomColor: darken(_rgb(color), 0.2),
@ -29,7 +32,7 @@ const useStyles = makeStyles(theme => ({
...theme.typography.body1, ...theme.typography.body1,
padding: '1px 6px 0.5px', padding: '1px 6px 0.5px',
fontSize: '0.9em', fontSize: '0.9em',
fontWeight: '500', fontWeight: 500,
margin: '0.05em 1px calc(-1.5px + 0.05em)', margin: '0.05em 1px calc(-1.5px + 0.05em)',
borderRadius: '3px', borderRadius: '3px',
display: 'inline-block', display: 'inline-block',
@ -38,7 +41,8 @@ const useStyles = makeStyles(theme => ({
}, },
})); }));
function Label({ label }) { type Props = { label: LabelFragment };
function Label({ label }: Props) {
const classes = useStyles(); const classes = useStyles();
return ( return (
<span className={classes.label} style={createStyle(label.color)}> <span className={classes.label} style={createStyle(label.color)}>

View File

@ -17,7 +17,11 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "noEmit": true,
"jsx": "react" "jsx": "react",
"typeRoots": [
"node_modules/@types/",
"types/"
]
}, },
"include": [ "include": [
"src" "src"

6
webui/types/remark-html/index.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
declare module "remark-html" {
import { Plugin } from 'unified';
const plugin: Plugin;
export default plugin;
};

6
webui/types/remark-react/index.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
declare module "remark-react" {
import { Plugin } from 'unified';
const plugin: Plugin;
export default plugin;
};