You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.3 KiB

4 years ago
import { AddIcon } from "@chakra-ui/icons"
import {
Box, Button, chakra, Flex, HStack, VStack, Menu,
MenuButton,
MenuList,
MenuItem,
IconButton,
Heading,
Divider
} from "@chakra-ui/react"
4 years ago
import Card, { CardBody, CardHeader } from "components/card"
4 years ago
import Stories from "components/story/stories"
import SimplePostCard from "components/story/simple-story-card"
import SEO from "components/seo"
import siteConfig from "configs/site-config"
4 years ago
import PageContainer1 from "layouts/page-container1"
import React, { useEffect, useState } from "react"
import { requestApi } from "utils/axios/request"
4 years ago
import StoryFilters from "components/story/story-filter"
import { concat } from "lodash"
import useInfiniteScroll from 'src/hooks/use-infinite-scroll'
4 years ago
import { HomeSidebar } from "src/types/misc"
import Link from "next/link"
import { ReserveUrls } from "src/data/reserve-urls"
4 years ago
const HomePage = () => {
4 years ago
const [filter, setFilter] = useState('Best')
4 years ago
const initData = (p) => {
return requestApi.get(`/story/posts/home?filter=${filter}&page=${p}&per_page=5`)
4 years ago
}
4 years ago
const onFilterChange = f => {
setFilter(f)
4 years ago
}
4 years ago
return (
<>
<SEO
title={siteConfig.seo.title}
description={siteConfig.seo.description}
/>
<PageContainer1>
<HStack alignItems="top" spacing="3">
4 years ago
<VStack alignItems="left" width={["100%", "100%", "70%", "70%"]} spacing="3">
4 years ago
<Card p="2">
<Flex justifyContent="space-between" alignItems="center">
4 years ago
<StoryFilters onChange={onFilterChange} />
4 years ago
<Menu>
<MenuButton
as={IconButton}
aria-label="Options"
icon={<svg fill="none" stroke="currentColor" opacity="0.75" height="1.3rem" viewBox="0 0 55 55"><path d="M2 2h51v21H2V2zm0 30h51v21H2V32z" stroke="stroke-current" strokeWidth="4"></path></svg>}
size="xs"
variant="ghost"
_focus={null}
/>
<MenuList>
<MenuItem icon={<svg fill="none" stroke="currentColor" opacity="0.75" height="1.3rem" viewBox="0 0 55 55"><path d="M2 2h51v21H2V2zm0 30h51v21H2V32z" stroke="stroke-current" strokeWidth="4"></path></svg>}>
Modern
</MenuItem>
<MenuItem icon={<svg stroke="currentColor" height="1.2rem" viewBox="0 0 55 55" fill="none"><path d="M2 2h51v11H2V2zm0 40h51v11H2V42zm0-20h51v11H2V22z" stroke="stoke-current" strokeWidth="4"></path></svg>}>
Compact
</MenuItem>
</MenuList>
</Menu>
</Flex>
</Card>
4 years ago
<Card width="100%" height="fit-content" p="0">
4 years ago
<Stories onLoad={initData} filter={filter} />
4 years ago
</Card>
</VStack>
4 years ago
<IndexSidebar />
4 years ago
</HStack>
</PageContainer1>
</>
)
}
export default HomePage
4 years ago
4 years ago
export const IndexSidebar = () => {
const [sidebars, setSidebars]: [HomeSidebar[], any] = useState([])
const getSidebars = async () => {
const res = await requestApi.get("/sidebars")
setSidebars(res.data)
}
4 years ago
4 years ago
useEffect(() => {
getSidebars()
}, [])
4 years ago
return (
<VStack alignItems="left" width="30%" display={{ base: "none", md: "flex" }}>
4 years ago
{
sidebars.map(sb =>
<IndexSidebarCard sidebar={sb} />
)
}
</VStack>
)
}
const IndexSidebarCard = ({ sidebar }) => {
const [posts, setPosts] = useState([])
useEffect(() => {
initData()
}, [])
const initData = async () => {
const res = await requestApi.get(`/tag/info/${sidebar.tagName}`)
const res1 = await requestApi.get(`/tag/posts/${res.data.id}?filter=${sidebar.sort}&page=${1}&per_page=${sidebar.displayCount}`)
setPosts(res1.data)
}
return (
<>
{posts.length > 0 && <Card p="0">
4 years ago
<CardHeader>
4 years ago
<Link href={`${ReserveUrls.Tags}/${sidebar.tagName}`}><Heading size="sm" cursor="pointer">#{sidebar.tagName}</Heading></Link>
4 years ago
</CardHeader>
4 years ago
<Divider />
4 years ago
<CardBody>
4 years ago
<VStack alignItems="left" mt="1">
{
posts.map(p => <Box mb="1"><SimplePostCard story={p} /></Box>)
}
</VStack>
4 years ago
</CardBody>
4 years ago
</Card>}
</>
4 years ago
)
}