Spaces:
No application file
No application file
File size: 1,273 Bytes
9a16769 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import { useEffect } from 'react';
import axios from 'axios';
import { useStore } from '../store';
// Debounce helper
let debounceTimer: ReturnType<typeof setTimeout>;
export function useAIAdaptation() {
const {
waveAmplitude, temperature, waveExpansion, finScale,
setAIResult, setLoadingAI, setFluidField,
} = useStore();
// Call /api/adapt-fins whenever ocean parameters change (debounced 1.5s)
useEffect(() => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
setLoadingAI(true);
try {
const [finRes, fluidRes] = await Promise.all([
axios.post('/api/adapt-fins', {
waveAmplitude,
temperature,
waveExpansion,
currentFinScale: finScale,
}),
axios.post('/api/fluid-state', {
waveAmplitude,
waveExpansion,
time: Date.now() / 1000,
}),
]);
setAIResult(finRes.data);
setFluidField(fluidRes.data.field);
} catch (err) {
console.warn('AI adaptation error:', err);
} finally {
setLoadingAI(false);
}
}, 1500);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [waveAmplitude, temperature, waveExpansion]);
}
|