Fixed bug when retrieving k profiles from printer

This commit is contained in:
Martin Ziegler
2025-12-01 09:32:01 +01:00
parent 1cf5c8e9c3
commit faeae88aef
6 changed files with 99 additions and 54 deletions
+27 -7
View File
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
Gauge,
@@ -499,11 +499,37 @@ export function KProfilesView() {
queryFn: () => api.getKProfiles(selectedPrinter!, nozzleDiameter),
enabled: !!selectedPrinter,
retry: false,
staleTime: 0, // Always consider data stale to ensure fresh fetch
refetchOnMount: 'always', // Always refetch when component mounts
});
// Check if error is due to printer not being connected
const isOfflineError = kprofilesError?.message?.includes('not connected');
// Auto-select first connected printer
useEffect(() => {
if (!selectedPrinter && printers && printers.length > 0) {
const activePrinter = printers.find((p) => p.is_active);
if (activePrinter) {
setSelectedPrinter(activePrinter.id);
}
}
}, [selectedPrinter, printers]);
// Refetch profiles when printer selection changes
useEffect(() => {
if (selectedPrinter) {
// Delay refetch to ensure query is enabled after state update
const timer = setTimeout(() => {
refetchProfiles();
}, 150);
return () => clearTimeout(timer);
}
}, [selectedPrinter, nozzleDiameter]); // eslint-disable-line react-hooks/exhaustive-deps
// Get connected printers for display
const connectedPrinters = printers?.filter((p) => p.is_active) || [];
// Filter profiles based on search query, extruder filter, and flow type
const filteredProfiles = React.useMemo(() => {
if (!kprofiles?.profiles) return [];
@@ -532,12 +558,6 @@ export function KProfilesView() {
});
}, [kprofiles?.profiles, searchQuery, extruderFilter, flowTypeFilter]);
// Auto-select first connected printer
const connectedPrinters = printers?.filter((p) => p.is_active) || [];
if (!selectedPrinter && connectedPrinters.length > 0) {
setSelectedPrinter(connectedPrinters[0].id);
}
// Check if selected printer is dual-nozzle (auto-detected from MQTT temperature data)
const selectedPrinterData = printers?.find((p) => p.id === selectedPrinter);
const isDualNozzle = selectedPrinterData?.nozzle_count === 2;