Skip to main content

useBuilding hooks

Hooks for managing building entities in the CDT platform. Used throughout the application wherever building data is displayed or modified (building details panels, map overlays, building lists). All hooks use SWR for data fetching with automatic caching and revalidation, and SWR Mutation for create/update operations.

Hooks

HookDescription
useBuildingsFetches all buildings
useBuildingFetches a single building by ID, includes update mutation
useBuildingsByOsmFetches buildings matching an OpenStreetMap ID
useBuildingOsmIdsFetches all OSM IDs that have associated buildings
useCreateBuildingCreates a new building

useBuildings

Fetches the complete list of buildings. Returns an empty array while loading or on error.

Signature

function useBuildings(): UseBuildingsReturn

Parameters

None.

Returns

FieldTypeDescription
buildingsBuilding[]Array of buildings, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { buildings, isLoading } = useBuildings();

if (isLoading) return <Skeleton />;

return (
<ul>
{buildings.map((b) => (
<li key={b.id}>{b.name}</li>
))}
</ul>
);

Notes


useBuilding

Fetches a single building by ID. Also provides an updateBuilding mutation that revalidates related caches on success.

Signature

function useBuilding(id: number | null): UseBuildingReturn

Parameters

ParamTypeRequiredDescription
idnumber | nullYesBuilding ID. Pass null to skip fetching.

Returns

FieldTypeDescription
buildingBuilding | nullThe fetched building, or null if not loaded
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state
updateBuilding(arg: Partial<Building>) => Promise<Building>Mutation trigger to update the building
isMutatingbooleanWhether an update is in progress
updateErrorError | undefinedError from the most recent update attempt
updatedDataBuilding | undefinedResponse data from successful update

Example

const { building, isLoading, updateBuilding, isMutating } = useBuilding(buildingId);

if (isLoading) return <Skeleton />;
if (!building) return <NotFound />;

const handleRename = async (name: string) => {
await updateBuilding({ name });
};

On successful update, the hook revalidates ["building", id], ["buildings"], and ["filesByBuilding", id, ""] cache keys.


useBuildingsByOsm

Fetches all buildings associated with a given OpenStreetMap ID.

Signature

function useBuildingsByOsm(osmId: number | null): UseBuildingsByOsmReturn

Parameters

ParamTypeRequiredDescription
osmIdnumber | nullYesOpenStreetMap building ID. Pass null to skip fetching.

Returns

FieldTypeDescription
buildingsBuilding[]Buildings matching the OSM ID, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { buildings, isLoading } = useBuildingsByOsm(selectedOsmId);

useBuildingOsmIds

Fetches the list of all OSM IDs that have buildings in the system.

Signature

function useBuildingOsmIds(): UseBuildingOsmIdsReturn

Parameters

None.

Returns

FieldTypeDescription
osmIdsnumber[]Array of OSM IDs, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { osmIds, isLoading } = useBuildingOsmIds();

// Use to highlight buildings on map that exist in the database

useCreateBuilding

Creates a new building. Revalidates the buildings list on success.

Signature

function useCreateBuilding(): UseCreateBuildingReturn

Parameters

None.

Returns

FieldTypeDescription
createBuilding(arg: { buildingData: Partial<Building>, organizationId: string }) => Promise<Building>Mutation trigger
isMutatingbooleanWhether creation is in progress
createErrorError | undefinedError from the most recent creation attempt
createdDataBuilding | undefinedThe newly created building on success

Example

const { createBuilding, isMutating, createError } = useCreateBuilding();

const handleSubmit = async (formData: BuildingFormData) => {
await createBuilding({
buildingData: formData,
organizationId: currentOrg.id,
});
};

Notes

On successful creation, the hook revalidates the ["buildings"] cache key.