-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Kelvin Steiner <[email protected]>
- Loading branch information
1 parent
c0b1958
commit 7147db1
Showing
5 changed files
with
189 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,49 @@ | ||
import { Suspense } from "react"; | ||
|
||
import type { Subnet } from "~/utils/types"; | ||
import { PaginationControls } from "~/app/components/pagination-controls"; | ||
import SubnetCard from "~/app/components/subnet-card"; | ||
import { SubnetViewControls } from "~/app/components/subnet-view-controls"; | ||
import { api } from "~/trpc/server"; | ||
|
||
export default async function SubnetsPage() { | ||
const data = await api.subnet.all(); | ||
export default async function SubnetsPage({ | ||
searchParams, | ||
}: { | ||
searchParams: { page?: string; sortBy?: string; order?: string }; | ||
}) { | ||
const currentPage = Number(searchParams.page) || 1; | ||
const sortBy = searchParams.sortBy ?? "id"; | ||
const order = searchParams.order === "desc" ? "desc" : "asc"; | ||
|
||
const { subnets, metadata } = await api.subnet.paginatedAll({ | ||
page: currentPage, | ||
limit: 24, | ||
sortBy: sortBy, | ||
order: order, | ||
}); | ||
|
||
return ( | ||
<div className="mb-4 flex w-full flex-col gap-4"> | ||
{data.map((subnet) => ( | ||
<SubnetCard | ||
key={subnet.id} | ||
founderAddress={subnet.founder} | ||
id={subnet.netuid} | ||
name={subnet.name} | ||
/> | ||
))} | ||
</div> | ||
<> | ||
<Suspense fallback={<div>Loading view controls...</div>}> | ||
<SubnetViewControls /> | ||
</Suspense> | ||
<div className="mb-4 flex w-full flex-col gap-4"> | ||
{subnets.length ? ( | ||
subnets.map((subnet: Subnet) => ( | ||
<SubnetCard | ||
key={subnet.id} | ||
founderAddress={subnet.founder} | ||
id={subnet.netuid} | ||
name={subnet.name} | ||
/> | ||
)) | ||
) : ( | ||
<p>No subnets found</p> | ||
)} | ||
</div> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<PaginationControls totalPages={metadata.totalPages} /> | ||
</Suspense> | ||
</> | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
apps/commune-validator/src/app/components/subnet-view-controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { useRouter, useSearchParams } from "next/navigation"; | ||
|
||
type SortField = | ||
| "id" | ||
| "founderShare" | ||
| "incentiveRatio" | ||
| "proposalRewardTreasuryAllocation" | ||
| "minValidatorStake" | ||
| "createdAt"; | ||
|
||
type SortOrder = "asc" | "desc"; | ||
|
||
const sortFieldLabels: Record<SortField, string> = { | ||
id: "ID", | ||
founderShare: "Founder Share", | ||
incentiveRatio: "Incentive Ratio", | ||
proposalRewardTreasuryAllocation: "Proposal Reward Alloc.", | ||
minValidatorStake: "Min. Vali. Stake", | ||
createdAt: "Creation Date", | ||
}; | ||
|
||
export function SubnetViewControls() { | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
const [sortField, setSortField] = useState<SortField>( | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
(searchParams.get("sortBy") as SortField) ?? "id", | ||
); | ||
const [sortOrder, setSortOrder] = useState<SortOrder>( | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
(searchParams.get("order") as SortOrder) ?? "asc", | ||
); | ||
|
||
useEffect(() => { | ||
const newSearchParams = new URLSearchParams(searchParams); | ||
newSearchParams.set("sortBy", sortField); | ||
newSearchParams.set("order", sortOrder); | ||
router.push(`?${newSearchParams.toString()}`, { scroll: false }); | ||
}, [sortField, sortOrder, router, searchParams]); | ||
|
||
const handleSortChange = (field: SortField) => { | ||
const newOrder = | ||
field === sortField && sortOrder === "asc" ? "desc" : "asc"; | ||
setSortField(field); | ||
setSortOrder(newOrder); | ||
}; | ||
|
||
return ( | ||
<div className="mb-4 flex w-full animate-fade-down flex-col items-center justify-between gap-2 border-b border-white/20 pb-4 animate-delay-200 md:flex-row"> | ||
<span className="w-full text-white">Sort by:</span> | ||
{(Object.keys(sortFieldLabels) as SortField[]).map((field) => ( | ||
<button | ||
key={field} | ||
onClick={() => handleSortChange(field)} | ||
className={`w-full py-1 text-sm ${ | ||
sortField === field | ||
? "border border-cyan-500 bg-cyan-500/20 text-white" | ||
: "border border-white/20 bg-[#898989]/5 text-gray-300 hover:bg-gray-600/50" | ||
}`} | ||
> | ||
{sortFieldLabels[field]}{" "} | ||
{sortField === field && (sortOrder === "asc" ? "↑" : "↓")} | ||
</button> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters