Merge pull request #12 from hamster1963/sort

feat: add display sort
This commit is contained in:
仓鼠 2024-08-09 17:24:27 +08:00 committed by GitHub
commit f0eb66c1cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 11 deletions

View File

@ -7,18 +7,25 @@ import useSWR from "swr";
export default function ServerListClient() { export default function ServerListClient() {
const { data } = useSWR<ServerApi>("/api/server", nezhaFetcher, { const { data } = useSWR<ServerApi>("/api/server", nezhaFetcher, {
refreshInterval: process.env.NEXT_PUBLIC_NezhaFetchInterval refreshInterval: Number(process.env.NEXT_PUBLIC_NezhaFetchInterval) || 2000,
? Number(process.env.NEXT_PUBLIC_NezhaFetchInterval)
: 2000,
}); });
if (!data) return null; 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 ( return (
<section className={"grid grid-cols-1 gap-2 md:grid-cols-2"}> <section className="grid grid-cols-1 gap-2 md:grid-cols-2">
{sortedResult.map((serverInfo) => ( {sortedServers.map((serverInfo) => (
<ServerCard key={serverInfo.id} serverInfo={serverInfo} /> <ServerCard key={serverInfo.id} serverInfo={serverInfo} />
))} ))}
</section> </section>
); );
} }

View File

@ -16,6 +16,7 @@ export interface NezhaAPI {
ipv4: string; ipv4: string;
ipv6: string; ipv6: string;
valid_ip: string; valid_ip: string;
display_index: number;
host: NezhaAPIHost; host: NezhaAPIHost;
status: NezhaAPIStatus; status: NezhaAPIStatus;
} }

View File

@ -35,10 +35,9 @@ export default function ServerCard({
{getUnicodeFlagIcon(country_code)} {getUnicodeFlagIcon(country_code)}
</span> </span>
) : ( ) : (
<span className="text-[12px] text-muted-foreground">🏴</span> <span className="text-[12px] text-muted-foreground">🏁</span>
) )
) : null} ) : null}
<span className="h-2 w-2 shrink-0 rounded-full bg-green-500"></span>
<p <p
className={cn( className={cn(
"break-all font-bold tracking-tight", "break-all font-bold tracking-tight",
@ -47,6 +46,7 @@ export default function ServerCard({
> >
{name} {name}
</p> </p>
<span className="h-2 w-2 shrink-0 rounded-full bg-green-500"></span>
</section> </section>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent side="top"> <PopoverContent side="top">
@ -94,10 +94,9 @@ export default function ServerCard({
{getUnicodeFlagIcon(country_code)} {getUnicodeFlagIcon(country_code)}
</span> </span>
) : ( ) : (
<span className="text-[12px] text-muted-foreground">🏴</span> <span className="text-[12px] text-muted-foreground">🏁</span>
) )
) : null} ) : null}
<span className="h-2 w-2 shrink-0 rounded-full bg-red-500"></span>
<p <p
className={cn( className={cn(
"break-all font-bold tracking-tight", "break-all font-bold tracking-tight",
@ -106,6 +105,7 @@ export default function ServerCard({
> >
{name} {name}
</p> </p>
<span className="h-2 w-2 shrink-0 rounded-full bg-red-500"></span>
</section> </section>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent className="w-fit p-2" side="top"> <PopoverContent className="w-fit p-2" side="top">