diff --git a/app/components/device-detail/device-detail-box.tsx b/app/components/device-detail/device-detail-box.tsx index 354d9b35..fa9822d6 100644 --- a/app/components/device-detail/device-detail-box.tsx +++ b/app/components/device-detail/device-detail-box.tsx @@ -99,6 +99,11 @@ export default function DeviceDetailBox() { const sensorIds = new Set() const data = useLoaderData() + const exploreData = matches.find((match) => match.pathname === '/explore') + ?.loaderData as { user?: { id?: string } } | undefined + const isOwner = + typeof data.device?.userId === 'string' && + exploreData?.user?.id === data.device.userId const nodeRef = useRef(null) // state variables const [open, setOpen] = useState(true) @@ -111,7 +116,9 @@ export default function DeviceDetailBox() { const [sensors, setSensors] = useState() useEffect(() => { const sortedSensors = [...(data.sensors as any)].sort( - (a, b) => (a.id as unknown as number) - (b.id as unknown as number), + (a, b) => + (a.order ?? Number.MAX_SAFE_INTEGER) - + (b.order ?? Number.MAX_SAFE_INTEGER) || a.id.localeCompare(b.id), ) setSensors(sortedSensors) }, [data]) @@ -192,7 +199,7 @@ export default function DeviceDetailBox() { >
+ {isOwner && ( + + + {t('sensor_order_hint')}{' '} + + {t('sensor_order_hint_link')} + + + + )}
{sensors && sensors.map( @@ -649,7 +669,7 @@ export default function DeviceDetailBox() { onClick={() => { setOpen(true) }} - className="absolute bottom-[10px] left-4 flex cursor-pointer rounded-xl border border-gray-100 bg-white shadow-lg transition-colors duration-300 ease-in-out hover:brightness-90 sm:bottom-[30px] sm:left-[10px] dark:bg-zinc-800 dark:text-zinc-200 dark:opacity-90" + className="absolute bottom-2.5 left-4 flex cursor-pointer rounded-xl border border-gray-100 bg-white shadow-lg transition-colors duration-300 ease-in-out hover:brightness-90 sm:bottom-7.5 sm:left-2.5 dark:bg-zinc-800 dark:text-zinc-200 dark:opacity-90" > diff --git a/app/db/models/device.server.ts b/app/db/models/device.server.ts index aa615648..3051596c 100644 --- a/app/db/models/device.server.ts +++ b/app/db/models/device.server.ts @@ -387,6 +387,9 @@ export async function updateDevice( ) } + let nextSensorOrder = + Math.max(...existingSensors.map((sensor) => sensor.order ?? -1)) + 1 + for (const s of args.sensors) { const hasDeleted = 'deleted' in s const hasEdited = 'edited' in s @@ -423,7 +426,9 @@ export async function updateDevice( sensorType: s.sensorType, icon: s.icon, deviceId, + order: nextSensorOrder, }) + nextSensorOrder += 1 } else if (hasEdited && s._id) { const sensorExists = existingSensors.some( (existing) => existing.id === s._id, @@ -894,7 +899,7 @@ export async function createDevice(deviceData: any, userId: string) { Array.isArray(sensorsToAdd) && sensorsToAdd.length > 0 ) { - for (const sensorData of sensorsToAdd) { + for (const [index, sensorData] of sensorsToAdd.entries()) { const [newSensor] = await tx .insert(sensor) .values({ @@ -903,6 +908,7 @@ export async function createDevice(deviceData: any, userId: string) { sensorType: sensorData.sensorType, icon: sensorData.icon, deviceId: createdDevice.id, + order: sensorData.order ?? index, }) .returning() diff --git a/app/db/models/sensor.server.ts b/app/db/models/sensor.server.ts index 5908727b..9f9c2633 100644 --- a/app/db/models/sensor.server.ts +++ b/app/db/models/sensor.server.ts @@ -96,6 +96,8 @@ export async function getSensorsWithLastMeasurement( s.sensor_type AS "sensorType", s.icon, s.status, + s.device_id AS "deviceId", + s."order", json_agg( json_build_object( 'value', measure.value, @@ -113,7 +115,8 @@ export async function getSensorsWithLastMeasurement( LIMIT ${count} ) AS measure ON true WHERE s.device_id = ${deviceId} - GROUP BY s.id;`, + GROUP BY s.id + ORDER BY s."order" ASC, s.id ASC;`, ) const cast = [...result].map((r) => { diff --git a/public/locales/de/device-detail-box.json b/public/locales/de/device-detail-box.json index 2547185e..98c1ea89 100644 --- a/public/locales/de/device-detail-box.json +++ b/public/locales/de/device-detail-box.json @@ -8,6 +8,8 @@ "open_external_link": "Externen Link öffnen", "description": "Beschreibung", "sensors": "Sensoren", + "sensor_order_hint": "Du möchtest die Reihenfolge der Sensoren ändern? Das geht in den", + "sensor_order_hint_link": "Sensoreinstellungen", "compare_devices": "Geräte vergleichen", "choose_device_for_comparison": "Wähle ein Gerät auf der Karte für den Vergleich.", "open_device_details": "Öffne Gerätedetails" diff --git a/public/locales/en/device-detail-box.json b/public/locales/en/device-detail-box.json index b758a9c0..4214e94d 100644 --- a/public/locales/en/device-detail-box.json +++ b/public/locales/en/device-detail-box.json @@ -8,6 +8,8 @@ "open_external_link": "Open external link", "description": "Description", "sensors": "Sensors", + "sensor_order_hint": "Want to change the sensor order? You can reorder them in", + "sensor_order_hint_link": "sensor settings", "compare_devices": "Compare devices", "choose_device_for_comparison": "Choose a device on the map to compare with.", "open_device_details": "Open device details"