Skip to content

Commit c71efa1

Browse files
committed
Improved disconnect logic
1 parent 23c1771 commit c71efa1

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/app/muscle-strength/page.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface DeviceConfig {
4949

5050
const defaultConfig: DeviceConfig = {
5151
maxChannels: 3,
52-
sampleLength: 7,
52+
sampleLength: 7,
5353
hasBattery: true,
5454
name: ""
5555
};
@@ -280,12 +280,6 @@ const MuscleStrength = () => {
280280
if (!containerRef.current) return;
281281
const ro = new ResizeObserver(() => {
282282
createCanvasElements();
283-
rebuildInfoBoxes(); // <-- whatever your right-hand sizing fn is
284-
285-
function rebuildInfoBoxes() {
286-
console.log("Rebuilding info boxes...");
287-
// Add your logic here
288-
}
289283
});
290284
ro.observe(containerRef.current);
291285
return () => ro.disconnect();
@@ -777,7 +771,7 @@ const MuscleStrength = () => {
777771
}
778772
prevSampleCounter = sampleCounter;
779773
}
780-
channelData.push(sampleCounter);
774+
channelData.push(sampleCounter);
781775

782776
// Process all channels based on device configuration
783777
for (let channel = 0; channel < config.maxChannels; channel++) {
@@ -962,7 +956,6 @@ const MuscleStrength = () => {
962956
}
963957
}
964958

965-
966959
async function disconnect(): Promise<void> {
967960
try {
968961
setIsLoading(true); // Show loading while disconnecting
@@ -1036,6 +1029,9 @@ const MuscleStrength = () => {
10361029
deviceConfigRef.current = defaultConfig;
10371030
setDeviceConfig(defaultConfig);
10381031

1032+
// IMPORTANT: Reset selectedChannels to default 3 channels
1033+
setSelectedChannels([0, 1, 2]);
1034+
10391035
// Reset sample tracking
10401036
prevSampleCounter = null;
10411037
channelData = [];
@@ -1044,8 +1040,8 @@ const MuscleStrength = () => {
10441040

10451041
// Clear lines and sweep positions
10461042
linesRefs.current = [];
1047-
sweepPositions.current = new Array(deviceConfig.maxChannels).fill(0);
1048-
currentSweepPos.current = new Array(deviceConfig.maxChannels).fill(0);
1043+
sweepPositions.current = new Array(3).fill(0); // Hardcode to 3 for default
1044+
currentSweepPos.current = new Array(3).fill(0);
10491045

10501046
// Reset envelope filters
10511047
envelopeFilters.current = [];
@@ -1068,7 +1064,7 @@ const MuscleStrength = () => {
10681064
}
10691065

10701066
// Reset band power data
1071-
const emptyBandData = Array(deviceConfig.maxChannels).fill(0);
1067+
const emptyBandData = Array(3).fill(0); // Hardcode to 3 for default
10721068
setBandPowerData(emptyBandData);
10731069
prevBandPowerData.current = emptyBandData;
10741070

@@ -1078,6 +1074,12 @@ const MuscleStrength = () => {
10781074
// Force re-render
10791075
setRefreshKey(prev => prev + 1);
10801076

1077+
// CRITICAL FIX: Recreate canvas elements with default 3 channels
1078+
// Use setTimeout to ensure state updates have completed
1079+
setTimeout(() => {
1080+
createCanvasElements();
1081+
}, 0);
1082+
10811083
setIsLoading(false);
10821084

10831085
// Optional: Show success message
@@ -1090,6 +1092,17 @@ const MuscleStrength = () => {
10901092
setIsConnected(false);
10911093
connectedDeviceRef.current = null;
10921094
batteryCharacteristicRef.current = null;
1095+
1096+
// Still reset to default config
1097+
deviceConfigRef.current = defaultConfig;
1098+
setDeviceConfig(defaultConfig);
1099+
setSelectedChannels([0, 1, 2]);
1100+
1101+
// Recreate canvas elements
1102+
setTimeout(() => {
1103+
createCanvasElements();
1104+
}, 0);
1105+
10931106
setIsLoading(false);
10941107

10951108
toast.error("Error during disconnection");

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"moduleResolution": "node",
1616
"resolveJsonModule": true,
1717
"isolatedModules": true,
18-
"jsx": "preserve",
18+
"jsx": "react-jsx",
1919
"incremental": true,
2020
"plugins": [
2121
{

0 commit comments

Comments
 (0)