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

fix: resolve the automatic save state invalid after refresh #1004

Merged
merged 3 commits into from
Jan 13, 2025

Conversation

SonyLeo
Copy link
Contributor

@SonyLeo SonyLeo commented Jan 8, 2025

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

自动保存打开后,刷新页面,自动保存会关闭

Issue Number: N/A

What is the new behavior?

自动保存 状态持久化到 本地存储 中,刷新页面 状态不丢失

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Added auto-save functionality with the ability to toggle and persist auto-save preferences.
    • Introduced local storage management for auto-save status.
  • Improvements

    • Enhanced user experience by allowing users to control auto-save settings.
    • Implemented persistent storage of auto-save preferences across sessions.

Copy link
Contributor

coderabbitai bot commented Jan 8, 2025

Walkthrough

This pull request introduces modifications to implement auto-save functionality within the application. Changes are made across three files, adding methods to manage auto-save status using local storage. The user can toggle auto-save preferences, which are persistently stored and retrieved between sessions. These updates enhance the save toolbar's functionality by allowing for a more configurable saving experience.

Changes

File Change Summary
packages/toolbars/save/src/Main.vue - Added imports for setAutoSaveStatus and getAutoSaveStatus
- Updated onMounted hook to initialize auto-save status
- Renamed autoSave method to saveConfig and updated its implementation
packages/toolbars/save/src/js/index.js - Added getAutoSaveStatus() method to retrieve auto-save status
- Added setAutoSaveStatus(status) method to store auto-save status
- Updated constants import to include AUTO_SAVED
packages/utils/src/constants/index.js - Added new constant AUTO_SAVED with value 'tiny-engine-auto-saved'

Possibly related PRs

Suggested reviewers

  • hexqi

Poem

🐰 A rabbit's tale of saving grace,
Where bits and bytes find their place,
Auto-save, a magical spell,
Protecting work we love so well!
Click and store, no worries here,
Our digital friend is always near! 💾


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5a20f93 and c319024.

📒 Files selected for processing (1)
  • packages/toolbars/save/src/Main.vue (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/toolbars/save/src/Main.vue
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@github-actions github-actions bot added bug Something isn't working refactor-main refactor/develop branch feature labels Jan 8, 2025
Copy link
Contributor

@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: 0

🧹 Nitpick comments (4)
packages/toolbars/save/src/js/index.js (2)

179-186: Consider adding error logging.

The error handling is appropriate, but consider logging errors to help with debugging issues in production.

 export const getAutoSaveStatus = () => {
   try {
     const value = localStorage.getItem(AUTO_SAVED)
     return JSON.parse(value) ?? false
-  } catch {
+  } catch (error) {
+    console.error('Failed to get auto-save status:', error)
     return false
   }
 }

188-195: Add JSDoc comments for better documentation.

The function implementation is correct, but adding JSDoc comments would improve maintainability.

+/**
+ * Sets the auto-save status in localStorage
+ * @param {boolean} status - The auto-save status to store
+ * @returns {boolean} - True if the status was successfully stored, false otherwise
+ */
 export const setAutoSaveStatus = (status) => {
   try {
     localStorage.setItem(AUTO_SAVED, JSON.stringify(status))
     return true
-  } catch {
+  } catch (error) {
+    console.error('Failed to set auto-save status:', error)
     return false
   }
 }
packages/toolbars/save/src/Main.vue (2)

148-151: Add error handling and loading state for initialization.

Consider adding error handling and a loading state during initialization to improve user experience.

+ const isInitializing = ref(true)
  onMounted(() => {
+   try {
      state.checked = getAutoSaveStatus()
+     if (state.checked) {
+       saveSetTimeout()
+     }
+   } catch (error) {
+     console.error('Failed to initialize auto-save:', error)
+     useNotify({
+       type: 'error',
+       message: 'Failed to load auto-save settings'
+     })
+   } finally {
+     isInitializing.value = false
+   }
  })

140-147: Add user feedback for save settings.

Consider providing feedback when save settings are updated or when errors occur.

  const autoSave = () => {
-   setAutoSaveStatus(state.checked)
+   if (setAutoSaveStatus(state.checked)) {
+     useNotify({
+       type: 'success',
+       message: `Auto-save ${state.checked ? 'enabled' : 'disabled'}`
+     })
+   } else {
+     useNotify({
+       type: 'error',
+       message: 'Failed to update auto-save settings'
+     })
+     // Revert the checkbox state on error
+     state.checked = getAutoSaveStatus()
+     return
+   }
    if (state.checked) {
      saveSetTimeout()
    } else {
      clearTimeout(state.preservationTime)
    }
  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a14a508 and 11fa8ce.

📒 Files selected for processing (3)
  • packages/toolbars/save/src/Main.vue (2 hunks)
  • packages/toolbars/save/src/js/index.js (2 hunks)
  • packages/utils/src/constants/index.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (1)
packages/utils/src/constants/index.js (1)

101-102: LGTM! The constant follows the established pattern.

The new AUTO_SAVED constant follows the project's naming convention and is appropriately placed among other storage-related constants.

hexqi
hexqi previously approved these changes Jan 9, 2025
@hexqi hexqi added this to the v2.2.0 milestone Jan 13, 2025
Copy link
Contributor

@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

🧹 Nitpick comments (1)
packages/toolbars/save/src/Main.vue (1)

139-145: Consider triggering an immediate save when auto-save is enabled.

The current implementation sets up auto-save for future intervals but doesn't perform an immediate save when auto-save is enabled. This might not meet user expectations, as they might expect their current changes to be saved immediately when enabling auto-save.

 const saveConfig = () => {
   setAutoSaveStatus(state.checked)
   if (state.checked) {
+    openApi() // Trigger immediate save
     saveSetTimeout()
   } else {
     clearTimeout(state.preservationTime)
   }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 11fa8ce and 5a20f93.

📒 Files selected for processing (1)
  • packages/toolbars/save/src/Main.vue (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (3)
packages/toolbars/save/src/Main.vue (3)

23-23: LGTM! Method name change improves clarity.

The rename from autoSave to saveConfig better reflects the method's purpose and follows the verb+noun naming convention.


58-58: LGTM! Required imports added for auto-save persistence.

The new imports support the auto-save state persistence functionality, which aligns with the PR objective.

Also applies to: 64-64


166-166: LGTM! Method correctly exposed to template.

The saveConfig method is properly exposed in the component's return statement.

@hexqi hexqi merged commit d955cd8 into opentiny:refactor/develop Jan 13, 2025
1 check passed
@SonyLeo SonyLeo deleted the fix/refreshStatusStorage branch January 21, 2025 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working refactor-main refactor/develop branch feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants