Add dashboard UI source and track it in the repository.
Expose the Vite/React website source under dashboard/ (with node_modules still ignored) so the full dashboard can be built and maintained from this repo.
This commit is contained in:
1 parent
e97e281bda
commit
b82f69783d
117 files changed
+18615
-3
No files matched your search
@@ -0,0 +1,124 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { api } from "../api/client";
|
||||
import {
|
||||
canEditConfig,
|
||||
canViewConfig,
|
||||
displayDiscordName,
|
||||
hasPermission,
|
||||
type DashboardPermissions,
|
||||
type DashboardUser,
|
||||
} from "../lib/permissions";
|
||||
|
||||
interface AuthContextValue {
|
||||
user: DashboardUser | null;
|
||||
permissions: DashboardPermissions | null;
|
||||
isAuthenticated: boolean;
|
||||
loading: boolean;
|
||||
loginWithDiscord: () => void;
|
||||
logout: () => Promise<void>;
|
||||
refreshSession: () => Promise<boolean>;
|
||||
hasPermission: (key: string) => boolean;
|
||||
canViewConfig: (file: string) => boolean;
|
||||
canEditConfig: (file: string) => boolean;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [user, setUser] = useState<DashboardUser | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const refreshSession = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.me();
|
||||
if (res.data) {
|
||||
setUser(res.data);
|
||||
return true;
|
||||
}
|
||||
setUser(null);
|
||||
return false;
|
||||
} catch {
|
||||
setUser(null);
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refreshSession().finally(() => setLoading(false));
|
||||
}, [refreshSession]);
|
||||
|
||||
const loginWithDiscord = useCallback(() => {
|
||||
window.location.href = "/api/auth/discord";
|
||||
}, []);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
await api.logout();
|
||||
} finally {
|
||||
setUser(null);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const permissions = user?.permissions ?? null;
|
||||
|
||||
const checkPermission = useCallback(
|
||||
(key: string) => hasPermission(permissions, key),
|
||||
[permissions],
|
||||
);
|
||||
|
||||
const checkViewConfig = useCallback(
|
||||
(file: string) => canViewConfig(permissions, file),
|
||||
[permissions],
|
||||
);
|
||||
|
||||
const checkEditConfig = useCallback(
|
||||
(file: string) => canEditConfig(permissions, file),
|
||||
[permissions],
|
||||
);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
user,
|
||||
permissions,
|
||||
isAuthenticated: !!user,
|
||||
loading,
|
||||
loginWithDiscord,
|
||||
logout,
|
||||
refreshSession,
|
||||
hasPermission: checkPermission,
|
||||
canViewConfig: checkViewConfig,
|
||||
canEditConfig: checkEditConfig,
|
||||
}),
|
||||
[
|
||||
user,
|
||||
permissions,
|
||||
loading,
|
||||
loginWithDiscord,
|
||||
logout,
|
||||
refreshSession,
|
||||
checkPermission,
|
||||
checkViewConfig,
|
||||
checkEditConfig,
|
||||
],
|
||||
);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) throw new Error("useAuth must be used within AuthProvider");
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export { displayDiscordName };
|
||||
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { api, type BotStats } from "@/api/client";
|
||||
|
||||
interface BotStatsContextValue {
|
||||
stats: BotStats | null;
|
||||
loading: boolean;
|
||||
error: string;
|
||||
refresh: () => Promise<void>;
|
||||
}
|
||||
|
||||
const BotStatsContext = createContext<BotStatsContextValue | null>(null);
|
||||
|
||||
export function BotStatsProvider({ children }: { children: ReactNode }) {
|
||||
const [stats, setStats] = useState<BotStats | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.stats();
|
||||
if (res.data) {
|
||||
setStats(res.data);
|
||||
setError("");
|
||||
} else if (res.error) {
|
||||
setError(res.error);
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Failed to load stats");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
const id = setInterval(refresh, 30_000);
|
||||
return () => clearInterval(id);
|
||||
}, [refresh]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({ stats, loading, error, refresh }),
|
||||
[stats, loading, error, refresh],
|
||||
);
|
||||
|
||||
return (
|
||||
<BotStatsContext.Provider value={value}>{children}</BotStatsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useBotStats() {
|
||||
const ctx = useContext(BotStatsContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useBotStats must be used within BotStatsProvider");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { api } from "@/api/client";
|
||||
import { applyDashboardBranding } from "@/lib/apply-dashboard-branding";
|
||||
import { DEFAULT_BRANDING, type DashboardBranding } from "@/lib/branding-types";
|
||||
|
||||
interface BrandingContextValue {
|
||||
branding: DashboardBranding;
|
||||
loading: boolean;
|
||||
refreshBranding: () => Promise<void>;
|
||||
setBrandingLocal: (next: DashboardBranding) => void;
|
||||
}
|
||||
|
||||
const BrandingContext = createContext<BrandingContextValue | null>(null);
|
||||
|
||||
export function BrandingProvider({ children }: { children: ReactNode }) {
|
||||
const [branding, setBranding] = useState<DashboardBranding>(DEFAULT_BRANDING);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const refreshBranding = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.getBranding();
|
||||
if (res.data) {
|
||||
setBranding(res.data);
|
||||
applyDashboardBranding(res.data);
|
||||
}
|
||||
} catch {
|
||||
setBranding(DEFAULT_BRANDING);
|
||||
applyDashboardBranding(DEFAULT_BRANDING);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshBranding();
|
||||
}, [refreshBranding]);
|
||||
|
||||
const setBrandingLocal = useCallback((next: DashboardBranding) => {
|
||||
setBranding(next);
|
||||
applyDashboardBranding(next);
|
||||
}, []);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
branding,
|
||||
loading,
|
||||
refreshBranding,
|
||||
setBrandingLocal,
|
||||
}),
|
||||
[branding, loading, refreshBranding, setBrandingLocal],
|
||||
);
|
||||
|
||||
return (
|
||||
<BrandingContext.Provider value={value}>{children}</BrandingContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useBranding() {
|
||||
const ctx = useContext(BrandingContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useBranding must be used within BrandingProvider");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import {
|
||||
api,
|
||||
type DashboardNotification,
|
||||
} from "@/api/client";
|
||||
|
||||
interface NotificationsContextValue {
|
||||
items: DashboardNotification[];
|
||||
unreadCount: number;
|
||||
loading: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
markAllRead: () => Promise<void>;
|
||||
dismiss: (id: string) => Promise<void>;
|
||||
}
|
||||
|
||||
const NotificationsContext = createContext<NotificationsContextValue | null>(
|
||||
null,
|
||||
);
|
||||
|
||||
export function NotificationsProvider({ children }: { children: ReactNode }) {
|
||||
const [items, setItems] = useState<DashboardNotification[]>([]);
|
||||
const [unreadCount, setUnreadCount] = useState(0);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.getNotifications();
|
||||
if (res.data) {
|
||||
setItems(res.data.items);
|
||||
setUnreadCount(res.data.unreadCount);
|
||||
}
|
||||
} catch {
|
||||
/* ignore when logged out or API down */
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const markAllRead = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.markNotificationsRead("all");
|
||||
setItems((prev) => prev.map((n) => ({ ...n, read: true })));
|
||||
setUnreadCount(res.data?.unreadCount ?? 0);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}, []);
|
||||
|
||||
const dismiss = useCallback(
|
||||
async (id: string) => {
|
||||
try {
|
||||
const res = await api.dismissNotification(id);
|
||||
setItems((prev) => prev.filter((n) => n.id !== id));
|
||||
setUnreadCount(res.data?.unreadCount ?? 0);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
const id = setInterval(refresh, 30_000);
|
||||
return () => clearInterval(id);
|
||||
}, [refresh]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
items,
|
||||
unreadCount,
|
||||
loading,
|
||||
refresh,
|
||||
markAllRead,
|
||||
dismiss,
|
||||
}),
|
||||
[items, unreadCount, loading, refresh, markAllRead, dismiss],
|
||||
);
|
||||
|
||||
return (
|
||||
<NotificationsContext.Provider value={value}>
|
||||
{children}
|
||||
</NotificationsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useNotifications() {
|
||||
const ctx = useContext(NotificationsContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useNotifications must be used within NotificationsProvider");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { api } from "@/api/client";
|
||||
import type { SetupStatus } from "@/lib/setup-types";
|
||||
|
||||
interface SetupContextValue {
|
||||
status: SetupStatus | null;
|
||||
loading: boolean;
|
||||
error: string;
|
||||
complete: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
}
|
||||
|
||||
const SetupContext = createContext<SetupContextValue | null>(null);
|
||||
|
||||
export function SetupProvider({ children }: { children: ReactNode }) {
|
||||
const [status, setStatus] = useState<SetupStatus | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setError("");
|
||||
try {
|
||||
const res = await api.getSetupStatus();
|
||||
setStatus(res.data ?? null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Could not load setup status");
|
||||
setStatus(null);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refresh().finally(() => setLoading(false));
|
||||
}, [refresh]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
status,
|
||||
loading,
|
||||
error,
|
||||
complete: Boolean(status?.complete),
|
||||
refresh,
|
||||
}),
|
||||
[status, loading, error, refresh],
|
||||
);
|
||||
|
||||
return <SetupContext.Provider value={value}>{children}</SetupContext.Provider>;
|
||||
}
|
||||
|
||||
export function useSetup() {
|
||||
const ctx = useContext(SetupContext);
|
||||
if (!ctx) throw new Error("useSetup must be used within SetupProvider");
|
||||
return ctx;
|
||||
}
|
||||
Reference in new issue
Block a user