fix(web): satisfy React hook lint rules
This commit is contained in:
1 parent
bb68be14fa
commit
a4215f86fb
8 files changed
+374
-256
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { io, type Socket } from 'socket.io-client';
|
||||
import { io } from 'socket.io-client';
|
||||
import { API_BASE_URL } from '../config';
|
||||
import type { ApiRiotRefreshEvent } from './types';
|
||||
|
||||
@@ -19,37 +19,43 @@ export function useRiotRefreshSocket({
|
||||
parentJobId?: string;
|
||||
onEvent: (event: ApiRiotRefreshEvent) => void;
|
||||
}) {
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const subscriptionKey =
|
||||
puuid || parentJobId ? JSON.stringify([puuid, parentJobId]) : null;
|
||||
const onEventRef = useRef(onEvent);
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>('idle');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [connectionState, setConnectionState] = useState<{
|
||||
key: string;
|
||||
status: ConnectionStatus;
|
||||
error: string | null;
|
||||
}>({ key: '', status: 'idle', error: null });
|
||||
|
||||
useEffect(() => {
|
||||
onEventRef.current = onEvent;
|
||||
}, [onEvent]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!puuid && !parentJobId) {
|
||||
setConnectionStatus('idle');
|
||||
if (!subscriptionKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
setConnectionStatus('connecting');
|
||||
setError(null);
|
||||
|
||||
const socket = io(`${API_BASE_URL}/riot-refresh`, {
|
||||
transports: ['websocket'],
|
||||
});
|
||||
socketRef.current = socket;
|
||||
|
||||
socket.on('connect', () => {
|
||||
setConnectionStatus('connected');
|
||||
setConnectionState({
|
||||
key: subscriptionKey,
|
||||
status: 'connected',
|
||||
error: null,
|
||||
});
|
||||
socket.emit('subscribe', { puuid, parentJobId });
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
setConnectionStatus('disconnected');
|
||||
setConnectionState((current) => ({
|
||||
key: subscriptionKey,
|
||||
status: 'disconnected',
|
||||
error: current.key === subscriptionKey ? current.error : null,
|
||||
}));
|
||||
});
|
||||
|
||||
socket.on('riot-refresh', (event: ApiRiotRefreshEvent) => {
|
||||
@@ -57,17 +63,29 @@ export function useRiotRefreshSocket({
|
||||
});
|
||||
|
||||
socket.on('error', (payload: { message?: string }) => {
|
||||
setConnectionStatus('error');
|
||||
setError(payload.message ?? '전적 갱신 소켓 연결 중 오류가 발생했습니다.');
|
||||
setConnectionState({
|
||||
key: subscriptionKey,
|
||||
status: 'error',
|
||||
error:
|
||||
payload.message ?? '전적 갱신 소켓 연결 중 오류가 발생했습니다.',
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
socket.removeAllListeners();
|
||||
socket.disconnect();
|
||||
socketRef.current = null;
|
||||
setConnectionStatus('idle');
|
||||
};
|
||||
}, [parentJobId, puuid]);
|
||||
}, [parentJobId, puuid, subscriptionKey]);
|
||||
|
||||
return { connectionStatus, error };
|
||||
const hasCurrentConnection =
|
||||
subscriptionKey !== null && connectionState.key === subscriptionKey;
|
||||
return {
|
||||
connectionStatus:
|
||||
subscriptionKey === null
|
||||
? 'idle'
|
||||
: hasCurrentConnection
|
||||
? connectionState.status
|
||||
: 'connecting',
|
||||
error: hasCurrentConnection ? connectionState.error : null,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user