Skip to main content

Sensor hooks

These hooks manage sensor data throughout the application. They use SWR for data fetching with automatic caching and revalidation. The hooks are created via a factory pattern (createSensorHooks) that accepts an API adapter, allowing for different data sources. Components import hooks directly from src/core/hooks/sensors/sensors.ts.

Hooks

HookDescription
useSensorsFetches all sensors
useSensorFetches a single sensor by ID; includes update and delete mutations
useSensorsByBuildingFetches sensors filtered by building ID
useSensorsByAuthorFetches sensors filtered by author ID
useCreateSensorMutation hook for creating a new sensor

useSensors

Fetches all sensors from the API adapter. Returns an empty array while loading or if no sensors exist.

Signature

function useSensors(): {
sensors: Sensor[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

None.

Returns

FieldTypeDescription
sensorsSensor[]Array of all sensors, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { sensors, isLoading } = useSensors();

if (isLoading) return <Skeleton />;

return <SensorList sensors={sensors} />;

useSensor

Fetches a single sensor by ID. Also provides updateSensor and deleteSensor mutation functions. Passing null disables the fetch.

Signature

function useSensor(id: number | null): {
sensor: Sensor | null;
isLoading: boolean;
isError: Error | undefined;
updateSensor: (arg: Partial<Sensor>) => Promise<Sensor>;
isMutating: boolean;
updateError: Error | undefined;
updatedData: Sensor | undefined;
deleteSensor: () => Promise<Sensor>;
isDeleting: boolean;
deleteError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
idnumber | nullYesSensor ID to fetch, or null to disable fetching

Returns

FieldTypeDescription
sensorSensor | nullThe fetched sensor, or null if loading/not found
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state
updateSensor(arg: Partial<Sensor>) => Promise<Sensor>Trigger function to update the sensor
isMutatingbooleanTrue while update mutation is in progress
updateErrorError | undefinedError from update mutation
updatedDataSensor | undefinedReturned data from successful update
deleteSensor() => Promise<Sensor>Trigger function to delete the sensor
isDeletingbooleanTrue while delete mutation is in progress
deleteErrorError | undefinedError from delete mutation

Example

const { sensor, isLoading, updateSensor, deleteSensor } = useSensor(sensorId);

const handleUpdate = async () => {
await updateSensor({ name: "Updated Sensor" });
};

const handleDelete = async () => {
await deleteSensor();
};

Notes

On successful update, the hook revalidates:

  • The individual sensor cache key
  • The all-sensors list
  • Building-specific and author-specific sensor lists (both previous and new values if changed)

On successful delete, the hook:

  • Removes the sensor from its individual cache key without revalidation
  • Revalidates the all-sensors list and any associated building/author lists

useSensorsByBuilding

Fetches sensors filtered by building ID. Passing null disables the fetch.

Signature

function useSensorsByBuilding(buildingId: number | null): {
sensors: Sensor[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
buildingIdnumber | nullYesBuilding ID to filter by, or null to disable fetching

Returns

FieldTypeDescription
sensorsSensor[]Array of sensors for the building, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { sensors, isLoading } = useSensorsByBuilding(selectedBuildingId);

useSensorsByAuthor

Fetches sensors filtered by author ID. Passing null disables the fetch.

Signature

function useSensorsByAuthor(authorId: number | null): {
sensors: Sensor[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
authorIdnumber | nullYesAuthor ID to filter by, or null to disable fetching

Returns

FieldTypeDescription
sensorsSensor[]Array of sensors by the author, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { sensors, isLoading } = useSensorsByAuthor(currentUserId);

useCreateSensor

Mutation hook for creating a new sensor. On success, revalidates the all-sensors list and any associated building/author lists.

Signature

function useCreateSensor(): {
createSensor: (arg: { sensorData: Partial<Sensor> }) => Promise<Sensor>;
isMutating: boolean;
createError: Error | undefined;
createdData: Sensor | undefined;
}

Parameters

None.

Returns

FieldTypeDescription
createSensor(arg: { sensorData: Partial<Sensor> }) => Promise<Sensor>Trigger function to create a sensor
isMutatingbooleanTrue while creation is in progress
createErrorError | undefinedError from create mutation
createdDataSensor | undefinedThe newly created sensor on success

Example

const { createSensor, isMutating } = useCreateSensor();

const handleCreate = async () => {
await createSensor({
sensorData: {
name: "Temperature Sensor",
buildingId: 42,
},
});
};