forked from guildxyz/guild.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
217 lines (193 loc) · 6.64 KB
/
index.tsx
File metadata and controls
217 lines (193 loc) · 6.64 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
Center,
Circle,
Flex,
GridItem,
Heading,
HStack,
SimpleGrid,
Spinner,
Stack,
Tag,
Text,
useColorMode,
} from "@chakra-ui/react"
import { useWeb3React } from "@web3-react/core"
import AddCard from "components/common/AddCard"
import Layout from "components/common/Layout"
import useUpvoty from "components/common/Layout/components/InfoMenu/hooks/useUpvoty"
import Link from "components/common/Link"
import LinkPreviewHead from "components/common/LinkPreviewHead"
import CategorySection from "components/index/CategorySection"
import ExplorerCardMotionWrapper from "components/index/ExplorerCardMotionWrapper"
import GuildCard from "components/index/GuildCard"
import useUsersGuildsRolesIds from "components/index/hooks/useUsersGuildsRolesIds"
import OrderSelect, { OrderOptions } from "components/index/OrderSelect"
import SearchBar from "components/index/SearchBar"
import { useQueryState } from "hooks/useQueryState"
import useScrollEffect from "hooks/useScrollEffect"
import { GetStaticProps } from "next"
import dynamic from "next/dynamic"
import { useEffect, useMemo, useRef, useState } from "react"
import useSWR from "swr"
import { GuildBase } from "types"
import fetcher from "utils/fetcher"
const AnimatedLogo = dynamic(() => import("components/index/AnimatedLogo"), {
ssr: false,
loading: () => <Circle size={{ base: 12, lg: 14 }} mr={-3} />,
})
const BATCH_SIZE = 24
type Props = {
guilds: GuildBase[]
}
const Page = ({ guilds: guildsInitial }: Props): JSX.Element => {
const { account } = useWeb3React()
const [search, setSearch] = useQueryState<string>("search", undefined)
const [order, setOrder] = useQueryState<OrderOptions>("order", "members")
const query = new URLSearchParams({ order, ...(search && { search }) }).toString()
const [guilds, setGuilds] = useState(guildsInitial)
const [renderedGuildsCount, setRenderedGuildsCount] = useState(BATCH_SIZE)
useEffect(() => {
setRenderedGuildsCount(BATCH_SIZE)
}, [search])
const guildsListEl = useRef(null)
useScrollEffect(() => {
if (
!guildsListEl.current ||
guildsListEl.current.getBoundingClientRect().bottom > window.innerHeight ||
guilds?.length <= renderedGuildsCount
)
return
setRenderedGuildsCount((prevValue) => prevValue + BATCH_SIZE)
})
const renderedGuilds = useMemo(
() => guilds?.slice(0, renderedGuildsCount) || [],
[guilds, renderedGuildsCount]
)
const { data: guildsData, isValidating: isLoading } = useSWR(`/guild?${query}`, {
dedupingInterval: 60000, // one minute
})
useEffect(() => {
if (guildsData) setGuilds(guildsData)
}, [guildsData])
const [usersGuilds, setUsersGuilds] = useState<GuildBase[]>([])
const { data: usersGuildsData, isValidating: isUsersLoading } = useSWR(
account ? `/guild/address/${account}?${query}` : null,
{
dedupingInterval: 60000, // one minute
}
)
useEffect(() => {
if (usersGuildsData) setUsersGuilds(usersGuildsData)
}, [usersGuildsData])
const { usersGuildsIds } = useUsersGuildsRolesIds()
// Setting up the dark mode, because this is a "static" page
const { setColorMode } = useColorMode()
useEffect(() => {
setColorMode("dark")
}, [])
const { isRedirecting, upvotyAuthError } = useUpvoty()
if (isRedirecting)
return (
<Flex alignItems="center" justifyContent="center" direction="column" h="100vh">
<Heading mb={4} fontFamily="display">
Guild - Upvoty authentication
</Heading>
{upvotyAuthError ? (
<Text as="span" fontSize="lg">
You are not a member of any guilds. Please <Link href="/">join one</Link>{" "}
and you can vote on the roadmap!
</Text>
) : (
<HStack>
<Spinner size="sm" />
<Text as="span" fontSize="lg">
Redirecting, please wait...
</Text>
</HStack>
)}
</Flex>
)
return (
<>
<LinkPreviewHead path="" />
<Layout
title="Guild"
description="A place for Web3 guilds"
image={<AnimatedLogo />}
>
<SimpleGrid
templateColumns={{ base: "auto 50px", md: "1fr 1fr 1fr" }}
gap={{ base: 2, md: "6" }}
mb={16}
>
<GridItem colSpan={{ base: 1, md: 2 }}>
<SearchBar placeholder="Search guilds" {...{ search, setSearch }} />
</GridItem>
<OrderSelect {...{ isLoading, order, setOrder }} />
</SimpleGrid>
<Stack ref={guildsListEl} spacing={12}>
<CategorySection
title={
usersGuildsIds?.length
? "Your guilds"
: "You're not part of any guilds yet"
}
titleRightElement={isUsersLoading && <Spinner size="sm" />}
fallbackText={`No results for ${search}`}
>
{usersGuildsIds?.length ? (
usersGuilds.length &&
usersGuilds
.map((guild) => (
<ExplorerCardMotionWrapper key={guild.urlName}>
<GuildCard guildData={guild} />
</ExplorerCardMotionWrapper>
))
.concat(
<ExplorerCardMotionWrapper key="create-guild">
<AddCard text="Create guild" link="/create-guild" />
</ExplorerCardMotionWrapper>
)
) : (
<ExplorerCardMotionWrapper key="create-guild">
<AddCard text="Create guild" link="/create-guild" />
</ExplorerCardMotionWrapper>
)}
</CategorySection>
<CategorySection
title="All guilds"
titleRightElement={
isLoading ? (
<Spinner size="sm" />
) : (
<Tag size="sm">{guilds.length}</Tag>
)
}
fallbackText={
search?.length
? `No results for ${search}`
: "Can't fetch guilds from the backend right now. Check back later!"
}
>
{renderedGuilds.length &&
renderedGuilds.map((guild) => (
<ExplorerCardMotionWrapper key={guild.urlName}>
<GuildCard guildData={guild} />
</ExplorerCardMotionWrapper>
))}
</CategorySection>
<Center>{guilds?.length > renderedGuildsCount && <Spinner />}</Center>
</Stack>
</Layout>
</>
)
}
export const getStaticProps: GetStaticProps = async () => {
const guilds = await fetcher(`/guild?sort=members`).catch((_) => [])
return {
props: { guilds },
revalidate: 10,
}
}
export default Page