import { useState } from "react";
type OperationState = 'NotStarted' | 'Succeeded' | 'Failed';
export const OperationStateIndicator = ({ id, state }: { id: string, state: OperationState }) => {
if (state === 'NotStarted') return null;
return { state === 'Succeeded' ? '✅' : '❌' };
}
export const useOperationStateWrapper = >(fn: (...args: T) => Promise): { wrappedFn: (...args: T) => Promise, opState: OperationState } => {
const [opState, setState] = useState('NotStarted');
const wrappedFn = async (...args: T): Promise => {
setState('NotStarted')
try {
await fn(...args);
setState('Succeeded');
} catch (e) {
console.error(e);
setState('Failed');
}
};
return { wrappedFn, opState };
};