import { useQueryClient } from '@tanstack/react-query'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { refreshLinkData } from '../../shared/api/refreshLinkData'; import type { Fix } from '../../shared/api/types/fixes'; import { useFixActions } from '../../shared/fixes/useFixActions'; import { errorMessage } from '../../shared/ui/errorMessage'; import { useToast } from '../../shared/ui/Toasts'; import { continueFix } from './historyQueries'; export type HistoryEntryWork = 'undo' | 'continue' | null; export function useHistoryEntryActions( fix: Fix ) { const queryClient = useQueryClient(); const showToast = useToast(); const { undo } = useFixActions(); const [ work, setWork ] = useState< HistoryEntryWork >( null ); const runUndo = async () => { setWork( 'undo' ); await undo( fix ); setWork( null ); }; const runContinue = async () => { setWork( 'continue' ); try { await continueFix( fix ); showToast( { text: __( 'Finished.', 'aiplaz-link-checkup' ) } ); } catch ( error ) { showToast( { text: errorMessage( error, '' ), tone: 'bad' } ); } finally { refreshLinkData( queryClient ); setWork( null ); } }; return { work, runUndo, runContinue }; }