Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Offline monitor #23

Merged
merged 2 commits into from
Mar 10, 2024
Merged

feat: Offline monitor #23

merged 2 commits into from
Mar 10, 2024

Conversation

rossbulat
Copy link
Collaborator

@rossbulat rossbulat commented Mar 10, 2024

Listens to online and offline events, displaying a prompt if the the connection appears to be offline.

Summary by CodeRabbit

  • New Features
    • Introduced enhanced offline handling with visual indicators to improve the user experience during network disruptions.
    • Added dark mode support for the offline warning display, ensuring consistency with user's theme preferences.

Copy link

coderabbitai bot commented Mar 10, 2024

Walkthrough

The recent updates focus on enhancing the application's resilience to offline scenarios. By introducing an Offline component and an OnlineStatusController, the application now actively monitors network status, enabling better user feedback during offline periods. This comprehensive approach ensures that the application remains user-friendly and responsive, regardless of internet connectivity, by displaying warnings and managing online/offline events efficiently.

Changes

Files Change Summary
src/Router.tsx
src/library/Offline/...
Added Offline component for enhanced offline handling.
src/contexts/Api/index.tsx
src/controllers/OnlineStatusController/...
Introduced OnlineStatusController for online/offline events.
src/library/Offline/Wrapper.ts Created Wrapper styled component with dark mode support.
src/library/Offline/index.tsx Implemented functionality for offline warning display.
src/controllers/OnlineStatusController/types.ts
src/types.ts
Added OnlineStatusEvent interface and custom event declaration.

🐇✨
In a world where connections might fade,
A clever rabbit's plan is laid.
With code and magic, hand in hand,
It weaves a net across the land.
"Fear not the offline," it decrees,
For in this code, your worries cease.
🌐❤️


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@rossbulat rossbulat merged commit 3b5644e into main Mar 10, 2024
5 checks passed
@rossbulat rossbulat deleted the rb-offline-monitor branch March 10, 2024 09:32
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 68e9c12 and 0b67b59.
Files selected for processing (7)
  • src/Router.tsx (2 hunks)
  • src/contexts/Api/index.tsx (2 hunks)
  • src/controllers/OnlineStatusController/index.tsx (1 hunks)
  • src/controllers/OnlineStatusController/types.ts (1 hunks)
  • src/library/Offline/Wrapper.ts (1 hunks)
  • src/library/Offline/index.tsx (1 hunks)
  • src/types.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • src/controllers/OnlineStatusController/types.ts
Additional comments: 8
src/controllers/OnlineStatusController/index.tsx (2)
  • 6-13: The implementation of initOnlineEvents method correctly sets up event listeners for 'offline' and 'online' events on the window object and dispatches a custom event with the online status. However, consider adding error handling for potential issues during event listener setup or dispatching to enhance robustness.
  • 16-24: The dispatchEvent method correctly creates and dispatches a custom event named 'online-status' with the online status detail. This method is well-implemented and follows best practices for custom event dispatching. No changes are necessary.
src/types.ts (2)
  • 5-5: The import of OnlineStatusEvent from controllers/OnlineStatusController/types is correctly done. This ensures that the custom event type is available for use in the application.
  • 16-16: The addition of the OnlineStatusEvent custom event to the DocumentEventMap interface is correctly implemented. This extension of the interface allows for strong typing of custom events, enhancing code maintainability and developer experience.
src/Router.tsx (2)
  • 12-12: The import of the Offline component from library/Offline is correctly done. This ensures that the component is available for use within the Router component.
  • 31-31: The placement of the <Offline /> component within the Router component's JSX structure is appropriate. This ensures that the offline prompt is rendered at a global level, making it accessible throughout the application. Good job on integrating this feature.
src/contexts/Api/index.tsx (2)
  • 13-13: The import of OnlineStatusController from controllers/OnlineStatusController is correctly done. This ensures that the controller is available for initializing online/offline events within the API context.
  • 154-157: The initialization of online/offline events using OnlineStatusController.initOnlineEvents() within the useEffect hook is correctly implemented. This ensures that the application starts listening for online/offline events as soon as the API context is initialized. Good job on integrating this feature to enhance the application's resilience to connectivity issues.

Comment on lines +6 to +29
export const Wrapper = styled.div`
background: var(--accent-color-primary);
border: 0.5px solid var(--border-primary-color);
/* TODO: make theme variable + dark mode support */
box-shadow:
0 2px 3px -1px rgba(0, 0, 0, 0.05),
0 1px 4px -2px rgba(0, 0, 0, 0.05);
position: fixed;
bottom: 0.5rem;
right: 0.5rem;
z-index: 999;
padding: 0.6rem 0.9rem;
border-radius: 0.6rem;
display: flex;

> h3,
svg {
color: var(--text-color-invert);
}

> svg {
margin-right: 0.5rem;
}
`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Wrapper styled component is correctly defined with appropriate styling for the offline notification. However, there's a TODO comment regarding making theme variables and adding dark mode support. It's important to address this to ensure consistency and accessibility across different themes.

Would you like me to help with implementing theme variables and dark mode support?

Comment on lines +11 to +36
export const Offline = () => {
// Whether the app is offline.
const [offline, setOffline] = useState<boolean>(false);

// Handle incoming online status updates.
const handleOnlineStatus = (e: Event): void => {
if (isCustomEvent(e)) {
const { online } = e.detail;
setOffline(!online);
}
};

// Listen for online status updates.
const documentRef = useRef<Document>(document);
useEventListener('online-status', handleOnlineStatus, documentRef);

if (!offline) {
return null;
}
return (
<Wrapper>
<FontAwesomeIcon icon={faWarning} />
<h3>Connection appears to be offline</h3>
</Wrapper>
);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Offline component is well-implemented, with clear state management and event listener setup for handling online status updates. The use of useState for tracking the offline status and useEventListener for listening to 'online-status' events is appropriate. The conditional rendering based on the offline status is also correctly done. However, consider adding a cleanup function for the event listener to prevent potential memory leaks in long-running applications.

+ useEffect(() => {
+   return () => documentRef.current.removeEventListener('online-status', handleOnlineStatus);
+ }, []);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const Offline = () => {
// Whether the app is offline.
const [offline, setOffline] = useState<boolean>(false);
// Handle incoming online status updates.
const handleOnlineStatus = (e: Event): void => {
if (isCustomEvent(e)) {
const { online } = e.detail;
setOffline(!online);
}
};
// Listen for online status updates.
const documentRef = useRef<Document>(document);
useEventListener('online-status', handleOnlineStatus, documentRef);
if (!offline) {
return null;
}
return (
<Wrapper>
<FontAwesomeIcon icon={faWarning} />
<h3>Connection appears to be offline</h3>
</Wrapper>
);
};
export const Offline = () => {
// Whether the app is offline.
const [offline, setOffline] = useState<boolean>(false);
// Handle incoming online status updates.
const handleOnlineStatus = (e: Event): void => {
if (isCustomEvent(e)) {
const { online } = e.detail;
setOffline(!online);
}
};
// Listen for online status updates.
const documentRef = useRef<Document>(document);
useEventListener('online-status', handleOnlineStatus, documentRef);
useEffect(() => {
return () => documentRef.current.removeEventListener('online-status', handleOnlineStatus);
}, []);
if (!offline) {
return null;
}
return (
<Wrapper>
<FontAwesomeIcon icon={faWarning} />
<h3>Connection appears to be offline</h3>
</Wrapper>
);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant