Skip to content

Commit

Permalink
react
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Jul 30, 2024
1 parent a8fc802 commit ac84abd
Show file tree
Hide file tree
Showing 61 changed files with 3,506 additions and 1,224 deletions.
12 changes: 10 additions & 2 deletions clients/react-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
"build-storybook": "storybook build"
},
"dependencies": {
"@cartridge/connector": "^0.3.36",
"@lootsurvivor/core": "workspace:^",
"@lootsurvivor/react": "workspace:^",
"@radix-ui/react-slider": "^1.2.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@starknet-react/chains": "^0.1.7",
"@starknet-react/core": "^2.8.3",
"@tanstack/react-query": "^5.51.15",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"framer-motion": "^11.3.19",
"get-starknet-core": "^3.3.2",
"graphql-request": "^7.1.0",
"lucide-react": "^0.416.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"starknet": "^6.11.0",
"tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7",
"@lootsurvivor/core": "workspace:^"
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {
"@chromatic-com/storybook": "^1.6.1",
Expand Down
Binary file added clients/react-vite/public/images/bg-skulls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 0 additions & 42 deletions clients/react-vite/src/App.css
Original file line number Diff line number Diff line change
@@ -1,42 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
18 changes: 10 additions & 8 deletions clients/react-vite/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useState } from "react";
import { LandingPage } from "./components/pages/LandingPage";
import { Button } from "./components/ui/button";

import "./App.css";

import { useGameStateStore } from "@lootsurvivor/core";
import { useSurvivorState } from "./hooks/useSurvivorState";
import { useConnect } from "@starknet-react/core";

function App() {
const [count, setCount] = useState(0);

const { gameState } = useGameStateStore.getState();
const { survivor } = useSurvivorState();

return <></>;
return (
<>
<LandingPage />
</>
);
}

export default App;
123 changes: 123 additions & 0 deletions clients/react-vite/src/components/pages/LandingPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { useAdventurersByXPWithScores } from "@/hooks";
import {
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
} from "@/components/ui/pagination";

import { useConnect } from "@starknet-react/core";
import { useState } from "react";
import { AnimatedNumber } from "../ui/animatedNumber";

export const LandingPage = () => {
const { connect, connectors } = useConnect();

const [page, setPage] = useState(1);

const handlePreviousPage = () => {
setPage((prev) => Math.max(prev - 1, 1));
};

const handleNextPage = () => {
setPage((prev) => Math.min(prev + 1, totalPages));
};

const { adventurersWithScores, isLoading, isError } =
useAdventurersByXPWithScores({ page });

const itemsPerPage = 10;
const totalPages = 5;
const startingRank = (page - 1) * itemsPerPage + 1;

return (
<div className="w-screen h-screen bg-skulls bg-cover bg-no-repeat bg-center flex justify-center">
<div className="self-center sm:w-1/2 text-center uppercase">
<div className="sm:text-5xl mb-8">Leaderboard</div>
<div className="bg-black/90 p-10 w-full border border-primary rounded-2xl ">
<div className="overflow-x-auto ">
<table className="w-full">
<thead>
<tr>
<th className="text-xl pb-3 px-4 border-b border-primary">
Rank
</th>
<th className="text-xl pb-3 px-4 border-b border-primary">
Player
</th>
<th className="text-xl pb-3 px-4 border-b border-primary">
Score
</th>
<th className="text-xl pb-3 px-4 border-b border-primary">
Lords Payout
</th>
</tr>
</thead>
<tbody>
{isLoading && <div className="p-10 mx-auto">Loading...</div>}
{isError && <div>Error</div>}
{adventurersWithScores?.slice(0, 1).map((adventurer, index) => (
<tr key={index} className=" bg-primary text-black">
<td className="text-xl py-5 px-4 border-b border-primary">
{startingRank}
</td>
<td className="text-xl py-5 px-4 border-b border-primary">
{adventurer.name}
</td>
<td className="text-xl py-5 px-4 border-b border-primary">
{adventurer.xp}
</td>
<td className="text-xl py-5 px-4 border-b border-primary">
<AnimatedNumber value={adventurer.totalPayout || 0} />
</td>
</tr>
))}
{adventurersWithScores
?.slice(1, 12)
.map((adventurer, index) => (
<tr key={index} className=" ">
<td className="py-3 px-4 border-b border-primary">
{startingRank + index + 1}
</td>
<td className="py-3 px-4 border-b border-primary">
{adventurer.name}
</td>
<td className="py-3 px-4 border-b border-primary">
{adventurer.xp}
</td>
<td className="py-3 px-4 border-b border-primary">
<AnimatedNumber value={adventurer.totalPayout || 0} />
</td>
</tr>
))}
</tbody>
</table>
</div>
<Pagination className="mt-4">
<PaginationContent>
<PaginationItem>
<PaginationPrevious onClick={handlePreviousPage} />
</PaginationItem>
{[...Array(totalPages)].map((_, i) => (
<PaginationItem key={i}>
<PaginationLink
href="#"
onClick={() => setPage(i + 1)}
isActive={page === i + 1}
>
{i + 1}
</PaginationLink>
</PaginationItem>
))}
<PaginationItem>
<PaginationNext onClick={handleNextPage} />
</PaginationItem>
</PaginationContent>
</Pagination>
</div>
</div>{" "}
</div>
);
};
16 changes: 16 additions & 0 deletions clients/react-vite/src/components/providers/QueryProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import {
useQuery,
useMutation,
useQueryClient,
QueryClient,
QueryClientProvider,
} from "@tanstack/react-query";

const queryClient = new QueryClient();

export function QueryProvider({ children }: { children: React.ReactNode }) {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
}
19 changes: 19 additions & 0 deletions clients/react-vite/src/components/providers/StarknetProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import CartridgeConnector from "@cartridge/connector";
import { sepolia, mainnet } from "@starknet-react/chains";
import { StarknetConfig, publicProvider, voyager } from "@starknet-react/core";

export function StarknetProvider({ children }: { children: React.ReactNode }) {
const cartridge = new CartridgeConnector([]);

return (
<StarknetConfig
chains={[mainnet, sepolia]}
provider={publicProvider()}
connectors={[cartridge]}
explorer={voyager}
>
{children}
</StarknetConfig>
);
}
13 changes: 13 additions & 0 deletions clients/react-vite/src/components/ui/animatedNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { motion, useMotionValue, useTransform, animate } from "framer-motion";
import { useEffect } from "react";
export const AnimatedNumber = ({ value }: { value: number }) => {
const count = useMotionValue(0);
const rounded = useTransform(count, (latest) => latest.toFixed(2));

useEffect(() => {
const controls = animate(count, value, { duration: 1 });
return controls.stop;
}, [count, value]);

return <motion.span>{rounded}</motion.span>;
};
2 changes: 1 addition & 1 deletion clients/react-vite/src/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const buttonVariants = cva(
"border border-input bg-transparent border-primary text-primary hover:bg-primary/40 ",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
ghost: "hover:bg-primary hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
Expand Down
117 changes: 117 additions & 0 deletions clients/react-vite/src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import * as React from "react"
import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react"

import { cn } from "@/lib/utils"
import { ButtonProps, buttonVariants } from "@/components/ui/button"

const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => (
<nav
role="navigation"
aria-label="pagination"
className={cn("mx-auto flex w-full justify-center", className)}
{...props}
/>
)
Pagination.displayName = "Pagination"

const PaginationContent = React.forwardRef<
HTMLUListElement,
React.ComponentProps<"ul">
>(({ className, ...props }, ref) => (
<ul
ref={ref}
className={cn("flex flex-row items-center gap-1", className)}
{...props}
/>
))
PaginationContent.displayName = "PaginationContent"

const PaginationItem = React.forwardRef<
HTMLLIElement,
React.ComponentProps<"li">
>(({ className, ...props }, ref) => (
<li ref={ref} className={cn("", className)} {...props} />
))
PaginationItem.displayName = "PaginationItem"

type PaginationLinkProps = {
isActive?: boolean
} & Pick<ButtonProps, "size"> &
React.ComponentProps<"a">

const PaginationLink = ({
className,
isActive,
size = "icon",
...props
}: PaginationLinkProps) => (
<a
aria-current={isActive ? "page" : undefined}
className={cn(
buttonVariants({
variant: isActive ? "outline" : "ghost",
size,
}),
className
)}
{...props}
/>
)
PaginationLink.displayName = "PaginationLink"

const PaginationPrevious = ({
className,
...props
}: React.ComponentProps<typeof PaginationLink>) => (
<PaginationLink
aria-label="Go to previous page"
size="default"
className={cn("gap-1 pl-2.5", className)}
{...props}
>
<ChevronLeft className="h-4 w-4" />
<span>Previous</span>
</PaginationLink>
)
PaginationPrevious.displayName = "PaginationPrevious"

const PaginationNext = ({
className,
...props
}: React.ComponentProps<typeof PaginationLink>) => (
<PaginationLink
aria-label="Go to next page"
size="default"
className={cn("gap-1 pr-2.5", className)}
{...props}
>
<span>Next</span>
<ChevronRight className="h-4 w-4" />
</PaginationLink>
)
PaginationNext.displayName = "PaginationNext"

const PaginationEllipsis = ({
className,
...props
}: React.ComponentProps<"span">) => (
<span
aria-hidden
className={cn("flex h-9 w-9 items-center justify-center", className)}
{...props}
>
<MoreHorizontal className="h-4 w-4" />
<span className="sr-only">More pages</span>
</span>
)
PaginationEllipsis.displayName = "PaginationEllipsis"

export {
Pagination,
PaginationContent,
PaginationEllipsis,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
}
Loading

0 comments on commit ac84abd

Please sign in to comment.