feat: weather conditions check from stroy-bot-main
Ports the weather check added to stroy-bot-main on 2026-10-16: daily station weather, report positions with weather per shift and the AI assessment of working conditions. Opens as ?page=weather&start=&end= instead of the #weathercheck hash, and hub dates survive the way back. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import * as React from 'react';
|
||||
import { useAppServices } from '../../app/AppServicesContext';
|
||||
import { HttpError } from '../../services/http/IHttpClient';
|
||||
import { fetchWeatherCheck } from './weatherRepository';
|
||||
import { IWeatherCheck, IWeatherPeriod } from './weatherTypes';
|
||||
import { WEATHER_TEXTS } from './texts';
|
||||
|
||||
export type WeatherLoadStatus =
|
||||
| { kind: 'idle' }
|
||||
| { kind: 'loading' }
|
||||
| { kind: 'error'; message: string }
|
||||
| { kind: 'ready'; data: IWeatherCheck };
|
||||
|
||||
/** Текст ошибки как в main: «статус текст» для ответа API, иначе сообщение ошибки. */
|
||||
export function weatherErrorDetails(error: unknown): string {
|
||||
if (error instanceof HttpError) return `${error.status} ${error.statusText}`;
|
||||
return error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Загрузка сверки погоды за период. Без проекта или периода — ничего не грузит (idle).
|
||||
* setData — для замены позиций после оценки ИИ.
|
||||
*/
|
||||
export function useWeatherCheck(
|
||||
projectId: number | undefined,
|
||||
period: IWeatherPeriod | undefined
|
||||
): { status: WeatherLoadStatus; setData: (update: (data: IWeatherCheck) => IWeatherCheck) => void } {
|
||||
const services = useAppServices();
|
||||
const [status, setStatus] = React.useState<WeatherLoadStatus>({ kind: 'idle' });
|
||||
const start = period?.start;
|
||||
const end = period?.end;
|
||||
|
||||
React.useEffect(() => {
|
||||
if (projectId === undefined || !start || !end) {
|
||||
setStatus({ kind: 'idle' });
|
||||
return;
|
||||
}
|
||||
let isCancelled = false;
|
||||
setStatus({ kind: 'loading' });
|
||||
fetchWeatherCheck(services.tnsuApi, services.settings.apiBaseUrl, projectId, { start, end })
|
||||
.then((data) => {
|
||||
if (!isCancelled) setStatus({ kind: 'ready', data });
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
console.error('Ошибка загрузки сверки погоды:', error);
|
||||
if (!isCancelled) setStatus({ kind: 'error', message: WEATHER_TEXTS.loadError(weatherErrorDetails(error)) });
|
||||
});
|
||||
return () => {
|
||||
isCancelled = true;
|
||||
};
|
||||
}, [services, projectId, start, end]);
|
||||
|
||||
const setData = React.useCallback((update: (data: IWeatherCheck) => IWeatherCheck): void => {
|
||||
setStatus((prev) => (prev.kind === 'ready' ? { kind: 'ready', data: update(prev.data) } : prev));
|
||||
}, []);
|
||||
|
||||
return { status, setData };
|
||||
}
|
||||
Reference in New Issue
Block a user