-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
160 lines (153 loc) · 4.46 KB
/
index.tsx
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
/**
* Copyright 2024 Shinami Corp.
* SPDX-License-Identifier: Apache-2.0
*/
import Canvas from "@/lib/components/Canvas";
import { Divider, HeroCard, NewHeroCard } from "@/lib/components/Elements";
import { useHeroesSample } from "@/lib/hooks/api";
import { useParsedSuiOwnedObjects } from "@/lib/hooks/sui";
import {
HERO_MOVE_TYPE,
Hero,
LEVEL_UP_TICKET_MOVE_TYPE,
LevelUpTicket,
} from "@/lib/shared/hero";
import { AuthContext } from "@/lib/shared/zklogin";
import {
Box,
Button,
Flex,
HStack,
Heading,
ScaleFade,
Text,
VStack,
Link as ChakraLink,
} from "@chakra-ui/react";
import {
AUTH_API_BASE,
LOGIN_PAGE_PATH,
ZkLoginUser,
} from "@shinami/nextjs-zklogin";
import { useZkLoginSession } from "@shinami/nextjs-zklogin/client";
import Link from "next/link";
export default function Home() {
const { user, isLoading: isLoadingUser } = useZkLoginSession<AuthContext>();
return (
<Canvas image="/hero-select-bg.jpg" user={user}>
<Flex flexDir="column" align="center" gap={2}>
{user && <UserHome user={user} />}
{!user && !isLoadingUser && <AnonymousHome />}
</Flex>
</Canvas>
);
}
const AnonymousHome = () => {
const { data: heroSamples } = useHeroesSample();
return (
<>
<VStack gap="50px">
<Heading size="3xl">Latest Heroes</Heading>
<ScaleFade
initialScale={0.95}
transition={{ enter: { duration: 1 } }}
in
>
<HStack gap="82px">
{heroSamples?.map((hero) => (
<Link key={hero.id.id} href={`/heroes/${hero.id.id}`}>
<HeroCard name={hero.name} character={hero.character} />
</Link>
))}
</HStack>
</ScaleFade>
</VStack>
<VStack width="1028px" gap="50px" mt="70px">
<Divider />
<VStack gap="22px">
<Link href={LOGIN_PAGE_PATH}>
<Button variant="solid">
<Box transform="skew(10deg)">Create your own!</Box>
</Button>
</Link>
</VStack>
</VStack>
</>
);
};
const UserHome = ({ user }: { user: ZkLoginUser<AuthContext> }) => {
const { data: heroes, isLoading: isLoadingHeroes } = useParsedSuiOwnedObjects(
user.wallet,
HERO_MOVE_TYPE,
Hero,
);
const { data: levelUpTickets } = useParsedSuiOwnedObjects(
user.wallet,
LEVEL_UP_TICKET_MOVE_TYPE,
LevelUpTicket,
);
if (isLoadingHeroes) return <Text fontSize="30px">Loading heroes...</Text>;
if (heroes)
return (
<>
{heroes.length === 0 && (
<VStack gap="50px">
<Heading size="3xl">No Heroes yet</Heading>
<Link href="/heroes/new">
<ScaleFade
initialScale={0.95}
transition={{ enter: { duration: 1 } }}
in
>
<NewHeroCard />
</ScaleFade>
</Link>
</VStack>
)}
{heroes.length > 0 && (
<VStack gap="50px">
<Heading size="3xl">My Heroes</Heading>
<ScaleFade
initialScale={0.95}
transition={{ enter: { duration: 1 } }}
in
>
<HStack gap="82px">
{heroes.map((hero) => {
const levelup = levelUpTickets?.find(
(ticket) => ticket.hero_id === hero.id.id,
);
return (
<Link key={hero.id.id} href={`/heroes/${hero.id.id}`}>
<HeroCard
name={hero.name}
character={hero.character}
hasLevelUpPoints={!!levelup}
/>
</Link>
);
})}
</HStack>
</ScaleFade>
</VStack>
)}
<VStack width="1028px" gap="50px" mt="70px">
<Divider />
<VStack gap="22px">
<Link href="/heroes/new">
<Button isDisabled={heroes?.length >= 3} variant="solid">
{heroes?.length >= 3 ? (
<Box transform="skew(10deg)">Hero limit reached</Box>
) : (
<Box transform="skew(10deg)">Create new hero</Box>
)}
</Button>
</Link>
<Link href={`${AUTH_API_BASE}/logout`}>
<Button variant="ghost">Sign out</Button>
</Link>
</VStack>
</VStack>
</>
);
};