Add login and register dialogs, user dropdown menu with profile link and logout option, and integrate with session management hook. Prevent hydration mismatches by conditionally rendering auth elements after mount. Update package-lock.json with new @esbuild dependencies for build tooling.
93 lines
2.3 KiB
TypeScript
93 lines
2.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect, useCallback } from "react";
|
|
|
|
export interface Server {
|
|
serverId: number;
|
|
serverName: string;
|
|
serverType: "pvp" | "no-pvp" | "retro-pvp" | "hardcore-pvp" | null;
|
|
location: "NA" | "SA" | "EU" | "OC" | "BR" | null;
|
|
isActive: boolean;
|
|
stats: {
|
|
totalListings: number;
|
|
totalUsers: number;
|
|
totalSubscribers: number;
|
|
totalTransactions: number;
|
|
totalVolume: number;
|
|
averageListingPrice: number;
|
|
lastActivityAt: Date | null;
|
|
};
|
|
}
|
|
|
|
interface UseServersReturn {
|
|
servers: Server[];
|
|
isLoading: boolean;
|
|
error: string | null;
|
|
refetch: () => Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* Hook para buscar e gerenciar servidores disponíveis
|
|
*/
|
|
export function useServers(): UseServersReturn {
|
|
const [servers, setServers] = useState<Server[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchServers = useCallback(async () => {
|
|
// Só buscar no cliente
|
|
if (typeof window === "undefined") {
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
setIsLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await fetch("/api/servers", {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
// Verificar se a resposta é JSON
|
|
const contentType = response.headers.get("content-type");
|
|
if (!contentType || !contentType.includes("application/json")) {
|
|
const text = await response.text();
|
|
console.error("Resposta não é JSON:", text.substring(0, 200));
|
|
setError("Erro: resposta inválida do servidor");
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
setServers(data.servers || []);
|
|
} else {
|
|
setError(data.error || "Erro ao buscar servidores");
|
|
}
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : "Erro desconhecido";
|
|
setError("Erro ao conectar com o servidor");
|
|
console.error("Erro ao buscar servidores:", errorMessage, err);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
fetchServers();
|
|
}, [fetchServers]);
|
|
|
|
return {
|
|
servers,
|
|
isLoading,
|
|
error,
|
|
refetch: fetchServers,
|
|
};
|
|
}
|
|
|