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

new-log-viewer: Persist active tab across sessions. #93

Merged
merged 2 commits into from
Oct 21, 2024

Conversation

junhaoliao
Copy link
Member

@junhaoliao junhaoliao commented Oct 15, 2024

References

new-log-viewer series: #45 #46 #48 #51 #52 #53 #54 #55 #56 #59 #60 #61 #62 #63 #66 #67 #68 #69 #70 #71 #72 #73 #74 #76 #77 #78 #79 #80 #81 #82 #83 #84 #89 #91

Description

  1. Add config option "INITIAL_TAB_NAME" into the config utilities.
  2. Use the config value as the initial tab name.
  3. Update the config value whenever tab change happens.

Validation performed

  1. Cleared browser caches (especially the data in localStorage).
  2. Started debug server as specified in Add support for loading files, decoding them as JSONL, and formatting them using a Logback-like format string. #46.
  3. Observed the panel was expanded initially with the "File info" tab being active.
  4. Clicked the "File info" tab button to collapse the panel.
  5. Refreshed the browser page so that the log viewer app got reloaded. Observed that the panel was collapsed when the app was loaded.
  6. Clicked the "File info" tab button to expand the panel and activate the tab.
  7. Refreshed the browser page so that the log viewer app got reloaded. Observed that the panel was expanded initially with the "File info" tab being active when the app was loaded.

Summary by CodeRabbit

  • New Features

    • Enhanced sidebar functionality with dynamic tab management based on configuration.
    • Improved configuration management for initial tab settings.
  • Bug Fixes

    • Streamlined logic for handling active tab and panel width.
  • Documentation

    • Updated configuration keys for better clarity and usage.

Copy link

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes involve significant updates to the Sidebar component, enhancing its state management through the introduction of React hooks. The initial tab configuration is now dynamically derived rather than hardcoded. Additionally, the configuration management has been improved across multiple files, with new entries added to enums and updates to configuration functions to support the new INITIAL_TAB_NAME key.

Changes

File Path Change Summary
src/components/CentralContainer/Sidebar/index.tsx Updated Sidebar component to use useEffect and useMemo for managing active tab state and panel width. Introduced initialTabName derived from configuration. Streamlined handleActiveTabNameChange.
src/typings/config.ts Added import for TAB_NAME, introduced INITIAL_TAB_NAME in CONFIG_KEY and LOCAL_STORAGE_KEY enums, and updated ConfigMap type to include the new key.
src/utils/config.ts Added CONFIG_KEY.INITIAL_TAB_NAME to CONFIG_DEFAULT, updated setConfig and getConfig functions to handle the new configuration key for local storage.

Sequence Diagram(s)

sequenceDiagram
    participant Sidebar as Sidebar Component
    participant Config as Config Management
    participant LocalStorage as Local Storage

    Sidebar->>Config: getConfig(INITIAL_TAB_NAME)
    Config-->>Sidebar: return initialTabName
    Sidebar->>Sidebar: useEffect to set panel width
    Sidebar->>LocalStorage: setConfig(INITIAL_TAB_NAME, value)
    LocalStorage-->>Sidebar: confirm storage
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a75f768 and 6129883.

📒 Files selected for processing (1)
  • new-log-viewer/src/utils/config.ts (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • new-log-viewer/src/utils/config.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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>, please review it.
    • Generate unit testing code 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
new-log-viewer/src/typings/config.ts (1)

31-31: LGTM: ConfigMap updated correctly, consider adding a comment

The INITIAL_TAB_NAME entry has been correctly added to the ConfigMap type with the proper typing. This ensures type-safe usage of the new configuration option.

Consider adding a brief comment above this line to explain the purpose of the INITIAL_TAB_NAME configuration, for improved code documentation. For example:

// Stores the name of the initially active tab
[CONFIG_KEY.INITIAL_TAB_NAME]: TAB_NAME,
new-log-viewer/src/utils/config.ts (2)

25-25: LGTM! Consider using a type assertion for improved type safety.

The addition of INITIAL_TAB_NAME to CONFIG_DEFAULT is consistent with the PR objectives. It's a good practice to set a default value for the initial tab.

For improved type safety, consider using a type assertion:

[CONFIG_KEY.INITIAL_TAB_NAME]: TAB_NAME.FILE_INFO as const,

This ensures that the type of INITIAL_TAB_NAME is specifically TAB_NAME.FILE_INFO rather than the more general TAB_NAME type.


93-95: LGTM! Consider adding error handling for localStorage operations.

The implementation correctly stores the INITIAL_TAB_NAME value in localStorage, consistent with how other configuration values are handled.

Consider adding error handling for the localStorage operation:

case CONFIG_KEY.INITIAL_TAB_NAME:
    try {
        window.localStorage.setItem(CONFIG_KEY.INITIAL_TAB_NAME, value.toString());
    } catch (error) {
        console.error(`Failed to set ${CONFIG_KEY.INITIAL_TAB_NAME} in localStorage:`, error);
    }
    break;

This will catch and log any errors that might occur during the localStorage operation, such as when storage is full or disabled.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ae24669 and a75f768.

📒 Files selected for processing (3)
  • new-log-viewer/src/components/CentralContainer/Sidebar/index.tsx (4 hunks)
  • new-log-viewer/src/typings/config.ts (3 hunks)
  • new-log-viewer/src/utils/config.ts (4 hunks)
🧰 Additional context used
🔇 Additional comments (8)
new-log-viewer/src/typings/config.ts (3)

2-2: LGTM: New import added correctly

The import of TAB_NAME from "./tab" is correctly added and necessary for the new INITIAL_TAB_NAME configuration.


13-13: LGTM: Enum additions are correct and consistent

The INITIAL_TAB_NAME entries have been correctly added to both CONFIG_KEY and LOCAL_STORAGE_KEY enums. The naming and structure are consistent with existing entries.

Also applies to: 23-23


Line range hint 1-31: Summary: Changes align well with PR objectives

The modifications to this file successfully implement the necessary type definitions and configurations to support the new INITIAL_TAB_NAME feature. These changes align well with the PR objective of persisting the active tab across sessions.

The additions are well-integrated into the existing code structure, maintaining consistency in naming conventions and type safety. This implementation provides a solid foundation for the feature's functionality in other parts of the application.

new-log-viewer/src/utils/config.ts (1)

Line range hint 1-168: Overall assessment: Changes meet PR objectives with minor improvements needed.

The implementation successfully adds support for persisting the initial tab name across sessions, meeting the PR objectives. The changes are well-integrated into the existing code structure and follow established patterns.

Key points:

  1. The addition of INITIAL_TAB_NAME to CONFIG_DEFAULT is appropriate.
  2. The setConfig and getConfig functions have been correctly updated to handle the new configuration key.
  3. Minor improvements can be made for type safety and error handling.
  4. An inconsistency in the localStorage key usage needs to be addressed.

Once the suggested changes are implemented, particularly fixing the localStorage key inconsistency, the code will be ready for merge.

new-log-viewer/src/components/CentralContainer/Sidebar/index.tsx (4)

3-4: Imports are correctly added.

The addition of useEffect and useMemo to the imports is appropriate for the hooks used in this component.


9-14: Configuration imports are necessary and well-placed.

Importing CONFIG_KEY, getConfig, and setConfig is essential for managing the configuration state within the component.


63-71: Persisting active tab state is correctly implemented.

The logic within handleActiveTabNameChange effectively updates the active tab, adjusts the panel width, and persists the active tab name to the configuration. This ensures a consistent user experience across sessions.


103-113: Initialization effect accurately manages panel display.

The useEffect hook appropriately sets the panel width based on the initialTabName. This ensures the sidebar panel is correctly displayed or hidden upon initial load, aligning with the user's last active tab preference.

Comment on lines +132 to +134
case CONFIG_KEY.INITIAL_TAB_NAME:
value = window.localStorage.getItem(LOCAL_STORAGE_KEY.INITIAL_TAB_NAME);
break;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

LGTM! Fix inconsistency in localStorage key usage.

The implementation correctly retrieves the INITIAL_TAB_NAME value from localStorage. However, there's an inconsistency in the key used to access the value.

In the setConfig function, CONFIG_KEY.INITIAL_TAB_NAME is used as the localStorage key, but here LOCAL_STORAGE_KEY.INITIAL_TAB_NAME is used. To fix this inconsistency, replace line 133 with:

value = window.localStorage.getItem(CONFIG_KEY.INITIAL_TAB_NAME);

This ensures that the same key is used for both setting and getting the INITIAL_TAB_NAME value.

Comment on lines +52 to +53
const initialTabName = useMemo(() => getConfig(CONFIG_KEY.INITIAL_TAB_NAME), []);
const [activeTabName, setActiveTabName] = useState<TAB_NAME>(initialTabName);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle potential undefined initialTabName.

When retrieving initialTabName from the configuration, there's a possibility it might be undefined or not set. To ensure robustness, consider providing a default value:

- const initialTabName = useMemo(() => getConfig(CONFIG_KEY.INITIAL_TAB_NAME), []);
+ const initialTabName = useMemo(() => getConfig(CONFIG_KEY.INITIAL_TAB_NAME) || TAB_NAME.DEFAULT_TAB, []);

Replace TAB_NAME.DEFAULT_TAB with an appropriate default tab name to prevent any unintended behaviour.

📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const initialTabName = useMemo(() => getConfig(CONFIG_KEY.INITIAL_TAB_NAME), []);
const [activeTabName, setActiveTabName] = useState<TAB_NAME>(initialTabName);
const initialTabName = useMemo(() => getConfig(CONFIG_KEY.INITIAL_TAB_NAME) || TAB_NAME.DEFAULT_TAB, []);
const [activeTabName, setActiveTabName] = useState<TAB_NAME>(initialTabName);

Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

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

Code looks pretty good to me. I had one comment, otherwise i am happy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add an empty case into testConfig()? I know it wont do anything but at least its shows we considered it. See suggestion

        case CONFIG_KEY.INITIAL_TAB_NAME:
            break;

@davemarco davemarco self-requested a review October 21, 2024 14:09
Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

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

LGTM

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.

2 participants