Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions src/app/components/offline/OfflineStatusIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import {
Wifi,
Expand Down Expand Up @@ -29,11 +29,51 @@
const [showTooltip, setShowTooltip] = useState(false);
const [showSettings, setShowSettings] = useState(false);
const [isMounted, setIsMounted] = useState(false);
const [announcement, setAnnouncement] = useState('');

const triggerRef = useRef<HTMLButtonElement>(null);
const panelRef = useRef<HTMLDivElement>(null);
const prevOnlineRef = useRef(true);
const prevSyncRef = useRef('idle');

React.useEffect(() => {
setIsMounted(true);
}, []);

React.useEffect(() => {
if (prevOnlineRef.current !== isOnline) {
setAnnouncement(isOnline ? 'You are back online.' : 'Connection lost. You are offline.');
prevOnlineRef.current = isOnline;
}
}, [isOnline]);

Check failure on line 48 in src/app/components/offline/OfflineStatusIndicator.tsx

View workflow job for this annotation

GitHub Actions / type-check

Variable 'isOnline' is used before being assigned.

Check failure on line 48 in src/app/components/offline/OfflineStatusIndicator.tsx

View workflow job for this annotation

GitHub Actions / type-check

Block-scoped variable 'isOnline' used before its declaration.

React.useEffect(() => {
if (prevSyncRef.current !== syncStatus) {
if (syncStatus === 'error') {
setAnnouncement('Sync failed. Please try again.');
} else if (syncStatus === 'synced') {
setAnnouncement('Sync complete.');
}
prevSyncRef.current = syncStatus;
}
}, [syncStatus]);

Check failure on line 59 in src/app/components/offline/OfflineStatusIndicator.tsx

View workflow job for this annotation

GitHub Actions / type-check

Variable 'syncStatus' is used before being assigned.

Check failure on line 59 in src/app/components/offline/OfflineStatusIndicator.tsx

View workflow job for this annotation

GitHub Actions / type-check

Block-scoped variable 'syncStatus' used before its declaration.

React.useEffect(() => {
if (announcement) {
const timer = setTimeout(() => setAnnouncement(''), 7000);
return () => clearTimeout(timer);
}
}, [announcement]);

React.useEffect(() => {
if (showTooltip && panelRef.current) {
const focusable = panelRef.current.querySelector<HTMLElement>(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
focusable?.focus();
}
}, [showTooltip]);

const {
isOnline,
isOfflineModeEnabled,
Expand Down Expand Up @@ -146,10 +186,29 @@
}
};

const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Escape' && showTooltip) {
setShowTooltip(false);
e.stopPropagation();
triggerRef.current?.focus();
}
};

return (
<div className={`relative ${className}`}>
<div className={`relative ${className}`} onKeyDown={handleKeyDown}>
{/* Announcements for screen readers */}
<span
role="status"
aria-live="assertive"
aria-atomic="true"
className="sr-only"
>
{announcement}
</span>

{/* Main Status Indicator */}
<button
ref={triggerRef}
onClick={() => setShowTooltip(!showTooltip)}
onMouseEnter={() => setShowTooltip(true)}
onMouseLeave={() => setShowTooltip(false)}
Expand All @@ -172,6 +231,7 @@
<AnimatePresence>
{showTooltip && (
<motion.div
ref={panelRef}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10 }}
Expand Down
Loading