Skip to content

Commit

Permalink
implement lexicographic navigation (#68)
Browse files Browse the repository at this point in the history
* feat: lexicographic navigation

* fix: delete seperate style

* style: remove underline & hide under small screen

---------

Co-authored-by: NCJ <[email protected]>
  • Loading branch information
OrkWard and iamNCJ committed Jul 17, 2023
1 parent c6b0b86 commit 8e2f6f5
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 136 deletions.
50 changes: 50 additions & 0 deletions src/components/nav-bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import { Link, Grid, Hidden } from '@mui/material';
import { MirrorDto } from '../types/mirror';

export default ({ data }: { data: MirrorDto[] }) => {
const caps: { [key: string]: boolean } = {};

data.forEach(mirror => {
if (mirror.id.length < 0) {
throw new Error('mirror id empty');
}
const cap = mirror.id[0].toLocaleUpperCase();

caps[cap] = true;
});

const buttons: React.ReactNode[] = [];

for (let i = 'A'.charCodeAt(0); i <= 'Z'.charCodeAt(0); i += 1) {
const char = String.fromCharCode(i);
if (caps[char]) {
buttons.push(
<Link href={`#${char}`} style={{ textDecoration: 'none' }}>
{char}
</Link>
);
};
}

return (
<Hidden smDown>
<Grid
container
flexDirection="column"
flexWrap="nowrap"
spacing={1}
sx={{
position: 'fixed',
bottom: '1rem',
right: '1rem',
width: 'fit-content',
}}
>
{buttons.map(fab => (
<Grid item>{fab}</Grid>
))}
</Grid>
</Hidden>
);
};
64 changes: 46 additions & 18 deletions src/components/search-table.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
import React from 'react';
import { Box, Grid } from '@mui/material';
import { Grid, Typography } from '@mui/material';
import SearchItemCard from './search-item-card';
import { Mirror } from '../types/mirror';

export default (props: { queryResults: Mirror[] }) => (
<Box sx={{ display: 'flex', justifyContent: 'center' }}>
export default (props: { queryResults: Mirror[] }) => {
const lexicoMap: { [key: string]: Mirror[] } = {};
const caps: string[] = [];
props.queryResults.forEach(mirror => {
if (mirror.id.length < 0) {
throw new Error('mirror id empty');
}
const cap = mirror.id[0].toLocaleUpperCase();

if (lexicoMap[cap]) {
lexicoMap[cap].push(mirror);
} else {
caps.push(cap);
lexicoMap[cap] = [mirror];
}
});
caps.sort();

return (
<Grid
container
spacing={{ xs: 2 }}
columns={{ xs: 1, sm: 2, md: 3, lg: 4 }}
alignItems="stretch"
spacing={2}
columns={1}
flexDirection="column"
wrap="nowrap"
>
{
// sort by queryResults.id
props.queryResults
?.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase()))
.map((item, i) => (
<Grid item xs={1} key={i}>
<SearchItemCard queryItem={item} />
</Grid>
))
}
{caps.map(cap => (
<Grid item>
<Typography gutterBottom variant="h6" component="div" id={cap}>
{cap}
</Typography>
<Grid
container
spacing={{ xs: 2 }}
columns={{ xs: 1, sm: 2, md: 3, lg: 4 }}
alignItems="stretch"
key={cap}
>
{lexicoMap[cap].map(mirror => (
<Grid item xs={1} key={mirror.id}>
<SearchItemCard queryItem={mirror} />
</Grid>
))}
</Grid>
</Grid>
))}
</Grid>
</Box>
);
);
};
241 changes: 123 additions & 118 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Footer from '../components/footer';
import FrequentlyUsedMirrorCard from '../components/frequently-used-mirror-card';
import LanguageIconButton from '../components/language-icon-button';
import SearchTable from '../components/search-table';
import NavBar from '../components/nav-bar';
import Seo from '../components/seo';
import ThemeIconButton from '../components/theme-icon-button';
import { Mirror, MirrorDto } from '../types/mirror';
Expand Down Expand Up @@ -133,140 +134,144 @@ const Index = ({ data }: { data: Data }) => {
);

return (
<Box
sx={{
minHeight: '100vh',
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between',
}}
>
<Seo title="ZJU Mirror" />
<Grid
container
spacing={{ xs: 6 }}
columns={{ xs: 1 }}
sx={{ px: { xs: 4, sm: 8 }, py: 8 }}
<>
<Box
sx={{
minHeight: '100vh',
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between',
scrollBehavior: 'smooth',
}}
>
<Grid item xs={1}>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
}}
>
<Seo title="ZJU Mirror" />
<Grid
container
spacing={{ xs: 6 }}
columns={{ xs: 1 }}
sx={{ px: { xs: 4, sm: 8 }, py: 8 }}
>
<Grid item xs={1}>
<Box
sx={{
minWidth: { xs: 54, sm: 72 },
maxWidth: { xs: 54, sm: 72 },
display: 'flex',
flexDirection: 'row',
}}
>
<ZjuFalconIcon />
</Box>
<Box sx={{ width: '100%', ml: 2 }}>
<Grid
container
direction="row"
justifyContent="space-between"
alignItems="center"
>
<Grid item>
<Typography
variant="h1"
component="div"
color="primary"
sx={{ fontSize: { xs: 48, sm: 64 }, mt: -1 }}
>
<Trans>ZJU Mirror</Trans>
</Typography>
</Grid>
<Grid item sx={{ display: { xs: 'none', sm: 'block' } }}>
<NameIconButton />
<LanguageIconButton />
<ThemeIconButton />
</Grid>
</Grid>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
mt: { xs: -1, sm: -2 },
minWidth: { xs: 54, sm: 72 },
maxWidth: { xs: 54, sm: 72 },
}}
>
<Typography
variant="subtitle1"
component="div"
color="primary"
sx={{ fontSize: { xs: 21, sm: 28 } }}
<ZjuFalconIcon />
</Box>
<Box sx={{ width: '100%', ml: 2 }}>
<Grid
container
direction="row"
justifyContent="space-between"
alignItems="center"
>
<Trans>浙江大学开源软件镜像站</Trans>
</Typography>
<Typography
variant="subtitle1"
component="div"
color="primary"
sx={{ ml: 1 }}
<Grid item>
<Typography
variant="h1"
component="div"
color="primary"
sx={{ fontSize: { xs: 48, sm: 64 }, mt: -1 }}
>
<Trans>ZJU Mirror</Trans>
</Typography>
</Grid>
<Grid item sx={{ display: { xs: 'none', sm: 'block' } }}>
<NameIconButton />
<LanguageIconButton />
<ThemeIconButton />
</Grid>
</Grid>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
mt: { xs: -1, sm: -2 },
}}
>
<Chip
size="medium"
label={t(networkMap[networkMode].text)}
color={networkMap[networkMode].color}
sx={{ display: { xs: 'none', sm: 'grid' } }}
/>
</Typography>
<Typography
variant="subtitle1"
component="div"
color="primary"
sx={{ fontSize: { xs: 21, sm: 28 } }}
>
<Trans>浙江大学开源软件镜像站</Trans>
</Typography>
<Typography
variant="subtitle1"
component="div"
color="primary"
sx={{ ml: 1 }}
>
<Chip
size="medium"
label={t(networkMap[networkMode].text)}
color={networkMap[networkMode].color}
sx={{ display: { xs: 'none', sm: 'grid' } }}
/>
</Typography>
</Box>
</Box>
</Box>
</Box>
</Grid>
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>常用镜像</Trans>
</Typography>
<Grid container spacing={{ xs: 2 }} columns={{ xs: 1, sm: 3, md: 6 }}>
{frequentlyUsedMirror.map((e, i) => {
const mirror = mirrors[e.id];
return (
mirror && (
<Grid item xs={1} key={i}>
<FrequentlyUsedMirrorCard
name={mirror.name[language]}
desc={mirror.desc[language]}
icon={e.icon}
url={getUrl(mirror.docUrl || mirror.url, !!mirror.docUrl)}
/>
</Grid>
)
);
})}
</Grid>
</Grid>
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>近期更新</Trans>
</Typography>
<Grid>
{newsUrls.map(([title, date, url], _) => (
<Grid container>
<Link href={url} underline="hover">
{title}
</Link>
<Typography color="info.light" component="div">
&nbsp; - {date.toLocaleDateString()}
</Typography>
</Grid>
))}
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>常用镜像</Trans>
</Typography>
<Grid container spacing={{ xs: 2 }} columns={{ xs: 1, sm: 3, md: 6 }}>
{frequentlyUsedMirror.map((e, i) => {
const mirror = mirrors[e.id];
return (
mirror && (
<Grid item xs={1} key={i}>
<FrequentlyUsedMirrorCard
name={mirror.name[language]}
desc={mirror.desc[language]}
icon={e.icon}
url={getUrl(mirror.docUrl || mirror.url, !!mirror.docUrl)}
/>
</Grid>
)
);
})}
</Grid>
</Grid>
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>近期更新</Trans>
</Typography>
<Grid>
{newsUrls.map(([title, date, url], _) => (
<Grid container>
<Link href={url} underline="hover">
{title}
</Link>
<Typography color="info.light" component="div">
&nbsp; - {date.toLocaleDateString()}
</Typography>
</Grid>
))}
</Grid>
</Grid>
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>所有镜像</Trans>
</Typography>
<SearchTable queryResults={Object.values(mirrors)} />
</Grid>
</Grid>
<Grid item xs={1}>
<Typography gutterBottom variant="h5" component="div">
<Trans>所有镜像</Trans>
</Typography>
<SearchTable queryResults={Object.values(mirrors)} />
</Grid>
</Grid>
<Footer />
</Box>
<Footer />
</Box>
<NavBar data={Object.values(mirrors)} />
</>
);
};

Expand Down
Loading

0 comments on commit 8e2f6f5

Please sign in to comment.