index.tsx
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useEffect, useState } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/dist/client/router';
import { Layout, Menu } from 'antd';
import { MenuOutlined } from '@ant-design/icons';
import { useQuery } from '@apollo/client';
import { GET_ALL_POSTS } from '@src/gql/get-all-posts';
const { Header: HeaderContainer } = Layout;
export default function Header() {
const [selected, setSelected] = useState(null);
const { query } = useRouter();
const { error, data } = useQuery(GET_ALL_POSTS);
if (error) console.log(JSON.stringify(error, null, 2));
let list = [];
let getAllPosts = data?.getAllPosts || [];
getAllPosts.forEach((post) => {
if (!list.find((category) => post.category === category)) {
list.push(post.category);
}
});
useEffect(() => {
setSelected(query.name || '/');
}, [query]);
return (
<HeaderContainer className={'app-header'}>
<Menu
overflowedIndicator={<MenuOutlined className={'header-hbg-menu'} />}
theme={'dark'}
mode={'horizontal'}
selectedKeys={[selected]}
>
{/* logo */}
<Menu.Item key={'/'}>
<Link href={'/'}>
<a
style={{
float: 'left',
width: '120px',
height: '31px',
margin: '16px 24px 16px 0',
background: 'rgba(255, 255, 255, 0.3)',
}}
/>
</Link>
</Menu.Item>
{list.map((item) => (
<Menu.Item key={item} className={'header-item'}>
<Link href={`/category/${item}`}>
<a>{item}</a>
</Link>
</Menu.Item>
))}
</Menu>
</HeaderContainer>
);
}