autumn-js
Version:
Autumn JS Library
49 lines (45 loc) • 1.2 kB
JavaScript
"use client";
import {
AutumnError
} from "./chunk-OCZFV7OX.mjs";
// src/libraries/react/hooks/useEntityProvider.tsx
import { useState } from "react";
var useEntityProvider = ({ client }) => {
const [entity, setEntity] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [lastParams, setLastParams] = useState(null);
const fetchEntity = async ({
entityId,
params
}) => {
if (!entityId) {
console.warn("(Autumn) No entity ID provided in useEntity hook");
return;
}
setIsLoading(true);
setLastParams(params || null);
try {
const { data, error: error2 } = await client.entities.get(entityId, params);
if (error2) {
setError(error2);
setEntity(null);
} else {
setEntity(data);
setError(null);
}
} catch (error2) {
setError(
new AutumnError({
message: error2?.message || "Failed to fetch entity",
code: "entity_fetch_failed"
})
);
}
setIsLoading(false);
};
return { entity, isLoading, error, refetch: fetchEntity, lastParams };
};
export {
useEntityProvider
};