|
| 1 | +/* |
| 2 | + this page is for /explore |
| 3 | + */ |
1 | 4 | import { Provider } from 'mobx-react' |
2 | | -import { GetStaticPaths, GetStaticProps } from 'next' |
3 | | -import { merge, toLower } from 'ramda' |
4 | | - |
5 | | -// import { PAGE_SIZE } from '@/config' |
6 | | -import { HCN, THREAD, METRIC } from '@/constant' |
7 | | -import { useStore } from '@/stores/init' |
| 5 | +import { clone } from 'ramda' |
| 6 | +import { METRIC } from '@/constant' |
8 | 7 |
|
| 8 | +import { PAGE_SIZE } from '@/config' |
9 | 9 | import { |
10 | | - isArticleThread, |
11 | | - ssrPagedArticleSchema, |
12 | | - isrPagedArticlesFilter, |
13 | | - ssrParseArticleThread, |
14 | | - communitySEO, |
15 | | - singular, |
16 | | - makeGQClient, |
| 10 | + ssrBaseStates, |
| 11 | + ssrFetchPrepare, |
| 12 | + ssrGetParam, |
| 13 | + refreshIfneed, |
| 14 | + exploreSEO, |
| 15 | + ssrError, |
17 | 16 | } from '@/utils' |
18 | 17 |
|
| 18 | +import { useStore } from '@/stores/init' |
| 19 | + |
19 | 20 | import GlobalLayout from '@/containers/layout/GlobalLayout' |
20 | | -import CommunityContent from '@/containers/content/CommunityContent' |
| 21 | +import ExploreContent from '@/containers/content/ExploreContent' |
21 | 22 |
|
22 | 23 | import { P } from '@/schemas' |
23 | 24 |
|
24 | | -const loader = async () => { |
25 | | - const gqClient = makeGQClient('') |
| 25 | +const loader = async (context, opt = {}) => { |
| 26 | + const { gqClient, userHasLogin } = ssrFetchPrepare(context, opt) |
26 | 27 |
|
27 | | - // 线上环境会直接跳过 index 到这里,有待排查。。 |
28 | | - const community = HCN |
29 | | - const thread = THREAD.POST |
| 28 | + const category = ssrGetParam(context, 'nc_path') |
| 29 | + const page = ssrGetParam(context, 'page') |
30 | 30 |
|
31 | | - // query data |
32 | | - const curCommunity = gqClient.request(P.community, { |
33 | | - raw: community, |
34 | | - userHasLogin: false, |
35 | | - }) |
| 31 | + const filter = { |
| 32 | + page: 1, |
| 33 | + size: PAGE_SIZE.M, |
| 34 | + } |
36 | 35 |
|
37 | | - const pagedArticleTags = gqClient.request(P.pagedArticleTags, { |
38 | | - filter: { communityRaw: community, thread: singular(thread, 'upperCase') }, |
39 | | - }) |
| 36 | + const communitiesFilter = clone(filter) |
| 37 | + // @ts-ignore |
| 38 | + if (category) communitiesFilter.category = category |
| 39 | + if (page) communitiesFilter.page = parseInt(page, 10) |
40 | 40 |
|
41 | | - const filter = isrPagedArticlesFilter({}) |
| 41 | + const sessionState = gqClient.request(P.sessionState) |
| 42 | + const pagedCommunities = gqClient.request(P.pagedCommunities, { |
| 43 | + filter: communitiesFilter, |
| 44 | + userHasLogin, |
| 45 | + }) |
| 46 | + const pagedCategories = gqClient.request(P.pagedCategories, { filter }) |
42 | 47 |
|
43 | | - const pagedArticles = isArticleThread(thread) |
44 | | - ? gqClient.request(ssrPagedArticleSchema(thread), filter) |
45 | | - : {} |
| 48 | + const subscribedCommunities = gqClient.request(P.subscribedCommunities, { |
| 49 | + filter: { |
| 50 | + page: 1, |
| 51 | + size: 30, |
| 52 | + }, |
| 53 | + }) |
46 | 54 |
|
47 | 55 | return { |
48 | | - filter, |
49 | | - ...(await pagedArticleTags), |
50 | | - ...(await curCommunity), |
51 | | - ...(await pagedArticles), |
| 56 | + ...(await sessionState), |
| 57 | + ...(await pagedCategories), |
| 58 | + ...(await pagedCommunities), |
| 59 | + ...(await subscribedCommunities), |
52 | 60 | } |
53 | 61 | } |
54 | 62 |
|
55 | | -export const getStaticProps: GetStaticProps = async () => { |
56 | | - console.log('index params: ') |
57 | | - |
58 | | - const thread = THREAD.POST |
59 | | - const resp = await loader() |
60 | | - |
61 | | - const { filter, community, pagedArticleTags } = resp |
62 | | - // console.log('iii got resp: ', resp) |
63 | | - const articleThread = ssrParseArticleThread(resp, thread, filter) |
64 | | - |
65 | | - const initProps = merge( |
66 | | - { |
67 | | - // ...ssrBaseStates(resp), |
68 | | - route: { |
69 | | - communityPath: community.raw, |
70 | | - mainPath: community.raw === HCN ? '' : community.raw, |
71 | | - subPath: thread === THREAD.POST ? '' : thread, |
72 | | - thread, |
73 | | - }, |
74 | | - tagsBar: { |
75 | | - tags: pagedArticleTags.entries, |
76 | | - }, |
77 | | - viewing: { |
78 | | - community, |
79 | | - activeThread: toLower(thread), |
80 | | - }, |
| 63 | +export const getServerSideProps = async (context) => { |
| 64 | + let resp |
| 65 | + try { |
| 66 | + resp = await loader(context) |
| 67 | + const { sessionState } = resp |
| 68 | + |
| 69 | + refreshIfneed(sessionState, '/explore', context) |
| 70 | + } catch (e) { |
| 71 | + console.log('#### error from server: ', e) |
| 72 | + return ssrError(context, 'fetch', 500) |
| 73 | + } |
| 74 | + |
| 75 | + const { pagedCategories, pagedCommunities } = resp |
| 76 | + |
| 77 | + const initProps = { |
| 78 | + ...ssrBaseStates(resp), |
| 79 | + exploreContent: { |
| 80 | + pagedCommunities, |
| 81 | + pagedCategories, |
81 | 82 | }, |
82 | | - articleThread, |
83 | | - ) |
| 83 | + } |
84 | 84 |
|
85 | | - return { props: { errorCode: null, ...initProps }, revalidate: 10 } |
| 85 | + return { props: { errorCode: null, ...initProps } } |
86 | 86 | } |
87 | 87 |
|
88 | | -const CommunityPage = (props) => { |
| 88 | +const ExplorePage = (props) => { |
89 | 89 | const store = useStore(props) |
90 | | - |
91 | | - const { viewing } = store |
92 | | - const { community, activeThread } = viewing |
| 90 | + const seoConfig = exploreSEO() |
93 | 91 |
|
94 | 92 | return ( |
95 | 93 | <Provider store={store}> |
96 | | - <GlobalLayout |
97 | | - metric={METRIC.COMMUNITY} |
98 | | - seoConfig={communitySEO(community, activeThread)} |
99 | | - > |
100 | | - <CommunityContent /> |
| 94 | + <GlobalLayout metric={METRIC.EXPLORE} seoConfig={seoConfig} noSidebar> |
| 95 | + <ExploreContent /> |
101 | 96 | </GlobalLayout> |
102 | 97 | </Provider> |
103 | 98 | ) |
104 | 99 | } |
105 | 100 |
|
106 | | -export default CommunityPage |
| 101 | +export default ExplorePage |
0 commit comments