import type { RaRecord, SortPayload } from "ra-core";
import {
useCreatePath,
useRecordContext,
useReferenceManyFieldController,
} from "ra-core";
import { Link } from "react-router";
/**
* Displays the count of related records that reference the current record.
*
* Calls dataProvider.getList() to compute the the number of records in a related resource that have a foreign key pointing to the current record.
* It can optionally link to a filtered list of those records.
*
* @see {@link https://marmelab.com/shadcn-admin-kit/docs/referencemanycount/ ReferenceManyCount documentation}
*
* @example
* import { List, DataTable, ReferenceManyCount } from '@/components/admin';
*
* const AuthorList = () => (
*
*
*
*
*
*
*
*
* );
*/
export const ReferenceManyCount = (
props: ReferenceManyCountProps,
) => {
const {
reference,
target,
filter,
sort,
link,
resource,
source = "id",
} = props;
const record = useRecordContext(props);
const createPath = useCreatePath();
const { isLoading, error, total } =
useReferenceManyFieldController({
filter,
sort,
page: 1,
perPage: 1,
record,
reference,
resource,
source,
target,
});
const body = isLoading ? "" : error ? "error" : total;
return link && record ? (
e.stopPropagation()}
>
{body}
) : (
{body}
);
};
export interface ReferenceManyCountProps<
RecordType extends RaRecord = RaRecord,
> {
record?: RecordType;
reference: string;
resource?: string;
target: string;
source?: string;
sort?: SortPayload;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter?: any;
link?: boolean;
timeout?: number;
}