fix: resolve WebSocket connection issues and API errors

- Fix React StrictMode double-mount causing WebSocket connection loops
  - Add isMountedRef to Tasks.tsx and NotificationContext.tsx
  - Delay WebSocket connection by 100ms to avoid race conditions
  - Check mounted state before reconnection attempts

- Fix React Router v7 deprecation warnings
  - Add future flags: v7_startTransition, v7_relativeSplatPath

- Fix CalendarView 422 API error
  - Send full ISO 8601 datetime format instead of date-only
  - Add URL encoding for query parameters

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beabigegg
2026-01-08 22:49:19 +08:00
parent a7c452ffd8
commit 934decd314
5 changed files with 55 additions and 26 deletions

View File

@@ -25,6 +25,7 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
const wsRef = useRef<WebSocket | null>(null)
const pingIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
const reconnectTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const isMountedRef = useRef(true)
const refreshUnreadCount = useCallback(async () => {
try {
@@ -168,10 +169,14 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
pingIntervalRef.current = null
}
// Attempt to reconnect after delay
reconnectTimeoutRef.current = setTimeout(() => {
connectWebSocket()
}, WS_RECONNECT_DELAY)
// Only attempt to reconnect if component is still mounted
if (isMountedRef.current) {
reconnectTimeoutRef.current = setTimeout(() => {
if (isMountedRef.current) {
connectWebSocket()
}
}, WS_RECONNECT_DELAY)
}
}
ws.onerror = (err) => {
@@ -184,23 +189,35 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
// Initial fetch and WebSocket connection
useEffect(() => {
isMountedRef.current = true
const token = localStorage.getItem('token')
if (token) {
refreshUnreadCount()
connectWebSocket()
// Delay WebSocket connection to avoid StrictMode race condition
const connectTimeout = setTimeout(() => {
if (isMountedRef.current) {
connectWebSocket()
}
}, 100)
return () => {
clearTimeout(connectTimeout)
isMountedRef.current = false
// Cleanup on unmount
if (wsRef.current) {
wsRef.current.close()
}
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current)
}
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current)
}
}
}
return () => {
// Cleanup on unmount
if (wsRef.current) {
wsRef.current.close()
}
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current)
}
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current)
}
isMountedRef.current = false
}
}, [refreshUnreadCount, connectWebSocket])