67 lines
No EOL
1.7 KiB
TypeScript
67 lines
No EOL
1.7 KiB
TypeScript
import { useEffect } from 'react';
|
|
import z from 'zod';
|
|
|
|
import { useAppDispatch, useAppSelector, useLoading } from 'soapbox/hooks';
|
|
|
|
import { importEntities } from '../actions';
|
|
|
|
import type { Entity } from '../types';
|
|
import type { EntitySchema, EntityPath, EntityFn } from './types';
|
|
|
|
/** Additional options for the hook. */
|
|
interface UseEntityOpts<TEntity extends Entity> {
|
|
/** A zod schema to parse the API entity. */
|
|
schema?: EntitySchema<TEntity>
|
|
/** Whether to refetch this entity every time the hook mounts, even if it's already in the store. */
|
|
refetch?: boolean
|
|
/** A flag to potentially disable sending requests to the API. */
|
|
enabled?: boolean
|
|
}
|
|
|
|
function useEntity<TEntity extends Entity>(
|
|
path: EntityPath,
|
|
entityFn: EntityFn<void>,
|
|
opts: UseEntityOpts<TEntity> = {},
|
|
) {
|
|
const [isFetching, setPromise] = useLoading(true);
|
|
const dispatch = useAppDispatch();
|
|
|
|
const [entityType, entityId] = path;
|
|
|
|
const defaultSchema = z.custom<TEntity>();
|
|
const schema = opts.schema || defaultSchema;
|
|
|
|
const entity = useAppSelector(state => state.entities[entityType]?.store[entityId] as TEntity | undefined);
|
|
|
|
const isEnabled = opts.enabled ?? true;
|
|
const isLoading = isFetching && !entity;
|
|
|
|
const fetchEntity = async () => {
|
|
try {
|
|
const response = await setPromise(entityFn());
|
|
const entity = schema.parse(response.data);
|
|
dispatch(importEntities([entity], entityType));
|
|
} catch (e) {
|
|
// do nothing
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!isEnabled) return;
|
|
if (!entity || opts.refetch) {
|
|
fetchEntity();
|
|
}
|
|
}, [isEnabled]);
|
|
|
|
return {
|
|
entity,
|
|
fetchEntity,
|
|
isFetching,
|
|
isLoading,
|
|
};
|
|
}
|
|
|
|
export {
|
|
useEntity,
|
|
type UseEntityOpts,
|
|
}; |