1 import { useEffect, useState } from 'react';
3 import useErrorHandler from '@proton/components/hooks/useErrorHandler';
4 import useLoading from '@proton/hooks/useLoading';
5 import { getOrgAuthLogs } from '@proton/shared/lib/api/b2bevents';
7 import type { AuthLogsQueryParams } from './AuthenticationLogs';
9 const getFormattedQueryString = (params: { [key: string]: any }) => {
10 const queryParts: string[] = [];
12 Object.keys(params).forEach((key) => {
13 const value = params[key];
14 if (value === undefined) {
17 if (Array.isArray(value)) {
18 value.forEach((item) => {
19 queryParts.push(`${key}[]=${encodeURIComponent(item)}`);
22 queryParts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
26 return queryParts.join('&');
29 const useOrgAuthLogs = (api: any, query: AuthLogsQueryParams, page: number) => {
30 const handleError = useErrorHandler();
31 const [authLogs, setAuthLogs] = useState([]);
32 const [total, setTotal] = useState(0);
33 const [loading, withLoading] = useLoading();
34 const [error, setError] = useState<string | undefined>(undefined);
36 const fetchAuthLogs = async () => {
37 if (!query.Emails.length) {
43 const queryString = getFormattedQueryString({ ...query, Page: page - 1, PageSize: 10 });
44 const { Items, Total } = await api(getOrgAuthLogs(queryString));
45 const data = Items.map((item: any) => item.Data);
50 setError('No logs exist for this query.');
54 setError('Failed to fetch logs.');
59 void withLoading(fetchAuthLogs());
62 return { authLogs, total, loading, error };
65 export default useOrgAuthLogs;