Skip to content

Commit

Permalink
Merge pull request #3 from surgioproject/dev
Browse files Browse the repository at this point in the history
Artifact 流量信息
  • Loading branch information
geekdada authored Feb 28, 2020
2 parents 6424d0a + 821c1e1 commit 1311651
Show file tree
Hide file tree
Showing 26 changed files with 782 additions and 101 deletions.
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"prepare-ci": "ts-node -P scripts/tsconfig.json scripts/ci-prepare.ts"
},
"devDependencies": {
"@material-ui/core": "^4.8.3",
"@material-ui/core": "^4.9.4",
"@material-ui/icons": "^4.5.1",
"@material-ui/lab": "^4.0.0-alpha.44",
"@nestjs/cli": "^6.9.1",
"@nestjs/schematics": "^6.7.0",
"@nestjs/testing": "^6.8.0",
Expand All @@ -20,14 +21,17 @@
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^8.1.0",
"@types/jest": "^24.9.0",
"@types/loadable__component": "^5.10.0",
"@types/lodash-es": "^4.17.3",
"@types/node": "^10",
"@types/node-fetch": "^2.5.4",
"@types/node-fetch": "^2.5.5",
"@types/nunjucks": "^3.1.3",
"@types/passport-local": "^1.0.33",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router-dom": "^5.1.3",
"@types/react-test-renderer": "^16.9.2",
"@types/semver": "^7.1.0",
"@types/supertest": "^2.0.8",
"abort-controller": "^3.0.0",
"autoprefixer": "^9.7.3",
Expand All @@ -39,11 +43,12 @@
"ky": "^0.17.0",
"ky-universal": "^0.5.0",
"lerna": "^3.20.2",
"light-my-request": "^3.7.1",
"mobx": "^5.15.4",
"mobx-react": "^6.1.4",
"nock": "^12.0.1",
"node-fetch": "^2.6.0",
"notistack": "^0.9.7",
"notistack": "^0.9.8",
"npm-run-all": "^4.1.5",
"postcss-import": "^12.0.1",
"react": "^16.12.0",
Expand All @@ -55,8 +60,8 @@
"rimraf": "^3.0.0",
"sinon": "^9.0.0",
"supertest": "^4.0.2",
"surgio": "^1.12.2",
"swr": "^0.1.16",
"surgio": "^1.13.0",
"swr": "^0.1.18",
"tailwindcss": "^1.1.4",
"ts-jest": "^24.1.0",
"ts-loader": "^6.2.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/gateway-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
"publishConfig": {
"access": "public"
},
"gitHead": "da96fe077b24097d0a721392bd32ec4a88ac2ee0"
"gitHead": "da96fe077b24097d0a721392bd32ec4a88ac2ee0",
"dependencies": {
"@loadable/component": "^5.12.0",
"lodash-es": "^4.17.15"
}
}
Binary file modified packages/gateway-frontend/public/favicon.ico
Binary file not shown.
Binary file modified packages/gateway-frontend/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/gateway-frontend/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions packages/gateway-frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Route,
Link as RouterLink,
LinkProps as RouterLinkProps,
Redirect,
} from 'react-router-dom';
import AppBar from '@material-ui/core/AppBar';
import CssBaseline from '@material-ui/core/CssBaseline';
Expand All @@ -22,21 +23,24 @@ import ListItemText from '@material-ui/core/ListItemText';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Link from '@material-ui/core/Link';
import ListIcon from '@material-ui/icons/List';
import DnsIcon from '@material-ui/icons/Dns';
import SubjectIcon from '@material-ui/icons/Subject';
import MenuIcon from '@material-ui/icons/Menu';
import GitHubIcon from '@material-ui/icons/GitHub';
import { makeStyles, useTheme, Theme, createStyles } from '@material-ui/core/styles';
import loadable from '@loadable/component';

import './App.css';
import { defaultFetcher } from './libs/utils';
import ArtifactListPage from './pages/ArtifactList';
import AuthPage from './pages/Auth';
import HomePage from './pages/Home';
import NotFoundPage from './pages/NotFound';
import { useStores } from './stores';
import { Config } from './stores/config';
import NotFoundPage from './pages/NotFound';

const drawerWidth = 240;
const ArtifactListPage = loadable(() => import('./pages/ArtifactList'), {});
const ProviderListPage = loadable(() => import('./pages/ProviderList'), {});
const HomePage = loadable(() => import('./pages/Home'), {});
const AuthPage = loadable(() => import('./pages/Auth'), {});

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand Down Expand Up @@ -161,7 +165,8 @@ export default observer((props: ResponsiveDrawerProps) => {
</div>
<Divider />
<List>
<ListItemLink to="/list-artifact" primary="Artifacts" icon={<ListIcon />} />
<ListItemLink to="/artifacts" primary="Artifacts" icon={<SubjectIcon />} />
<ListItemLink to="/providers" primary="Providers" icon={<DnsIcon />} />
</List>
</div>
);
Expand Down Expand Up @@ -234,8 +239,14 @@ export default observer((props: ResponsiveDrawerProps) => {
<div className={classes.toolbar} />
<Switch>
<Route path="/list-artifact">
<Redirect to="/artifacts" />
</Route>
<Route path="/artifacts">
<ArtifactListPage />
</Route>
<Route path="/providers">
<ProviderListPage />
</Route>
<Route path="/auth">
<AuthPage />
</Route>
Expand Down
103 changes: 103 additions & 0 deletions packages/gateway-frontend/src/components/ProviderCard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import CardHeader from '@material-ui/core/CardHeader';
import CardContent from '@material-ui/core/CardContent';
import CardActions from '@material-ui/core/CardActions';
import DnsIcon from '@material-ui/icons/Dns';
import Chip from '@material-ui/core/Chip';
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Card from '@material-ui/core/Card';
import { useSnackbar } from 'notistack';

import { Provider } from '../../libs/types';
import { defaultFetcher } from '../../libs/utils';

const useStyles = makeStyles(theme => ({
ProviderCard: {
'& .MuiCardActions-root': {
flexWrap: 'wrap',
},
},
providerType: {
'margin': theme.spacing(0, 0, 2),
},
urlContainer: {
'background-color': '#eee',
'padding': theme.spacing(2, 3),
margin: theme.spacing(2, 0, 0),
'-webkit-overflow-scrolling': 'touch',
'overflow-x': 'scroll',
'font-family': [
'fira-code',
'monospace',
].join(','),
},
actionButton: {
margin: theme.spacing(1),
textDecoration: 'none',
},
}));

export interface ProviderCardProps {
provider: Provider;
}

function ProviderCard({ provider }: ProviderCardProps) {
const classes = useStyles();
const { enqueueSnackbar } = useSnackbar();

const checkSubscription = (providerName: string) => {
(async () => {
const data = await defaultFetcher<any>(`/api/providers/${providerName}/subscription`);

if (data) {
enqueueSnackbar(
`🤟 已用流量:${data.used} 剩余流量:${data.left} 有效期至:${data.expire}`,
{ variant: 'success' });
} else {
enqueueSnackbar('该 Provider 不支持查询', { variant: 'error' });
}
})()
.catch(err => {
enqueueSnackbar('网络问题', { variant: 'error' });
});
};

return (
<Card className={classes.ProviderCard}>
<CardHeader title={provider.name} />

<CardContent>
<div className={classes.providerType}>
<Chip icon={<DnsIcon fontSize="small" />}
label={provider.type} />
</div>

{
provider.url ? (
<Typography className={classes.urlContainer}
component="pre"
paragraph>
{ provider.url }
</Typography>
) : null
}
</CardContent>

<CardActions disableSpacing>
{
provider.supportGetSubscriptionUserInfo ? (
<Button className={classes.actionButton}
variant="contained"
color="primary"
onClick={() => checkSubscription(provider.name)}
>查询流量</Button>
) : null
}
</CardActions>
</Card>
);
}

export default ProviderCard;
115 changes: 115 additions & 0 deletions packages/gateway-frontend/src/components/SubscriptionPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import Paper from '@material-ui/core/Paper';
import { makeStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Skeleton from '@material-ui/lab/Skeleton';
import CircularProgress from '@material-ui/core/CircularProgress';
import Grid from '@material-ui/core/Grid';
import React from 'react';
import useSWR from 'swr';
import uniqWith from 'lodash-es/uniqWith';

import { Provider } from '../../libs/types';
import { defaultFetcher } from '../../libs/utils';

const useStyles = makeStyles(theme => ({
SubscriptionPanel: {
padding: theme.spacing(2),
},
SubscriptionPanelItem: {},
}));

export interface SubscriptionPanelItemProps {
provider: Provider;
}

function SubscriptionPanel() {
const classes = useStyles();
const { data: providerList, error } = useSWR<ReadonlyArray<Provider>>(
'/api/providers',
defaultFetcher,
);

if (error) {
return <div>Failed to load</div>;
}

if (!providerList) {
return <CircularProgress />;
}

const supportedProviderList = uniqWith(
providerList
.filter(provider => provider.supportGetSubscriptionUserInfo && provider.url),
(provider, other) => {
// return new URL(provider.url as string).host === new URL(other.url as string).host;
return provider.url === other.url;
}
);

return (
<Paper className={classes.SubscriptionPanel}>
<Typography gutterBottom variant="h5">订阅</Typography>

<Grid container spacing={3}>
{
supportedProviderList.map((provider: Provider) => {
return (
<SubscriptionPanelItem provider={provider}
key={provider.name} />
);
})
}
</Grid>

</Paper>
);
}

function SubscriptionPanelItem({ provider }: SubscriptionPanelItemProps) {
const classes = useStyles();
const { data, error } = useSWR<any>(
`/api/providers/${provider.name}/subscription`,
defaultFetcher,
);

if (error) {
return <div>Failed to load</div>;
}

if (typeof data === 'undefined') {
return (
<Grid item xs={12} sm={6} lg={4} key={provider.name}>
<Skeleton />
<Skeleton />
<Skeleton />
</Grid>
);
}

if (data === null) {
return (<></>);
}

return (
<Grid item xs={12} sm={6} lg={4} key={provider.name}>
<div className={classes.SubscriptionPanelItem}>
<Typography gutterBottom variant="h6">
{ provider.name }
</Typography>
<div>
<Typography gutterBottom variant="body2">
已用流量:{data.used}
</Typography>
<Typography gutterBottom variant="body2">
剩余流量:{data.left}
</Typography>
<Typography gutterBottom variant="body2">
有效期至:{data.expire}
</Typography>
</div>
</div>
</Grid>
);
}

export default SubscriptionPanel;
6 changes: 6 additions & 0 deletions packages/gateway-frontend/src/libs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Provider {
name: string;
type: string;
url?: string;
supportGetSubscriptionUserInfo: boolean;
}
9 changes: 7 additions & 2 deletions packages/gateway-frontend/src/pages/ArtifactList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import useSWR from 'swr';
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Box from '@material-ui/core/Box';
import Paper from '@material-ui/core/Paper';
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
Expand Down Expand Up @@ -70,11 +71,15 @@ const Page: React.FC = () => {
}, [artifactList]);

if (error) {
return <div>Failed to load</div>;
return <Box display="flex" justifyContent="center">Failed to load</Box>;
}

if (!artifactList) {
return <div>Loading...</div>;
return (
<Box display="flex" justifyContent="center">
Loading...
</Box>
);
}

const handleCategoryChange = (name: string) => (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down
Loading

0 comments on commit 1311651

Please sign in to comment.