feat: user sessions state

This commit is contained in:
2025-06-15 19:26:52 +02:00
parent e0f2c3219f
commit 44e1a18e9a

View File

@ -0,0 +1,57 @@
import {
adminGetUserSessionsApi,
adminRevokeUserSessionApi,
} from "@/api/admin/sessions";
import type { UserSession } from "@/types";
import { create } from "zustand";
export const ADMIN_USER_SESSIONS_PAGE_SIZE = 10;
export interface IUserSessionsState {
items: UserSession[];
totalPages: number;
page: number;
loading: boolean;
revokingId: string | null;
fetch: (page: number) => Promise<void>;
revoke: (id: string) => Promise<void>;
}
export const useUserSessions = create<IUserSessionsState>((set) => ({
items: [],
totalPages: 0,
page: 1,
loading: false,
revokingId: null,
fetch: async (page) => {
set({ loading: true, page });
try {
const response = await adminGetUserSessionsApi({
page,
size: ADMIN_USER_SESSIONS_PAGE_SIZE,
});
set({ items: response.items, totalPages: response.total_pages });
} catch (err) {
console.log("ERR: Failed to fetch admin user sessions:", err);
} finally {
set({ loading: false });
}
},
revoke: async (id) => {
set({ revokingId: id });
try {
await adminRevokeUserSessionApi(id);
} catch (err) {
console.log("ERR: Failed to revoke user sessions:", err);
} finally {
set({ revokingId: null });
}
},
}));