Lint & format (#159)

Co-authored-by: antanst <>
This commit is contained in:
Antonis Anastasiadis 2025-07-15 10:39:00 +03:00 committed by antanst
parent b9cfadf010
commit 4878c71618
47 changed files with 1436 additions and 815 deletions

View file

@ -1,4 +1,10 @@
import React, { createContext, useContext, useState, useCallback, useEffect } from 'react';
import React, {
createContext,
useContext,
useState,
useCallback,
useEffect,
} from 'react';
type TelegramStatus = 'healthy' | 'problem' | 'none';
@ -8,49 +14,57 @@ interface TelegramStatusContextType {
refreshStatus: () => Promise<void>;
}
const TelegramStatusContext = createContext<TelegramStatusContextType | undefined>(undefined);
const TelegramStatusContext = createContext<
TelegramStatusContextType | undefined
>(undefined);
export const TelegramStatusProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
export const TelegramStatusProvider: React.FC<{
children: React.ReactNode;
}> = ({ children }) => {
const [status, setStatus] = useState<TelegramStatus>('none');
const fetchTelegramStatus = useCallback(async (): Promise<TelegramStatus> => {
try {
// Check if user has telegram bot token
const profileResponse = await fetch('/api/profile', {
credentials: 'include',
});
if (!profileResponse.ok) {
return 'none';
}
const profileData = await profileResponse.json();
if (!profileData.telegram_bot_token) {
return 'none';
}
// Check polling status
const pollingResponse = await fetch('/api/telegram/polling-status', {
credentials: 'include',
});
if (!pollingResponse.ok) {
const fetchTelegramStatus =
useCallback(async (): Promise<TelegramStatus> => {
try {
// Check if user has telegram bot token
const profileResponse = await fetch('/api/profile', {
credentials: 'include',
});
if (!profileResponse.ok) {
return 'none';
}
const profileData = await profileResponse.json();
if (!profileData.telegram_bot_token) {
return 'none';
}
// Check polling status
const pollingResponse = await fetch(
'/api/telegram/polling-status',
{
credentials: 'include',
}
);
if (!pollingResponse.ok) {
return 'problem';
}
const pollingData = await pollingResponse.json();
if (pollingData.status?.running) {
return 'healthy';
} else {
return 'problem';
}
} catch (error) {
console.error('Error fetching Telegram status:', error);
return 'problem';
}
const pollingData = await pollingResponse.json();
if (pollingData.status?.running) {
return 'healthy';
} else {
return 'problem';
}
} catch (error) {
console.error('Error fetching Telegram status:', error);
return 'problem';
}
}, []);
}, []);
const refreshStatus = useCallback(async () => {
const newStatus = await fetchTelegramStatus();
@ -64,28 +78,38 @@ export const TelegramStatusProvider: React.FC<{ children: React.ReactNode }> = (
// Initial fetch and periodic refresh
useEffect(() => {
refreshStatus();
// Refresh every 30 seconds
const interval = setInterval(refreshStatus, 30000);
return () => clearInterval(interval);
}, [refreshStatus]);
// Listen for custom events to update status immediately
useEffect(() => {
const handleTelegramStatusChange = (event: CustomEvent<{ status: TelegramStatus }>) => {
const handleTelegramStatusChange = (
event: CustomEvent<{ status: TelegramStatus }>
) => {
setStatus(event.detail.status);
};
window.addEventListener('telegramStatusChanged', handleTelegramStatusChange as EventListener);
window.addEventListener(
'telegramStatusChanged',
handleTelegramStatusChange as EventListener
);
return () => {
window.removeEventListener('telegramStatusChanged', handleTelegramStatusChange as EventListener);
window.removeEventListener(
'telegramStatusChanged',
handleTelegramStatusChange as EventListener
);
};
}, []);
return (
<TelegramStatusContext.Provider value={{ status, updateStatus, refreshStatus }}>
<TelegramStatusContext.Provider
value={{ status, updateStatus, refreshStatus }}
>
{children}
</TelegramStatusContext.Provider>
);
@ -94,12 +118,16 @@ export const TelegramStatusProvider: React.FC<{ children: React.ReactNode }> = (
export const useTelegramStatus = () => {
const context = useContext(TelegramStatusContext);
if (context === undefined) {
throw new Error('useTelegramStatus must be used within a TelegramStatusProvider');
throw new Error(
'useTelegramStatus must be used within a TelegramStatusProvider'
);
}
return context;
};
// Utility function to dispatch status change events
export const dispatchTelegramStatusChange = (status: TelegramStatus) => {
window.dispatchEvent(new CustomEvent('telegramStatusChanged', { detail: { status } }));
};
window.dispatchEvent(
new CustomEvent('telegramStatusChanged', { detail: { status } })
);
};