2023-03-23 17:22:26 -07:00
|
|
|
import { useAppDispatch, useLoading } from 'soapbox/hooks';
|
2023-03-22 12:40:18 -07:00
|
|
|
|
|
|
|
import { dismissEntities } from '../actions';
|
|
|
|
|
2023-03-22 14:06:10 -07:00
|
|
|
import { parseEntitiesPath } from './utils';
|
|
|
|
|
2023-03-23 17:22:26 -07:00
|
|
|
import type { EntityFn, ExpandedEntitiesPath } from './types';
|
2023-03-22 12:40:18 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an entity from a specific list.
|
|
|
|
* To remove an entity globally from all lists, see `useDeleteEntity`.
|
|
|
|
*/
|
2023-03-23 17:22:26 -07:00
|
|
|
function useDismissEntity(expandedPath: ExpandedEntitiesPath, entityFn: EntityFn<string>) {
|
2023-03-22 12:40:18 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
2023-03-23 17:22:26 -07:00
|
|
|
const [isLoading, setPromise] = useLoading();
|
|
|
|
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
|
|
|
|
2023-03-22 12:40:18 -07:00
|
|
|
// TODO: optimistic dismissing
|
2023-03-23 17:22:26 -07:00
|
|
|
async function dismissEntity(entityId: string) {
|
|
|
|
const result = await setPromise(entityFn(entityId));
|
2023-03-22 12:40:18 -07:00
|
|
|
dispatch(dismissEntities([entityId], entityType, listKey));
|
|
|
|
return result;
|
2023-03-23 17:22:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
dismissEntity,
|
|
|
|
isLoading,
|
2023-03-22 12:40:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useDismissEntity };
|