Skip to content

Commit

Permalink
Merge pull request #12 from hamster1963/sort
Browse files Browse the repository at this point in the history
feat: add display sort
  • Loading branch information
hamster1963 authored Aug 9, 2024
2 parents 83363bc + 0f1649c commit f0eb66c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
21 changes: 14 additions & 7 deletions app/(main)/ClientComponents/ServerListClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import useSWR from "swr";

export default function ServerListClient() {
const { data } = useSWR<ServerApi>("/api/server", nezhaFetcher, {
refreshInterval: process.env.NEXT_PUBLIC_NezhaFetchInterval
? Number(process.env.NEXT_PUBLIC_NezhaFetchInterval)
: 2000,
refreshInterval: Number(process.env.NEXT_PUBLIC_NezhaFetchInterval) || 2000,
});

if (!data) return null;
const sortedResult = data.result.sort((a, b) => a.id - b.id);

const sortedServers = data.result.sort((a, b) => {
if (a.display_index && b.display_index) {
return b.display_index - a.display_index;
}
if (a.display_index) return -1;
if (b.display_index) return 1;
return a.id - b.id;
});

return (
<section className={"grid grid-cols-1 gap-2 md:grid-cols-2"}>
{sortedResult.map((serverInfo) => (
<section className="grid grid-cols-1 gap-2 md:grid-cols-2">
{sortedServers.map((serverInfo) => (
<ServerCard key={serverInfo.id} serverInfo={serverInfo} />
))}
</section>
);
}
}
1 change: 1 addition & 0 deletions app/types/nezha-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface NezhaAPI {
ipv4: string;
ipv6: string;
valid_ip: string;
display_index: number;
host: NezhaAPIHost;
status: NezhaAPIStatus;
}
Expand Down
8 changes: 4 additions & 4 deletions components/ServerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ export default function ServerCard({
{getUnicodeFlagIcon(country_code)}
</span>
) : (
<span className="text-[12px] text-muted-foreground">🏴‍☠️</span>
<span className="text-[12px] text-muted-foreground">🏁</span>
)
) : null}
<span className="h-2 w-2 shrink-0 rounded-full bg-green-500"></span>
<p
className={cn(
"break-all font-bold tracking-tight",
Expand All @@ -47,6 +46,7 @@ export default function ServerCard({
>
{name}
</p>
<span className="h-2 w-2 shrink-0 rounded-full bg-green-500"></span>
</section>
</PopoverTrigger>
<PopoverContent side="top">
Expand Down Expand Up @@ -94,10 +94,9 @@ export default function ServerCard({
{getUnicodeFlagIcon(country_code)}
</span>
) : (
<span className="text-[12px] text-muted-foreground">🏴‍☠️</span>
<span className="text-[12px] text-muted-foreground">🏁</span>
)
) : null}
<span className="h-2 w-2 shrink-0 rounded-full bg-red-500"></span>
<p
className={cn(
"break-all font-bold tracking-tight",
Expand All @@ -106,6 +105,7 @@ export default function ServerCard({
>
{name}
</p>
<span className="h-2 w-2 shrink-0 rounded-full bg-red-500"></span>
</section>
</PopoverTrigger>
<PopoverContent className="w-fit p-2" side="top">
Expand Down

0 comments on commit f0eb66c

Please sign in to comment.