1 import { useEffect, useMemo, useState } from 'react';
3 import { useActionRequest } from '@proton/pass/hooks/useActionRequest';
4 import type { itemHistoryFailure, itemHistorySuccess } from '@proton/pass/store/actions';
5 import { itemHistoryIntent } from '@proton/pass/store/actions';
6 import type { ItemRevisionsIntent, ItemRevisionsSuccess } from '@proton/pass/types';
8 type Props = Omit<ItemRevisionsIntent, 'since'>;
10 export const useItemRevisions = ({ shareId, itemId, pageSize }: Props) => {
11 const [state, setState] = useState<ItemRevisionsSuccess>({ next: null, revisions: [], since: null, total: 0 });
13 const { loading, revalidate, dispatch } = useActionRequest<
14 typeof itemHistoryIntent,
15 typeof itemHistorySuccess,
16 typeof itemHistoryFailure
17 >(itemHistoryIntent, {
18 onSuccess: ({ data }) => {
21 ...(data.since ? { revisions: [...prev.revisions, ...data.revisions] } : { revisions: data.revisions }),
27 /** Trigger initial request when component mounts */
28 dispatch({ shareId, itemId, pageSize, since: null });
33 state: { ...state, loading },
34 loadMore: () => !loading && state.next && revalidate({ shareId, itemId, pageSize, since: state.next }),
36 [loading, state, revalidate]