mirror of
https://github.com/khairul169/github-leaderboard.git
synced 2026-09-18 10:23:23 +07:00
feat: initial commit
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { useContext } from "react";
|
||||
import { AuthContext } from "@client/components/context/auth-context";
|
||||
import { API_BASEURL } from "@client/lib/api";
|
||||
|
||||
export const useAuth = () => {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
|
||||
return ctx;
|
||||
};
|
||||
|
||||
export const onLogin = () => {
|
||||
window.location.href = API_BASEURL + "/auth/login";
|
||||
};
|
||||
|
||||
export const onLogout = () => {
|
||||
window.location.href = API_BASEURL + "/auth/logout";
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
const cacheStore = new Map<string, any>();
|
||||
|
||||
type UseFetchOptions = {
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
export const useFetch = <T = any>(
|
||||
fetchKey: any,
|
||||
fetchFn: () => Promise<Response>,
|
||||
options?: Partial<UseFetchOptions>
|
||||
) => {
|
||||
const key = JSON.stringify(fetchKey);
|
||||
const loadingRef = useRef(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [data, setData] = useState<T | undefined>(cacheStore.get(key));
|
||||
const [error, setError] = useState<Error | undefined>();
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
if (loadingRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
loadingRef.current = true;
|
||||
setIsLoading(true);
|
||||
|
||||
const res = await fetchFn();
|
||||
if (!res.ok) {
|
||||
throw new Error(res.statusText);
|
||||
}
|
||||
|
||||
const isJson = res.headers
|
||||
.get("Content-Type")
|
||||
?.includes("application/json");
|
||||
|
||||
if (isJson) {
|
||||
const json = (await res.json()) as T;
|
||||
setData(json);
|
||||
cacheStore.set(key, json);
|
||||
} else {
|
||||
const text = await res.text();
|
||||
setData(text as unknown as T);
|
||||
cacheStore.set(key, text);
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err : new Error("Unknown error"));
|
||||
} finally {
|
||||
loadingRef.current = false;
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [key]);
|
||||
|
||||
useEffect(() => {
|
||||
if (options?.enabled !== false) {
|
||||
fetchData();
|
||||
}
|
||||
}, [fetchData, options?.enabled]);
|
||||
|
||||
return { data, isLoading, error, refetch: fetchData };
|
||||
};
|
||||
Reference in New Issue
Block a user