2023-03-23 14:22:15 -07:00
|
|
|
import { useEffect } from 'react';
|
2023-03-13 14:23:11 -07:00
|
|
|
import z from 'zod';
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2023-03-23 17:22:26 -07:00
|
|
|
import { useAppDispatch, useAppSelector, useLoading } from 'soapbox/hooks';
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
import { importEntities } from '../actions';
|
|
|
|
|
|
|
|
import type { Entity } from '../types';
|
2023-03-23 17:22:26 -07:00
|
|
|
import type { EntitySchema, EntityPath, EntityFn } from './types';
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2023-03-09 10:32:50 -08:00
|
|
|
/** Additional options for the hook. */
|
2023-03-14 12:24:11 -07:00
|
|
|
interface UseEntityOpts<TEntity extends Entity> {
|
2023-03-13 14:23:11 -07:00
|
|
|
/** A zod schema to parse the API entity. */
|
2023-03-14 12:24:11 -07:00
|
|
|
schema?: EntitySchema<TEntity>
|
2023-03-09 13:05:27 -08:00
|
|
|
/** Whether to refetch this entity every time the hook mounts, even if it's already in the store. */
|
|
|
|
refetch?: boolean
|
2023-05-11 11:41:31 -07:00
|
|
|
/** A flag to potentially disable sending requests to the API. */
|
|
|
|
enabled?: boolean
|
2023-03-09 10:32:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function useEntity<TEntity extends Entity>(
|
|
|
|
path: EntityPath,
|
2023-03-23 17:22:26 -07:00
|
|
|
entityFn: EntityFn<void>,
|
2023-03-09 10:32:50 -08:00
|
|
|
opts: UseEntityOpts<TEntity> = {},
|
|
|
|
) {
|
2023-04-04 05:11:03 -07:00
|
|
|
const [isFetching, setPromise] = useLoading(true);
|
2022-12-04 14:58:13 -08:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
|
|
const [entityType, entityId] = path;
|
2023-03-09 10:32:50 -08:00
|
|
|
|
2023-03-13 14:23:11 -07:00
|
|
|
const defaultSchema = z.custom<TEntity>();
|
|
|
|
const schema = opts.schema || defaultSchema;
|
2023-03-09 10:32:50 -08:00
|
|
|
|
2023-03-13 14:39:23 -07:00
|
|
|
const entity = useAppSelector(state => state.entities[entityType]?.store[entityId] as TEntity | undefined);
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2023-05-11 11:41:31 -07:00
|
|
|
const isEnabled = opts.enabled ?? true;
|
2022-12-04 14:58:13 -08:00
|
|
|
const isLoading = isFetching && !entity;
|
|
|
|
|
2023-03-23 13:15:04 -07:00
|
|
|
const fetchEntity = async () => {
|
|
|
|
try {
|
2023-03-23 17:22:26 -07:00
|
|
|
const response = await setPromise(entityFn());
|
2023-03-23 13:15:04 -07:00
|
|
|
const entity = schema.parse(response.data);
|
2023-03-13 14:39:23 -07:00
|
|
|
dispatch(importEntities([entity], entityType));
|
2023-03-23 13:15:04 -07:00
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2022-12-04 14:58:13 -08:00
|
|
|
};
|
|
|
|
|
2023-03-09 13:05:27 -08:00
|
|
|
useEffect(() => {
|
2023-05-11 11:41:31 -07:00
|
|
|
if (!isEnabled) return;
|
2023-03-09 13:05:27 -08:00
|
|
|
if (!entity || opts.refetch) {
|
|
|
|
fetchEntity();
|
|
|
|
}
|
2023-05-11 11:41:31 -07:00
|
|
|
}, [isEnabled]);
|
2023-03-09 13:05:27 -08:00
|
|
|
|
2022-12-04 14:58:13 -08:00
|
|
|
return {
|
|
|
|
entity,
|
|
|
|
fetchEntity,
|
|
|
|
isFetching,
|
|
|
|
isLoading,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
useEntity,
|
2023-04-12 13:39:41 -07:00
|
|
|
type UseEntityOpts,
|
2022-12-04 14:58:13 -08:00
|
|
|
};
|