Skip to content

Add cachedWritableStores to shared lib, and update hideZeroBalanceVaults to use the cached version #1698

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

Merged
merged 25 commits into from
May 4, 2025

Conversation

hardingjam
Copy link
Contributor

@hardingjam hardingjam commented Apr 25, 2025

Motivation

Move the cachedWritableStores to the shared lib, so that filter choices can be saved between refreshes

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Introduced a persistent setting to hide zero-balance vaults, which remembers your preference across sessions.
    • Added new utilities for persistent, typed data stores synchronized with localStorage.
  • Bug Fixes

    • Improved handling of corrupted or malformed stored data for user settings.
  • Chores

    • Streamlined store imports and exports by consolidating caching utilities into a shared package.
    • Removed redundant files and updated mock configuration data for testing purposes.
    • Added mock configuration data to support testing and development.

@hardingjam hardingjam added the enhancement New feature or request label Apr 25, 2025
@hardingjam hardingjam added this to the raindex.finance v2 milestone Apr 25, 2025
@hardingjam hardingjam requested a review from hardyjosh April 25, 2025 12:27
@hardingjam hardingjam self-assigned this Apr 25, 2025
@hardingjam hardingjam marked this pull request as ready for review April 25, 2025 12:27
Copy link
Contributor

coderabbitai bot commented Apr 25, 2025

Walkthrough

A new utility function, cachedWritableStore, was introduced to provide a generic, persistent Svelte writable store synchronized with localStorage. This function is now exported from the UI components package for use across the codebase. The web application leverages this utility to create a new persistent setting, hideZeroBalanceVaults, managing a boolean flag with robust error handling. Some import paths were updated to use the new shared utility, and minor refactoring was performed in the vaults page to adjust how the setting is accessed. An obsolete index file was also removed. Additionally, a mock configuration source was added for testing, and a test suite for settings stores was moved into the module guarded by a runtime check. Several local cachedWritableStore implementations were removed in favor of the shared package export.

Changes

File(s) Change Summary
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts Added new utility: cachedWritableStore, a generic Svelte writable store synced with localStorage and error handling.
packages/ui-components/src/lib/index.ts Exported cachedWritableStore and related typed cached writable stores from the new module.
packages/webapp/src/lib/stores/settings.ts Added hideZeroBalanceVaults persistent boolean store using cachedWritableStore.
packages/webapp/src/routes/vaults/+page.svelte Changed import of hideZeroBalanceVaults to direct import; reordered destructuring of page data stores.
packages/webapp/src/lib/index.ts Deleted obsolete file containing only a comment.
tauri-app/src/lib/storesGeneric/cachedWritableStore.ts Deleted local cachedWritableStore implementation (moved to shared package).
tauri-app/src/lib/storesGeneric/fetchableStore.ts Updated import path for cachedWritableStore to use external package.
tauri-app/src/lib/storesGeneric/detailStore.ts Updated import path for cachedWritableStore to use external package.
tauri-app/src/lib/storesGeneric/listStore.ts Updated import path for cachedWritableStore to use external package.
tauri-app/src/lib/storesGeneric/settingStore.ts Updated import path for cachedWritableString to use external package.
tauri-app/src/lib/stores/settings.ts Updated imports to use external package; added inline test suite for reactive store behavior using mock config.
tauri-app/src/lib/mocks/mockConfigSource.ts Added mock configuration source object for testing purposes.
tauri-app/src/lib/stores/settings.test.ts Deleted standalone test file for settings stores (tests moved inline in settings.ts).

Sequence Diagram(s)

sequenceDiagram
    participant App as Svelte App
    participant Store as cachedWritableStore
    participant localStorage

    App->>Store: Initialize cachedWritableStore(key, default, serialize, deserialize)
    Store->>localStorage: Get item by key
    alt Item exists and parse succeeds
        Store->>App: Provide parsed value
    else Item missing or parse fails
        Store->>App: Provide default value
    end
    App->>Store: Subscribe and update value
    Store->>localStorage: Set or remove item on change
Loading
sequenceDiagram
    participant Settings as settings.ts
    participant Store as cachedWritableStore
    participant localStorage

    Settings->>Store: Create hideZeroBalanceVaults store (key, true, serialize, deserialize)
    Store->>localStorage: Get 'settings.hideZeroBalanceVaults'
    alt Valid JSON boolean
        Store->>Settings: Provide boolean value
    else Invalid/corrupted data
        Store->>Settings: Provide default (true)
    end
    Settings->>Store: Update value
    Store->>localStorage: Update or remove key
Loading

Suggested labels

test

Suggested reviewers

  • hardyjosh
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

@hardingjam hardingjam linked an issue Apr 25, 2025 that may be closed by this pull request
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: 5

🔭 Outside diff range comments (2)
tauri-app/src/lib/storesGeneric/fetchableStore.ts (1)

50-52: 🧹 Nitpick (assertive)

Consider updating to Number.parseInt for consistency

For consistency with modern JavaScript standards, consider updating this instance of parseInt to Number.parseInt as well, though it's not part of the current changes.

-    (s) => parseInt(s),
+    (s) => Number.parseInt(s),
🧰 Tools
🪛 Biome (1.9.4)

[error] 51-51: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

tauri-app/src/lib/storesGeneric/listStore.ts (1)

37-43: 🧹 Nitpick (assertive)

Verify JSON serialization error handling

The current implementation of cachedWritablePages doesn't handle JSON parsing errors which could occur if the stored data becomes corrupted. Consider adding error handling.

const cachedWritablePages = <T>(key: string) =>
  cachedWritableStore<AllPages<T>>(
    key,
    [],
    (value) => JSON.stringify(value),
-    (value) => JSON.parse(value),
+    (value) => {
+      try {
+        return JSON.parse(value);
+      } catch (e) {
+        console.warn(`Failed to parse JSON for ${key}:`, e);
+        return [];
+      }
+    },
  );
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between eade778 and 157a963.

📒 Files selected for processing (12)
  • packages/ui-components/src/lib/components/ListViewOrderbookFilters.svelte (1 hunks)
  • packages/ui-components/src/lib/index.ts (1 hunks)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
  • packages/webapp/src/lib/index.ts (0 hunks)
  • packages/webapp/src/lib/stores/settings.ts (1 hunks)
  • packages/webapp/src/routes/vaults/+page.svelte (1 hunks)
  • tauri-app/src/lib/stores/settings.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (0 hunks)
  • tauri-app/src/lib/storesGeneric/detailStore.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/fetchableStore.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/listStore.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/settingStore.ts (1 hunks)
💤 Files with no reviewable changes (2)
  • packages/webapp/src/lib/index.ts
  • tauri-app/src/lib/storesGeneric/cachedWritableStore.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (2)
packages/ui-components/src/lib/index.ts (6)
  • cachedWritableStore (121-121)
  • cachedWritableString (122-122)
  • cachedWritableInt (123-123)
  • cachedWritableOptionalStore (124-124)
  • cachedWritableIntOptional (125-125)
  • cachedWritableStringOptional (126-126)
crates/settings/src/scenario.rs (1)
  • key (221-221)
🪛 Biome (1.9.4)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts

[error] 42-42: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 63-63: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: Deploy-Preview
  • GitHub Check: git-clean
  • GitHub Check: test
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (8)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

3-28: Well-implemented localStorage-backed Svelte store!

This generic cached writable store implementation provides a solid foundation for persistent state management in Svelte applications. The function correctly handles initialization from localStorage, synchronizes updates, and manages key removal when values become undefined.

tauri-app/src/lib/storesGeneric/fetchableStore.ts (1)

2-2: LGTM: Import path updated correctly

The import source has been successfully updated to use the shared component library.

tauri-app/src/lib/storesGeneric/listStore.ts (1)

6-6: LGTM: Import path updated correctly

The import source has been successfully updated to use the shared component library.

tauri-app/src/lib/storesGeneric/settingStore.ts (1)

2-2: LGTM: Import path updated correctly

The import source has been successfully updated to use the shared component library.

tauri-app/src/lib/storesGeneric/detailStore.ts (1)

2-2: Import updated to use shared library implementation.

The import source for cachedWritableStore has been updated to use the centralized implementation from @rainlanguage/ui-components instead of a local module path. This change aligns with the PR objective of moving cached stores to a shared library.

tauri-app/src/lib/stores/settings.ts (1)

2-2: Import path updated for shared cached store utilities.

The import source for cachedWritableStore and cachedWritableStringOptional has been updated to use the centralized implementation from @rainlanguage/ui-components. This aligns with the PR objective of centralizing cached store utilities in a shared library.

packages/webapp/src/lib/stores/settings.ts (1)

1-14: Added new cached settings implementation for webapp.

This new file adds a cached writable store for hideZeroBalanceVaults that uses the shared implementation from @rainlanguage/ui-components. The implementation includes:

  • Proper key naming convention ('settings.hideZeroBalanceVaults')
  • Default value of true
  • JSON serialization/deserialization with error handling

This implementation is consistent with how the same setting is implemented in the Tauri app.

packages/ui-components/src/lib/components/ListViewOrderbookFilters.svelte (1)

38-38: Improved UI logic for "My Items Only" checkbox display

This change enhances the UI behavior by showing the "My Items Only" checkbox when either the accounts store exists with no entries OR when a wallet is connected. This makes the filter more accessible to users with connected wallets, improving usability while maintaining the contextual tooltip only when needed.

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: 1

♻️ Duplicate comments (4)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

3-28: 🛠️ Refactor suggestion

Well-structured implementation of localStorage-backed Svelte store.

This is a solid implementation of a writable store synchronized with localStorage. The function handles initialization from cache and updates localStorage when the store changes.

However, consider adding error handling for the deserialization process to handle potentially corrupted localStorage values:

export function cachedWritableStore<T>(
	key: string,
	defaultValue: T,
	serialize: (value: T) => string,
	deserialize: (serialized: string) => T
) {
	const getCache = () => {
		const cached = localStorage.getItem(key);
-		return cached !== null ? deserialize(cached) : defaultValue;
+		if (cached === null) return defaultValue;
+		try {
+			return deserialize(cached);
+		} catch (e) {
+			console.warn(`Failed to deserialize ${key} from localStorage:`, e);
+			return defaultValue;
+		}
	};
	// rest of the function remains the same
tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (3)

42-42: Use Number.parseInt instead of global parseInt.

For better consistency with modern JavaScript standards, it's recommended to use the Number namespace version.

-		(v) => parseInt(v)
+		(v) => Number.parseInt(v)
🧰 Tools
🪛 Biome (1.9.4)

[error] 42-42: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


63-63: Use Number.parseInt instead of global parseInt.

For better consistency with modern JavaScript standards, it's recommended to use the Number namespace version.

-		(v) => parseInt(v)
+		(v) => Number.parseInt(v)
🧰 Tools
🪛 Biome (1.9.4)

[error] 63-63: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


51-56: Consider adding error handling for deserialization.

While the optional store implementation looks good, consider adding try/catch blocks for deserialization to handle potentially corrupted localStorage values gracefully.

export const cachedWritableOptionalStore = <T>(
	key: string,
	defaultValue: T | undefined = undefined,
	serialize: (value: T) => string,
	deserialize: (serialized: string) => T
) =>
	cachedWritableStore<T | undefined>(
		key,
		defaultValue,
-		(v) => (v ? serialize(v) : ''),
-		(v) => (v ? deserialize(v) : undefined)
+		(v) => (v ? serialize(v) : ''),
+		(v) => {
+			if (!v) return undefined;
+			try {
+				return deserialize(v);
+			} catch (e) {
+				console.warn(`Failed to deserialize ${key} from localStorage:`, e);
+				return defaultValue;
+			}
+		}
	);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 887a313 and f598c2a.

📒 Files selected for processing (3)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
  • tauri-app/src/lib/stores/settings.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
tauri-app/src/lib/storesGeneric/cachedWritableStore.ts

[error] 42-42: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 63-63: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: Deploy-Preview
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: test
  • GitHub Check: git-clean
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (3)
tauri-app/src/lib/stores/settings.ts (2)

2-2: Import consolidation looks good.

The consolidation of imports for cachedWritableStore and cachedWritableStringOptional is a clean approach.


255-266: Well-implemented boolean store with proper error handling.

The implementation of hideZeroBalanceVaults includes proper error handling for JSON deserialization, defaulting to true if parsing fails. The default value and fallback behavior are consistent.

tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (1)

4-28: Code formatting looks good.

The reformatting and indentation adjustments improve readability while maintaining functionality.

@hardingjam hardingjam changed the title Move cached stores to shared lib, and update hideZeroBalanceVaults to use the cached version Add cachedWritableStores to shared lib, and update hideZeroBalanceVaults to use the cached version Apr 25, 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

♻️ Duplicate comments (2)
tauri-app/src/lib/storesGeneric/fetchableStore.ts (1)

51-51: 🧹 Nitpick (assertive)

Use Number.parseInt instead of global parseInt

For better consistency with modern JavaScript standards, it's recommended to use the Number namespace version.

-    (s) => parseInt(s),
+    (s) => Number.parseInt(s),
🧰 Tools
🪛 Biome (1.9.4)

[error] 51-51: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

packages/ui-components/src/lib/index.ts (1)

120-120: Good addition of the store utility to shared exports

Exporting the cached store utility from the shared UI components package is a positive architectural change that follows the DRY principle. This makes it available for reuse across different parts of the application.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f598c2a and b1fca38.

📒 Files selected for processing (4)
  • packages/ui-components/src/lib/index.ts (1 hunks)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
  • packages/webapp/src/lib/stores/settings.ts (1 hunks)
  • tauri-app/src/lib/storesGeneric/fetchableStore.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/webapp/src/lib/stores/settings.ts (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)
  • cachedWritableStore (3-36)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)
tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (1)
  • cachedWritableStore (3-28)
⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: test
  • GitHub Check: git-clean
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: test
  • GitHub Check: Deploy-Preview
🔇 Additional comments (3)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

3-36: Well-implemented localStorage-backed Svelte store with proper error handling

This implementation of a cached writable store is robust and well-designed. You've properly handled potential localStorage failures with try-catch blocks and provided appropriate fallbacks to default values.

The generic typing makes this utility highly reusable across different data types while the clear separation of serialization/deserialization logic allows for flexible data storage strategies.

tauri-app/src/lib/storesGeneric/fetchableStore.ts (1)

2-2: Import path successfully updated to use the shared implementation

Good job updating the import path to use the centralized implementation of cachedWritableStore from the shared library.

packages/webapp/src/lib/stores/settings.ts (1)

1-14: Well-implemented persistent user setting with proper error handling

This store implementation correctly uses the shared cachedWritableStore utility to create a persistent boolean setting for hiding zero balance vaults. The JSON serialization/deserialization with error handling is appropriate for this use case, and the fallback to the default value ensures a consistent user experience even if localStorage data becomes corrupted.

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b1fca38 and 5101234.

📒 Files selected for processing (2)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
  • packages/webapp/src/lib/stores/settings.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)
tauri-app/src/lib/storesGeneric/cachedWritableStore.ts (1)
  • cachedWritableStore (3-28)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: test
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: git-clean
  • GitHub Check: test
🔇 Additional comments (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

3-37: Well-implemented utility for persistent Svelte stores.

This implementation of cachedWritableStore provides a robust way to create writable stores that persist their values in localStorage with proper error handling. The try-catch blocks around localStorage operations ensure the application will function even when localStorage is unavailable.

@hardyjosh
Copy link
Contributor

@hardingjam should we not be deleting cachedWritableStore from the tauri app now?

Copy link
Contributor

@hardyjosh hardyjosh left a comment

Choose a reason for hiding this comment

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

see comment

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: 1

♻️ Duplicate comments (1)
packages/ui-components/src/lib/index.ts (1)

120-120: 🧹 Nitpick (assertive)

Add JSDoc for cachedWritableStore.
Please include JSDoc comments on the implementation to describe its parameters, serialization/deserialization behavior, and error handling. This will improve discoverability and help developers understand usage patterns.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bb464c7 and 587eb54.

📒 Files selected for processing (2)
  • packages/ui-components/src/lib/index.ts (1 hunks)
  • packages/webapp/src/routes/vaults/+page.svelte (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
🔇 Additional comments (3)
packages/ui-components/src/lib/index.ts (1)

120-120: Approve addition of cachedWritableStore export.
Centralizing this utility in the UI components package promotes DRY, ensures consistent behavior across the webapp and Tauri apps, and enables shared persistence logic.

packages/webapp/src/routes/vaults/+page.svelte (2)

7-7: Confirm hideZeroBalanceVaults import path.
Importing the new persistent store from $lib/stores/settings is correct, but please verify that the settings.ts file exports the updated hideZeroBalanceVaults (backed by cachedWritableStore) and that there are no remaining stale imports of this store elsewhere.


22-22: Verify activeSubgraphs remains valid.
Since you moved hideZeroBalanceVaults out of $page.data.stores, ensure that activeSubgraphs is still populated by the page loader and doesn't need to be migrated to a cached store.

@hardingjam
Copy link
Contributor Author

@hardingjam should we not be deleting cachedWritableStore from the tauri app now?

True, i've removed it

@hardingjam hardingjam closed this Apr 28, 2025
@hardingjam hardingjam reopened this Apr 28, 2025
@hardingjam hardingjam requested a review from hardyjosh April 28, 2025 10:49
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

♻️ Duplicate comments (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

90-101: 🛠️ Refactor suggestion

Consider adding error handling for deserialization

The optional store implementation should handle potential deserialization errors to prevent runtime exceptions from corrupted localStorage values.

export const cachedWritableOptionalStore = <T>(
	key: string,
	defaultValue: T | undefined = undefined,
	serialize: (value: T) => string,
	deserialize: (serialized: string) => T
) =>
	cachedWritableStore<T | undefined>(
		key,
		defaultValue,
		(v) => (v !== undefined ? serialize(v) : ''),
-		(v) => (v !== '' ? deserialize(v) : undefined)
+		(v) => {
+			if (v === '') return undefined;
+			try {
+				return deserialize(v);
+			} catch (e) {
+				console.warn(`Failed to deserialize ${key} from localStorage:`, e);
+				return defaultValue;
+			}
+		}
	);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between aa225b2 and 6b44948.

📒 Files selected for processing (1)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (2)
packages/ui-components/src/lib/index.ts (4)
  • cachedWritableStore (121-121)
  • cachedWritableString (124-124)
  • cachedWritableIntOptional (122-122)
  • cachedWritableStringOptional (123-123)
crates/settings/src/scenario.rs (1)
  • key (221-221)
⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-legal)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-test)
  • GitHub Check: standard-tests (ubuntu-latest, ob-rs-test, true)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-test)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-wasm-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-static)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: standard-tests (ubuntu-latest, rainix-rs-artifacts, true)
  • GitHub Check: standard-tests (ubuntu-latest, test-js-bindings)
  • GitHub Check: build-tauri (ubuntu-22.04, true)
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: Deploy-Preview
  • GitHub Check: test
  • GitHub Check: git-clean
🔇 Additional comments (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

30-64: Well-implemented localStorage persistence with proper error handling!

The implementation properly handles localStorage unavailability with try/catch blocks and includes helpful comments explaining the design decision to silently ignore errors. This is a solid, reusable utility for creating persistent Svelte stores.

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: 1

♻️ Duplicate comments (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)

93-104: 🧹 Nitpick (assertive)

Consider improving error handling in the optional store implementation.

While the implementation works for most cases, it could be more robust with additional error handling during deserialization.

export const cachedWritableOptionalStore = <T>(
	key: string,
	defaultValue: T | undefined = undefined,
	serialize: (value: T) => string,
	deserialize: (serialized: string) => T
) =>
	cachedWritableStore<T | undefined>(
		key,
		defaultValue,
-		(v) => (v !== undefined ? serialize(v) : ''),
-		(v) => (v !== '' ? deserialize(v) : undefined)
+		(v) => (v !== undefined ? serialize(v) : ''),
+		(v) => {
+			if (v === '') return undefined;
+			try {
+				return deserialize(v);
+			} catch (error) {
+				console.warn(`Failed to deserialize value for key "${key}":`, error);
+				return defaultValue;
+			}
+		}
	);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6b44948 and 0926719.

📒 Files selected for processing (1)
  • packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (1)
packages/ui-components/src/lib/index.ts (4)
  • cachedWritableStore (121-121)
  • cachedWritableString (124-124)
  • cachedWritableIntOptional (122-122)
  • cachedWritableStringOptional (123-123)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (3)
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts (3)

1-64: Well-implemented localStorage-backed Svelte store with proper error handling.

The implementation of the generic cachedWritableStore is robust and follows best practices:

  • Comprehensive JSDoc documentation with clear examples
  • Proper error handling for localStorage operations
  • Graceful fallbacks when localStorage is unavailable
  • Clean integration with Svelte's reactive system

66-82: Good implementation of specialized helpers for common types.

The string and integer store implementations are correctly implemented with appropriate serialization/deserialization strategies. The integer store properly handles potential NaN values, which is an excellent defensive programming practice.


113-122: Good NaN handling in the optional integer store.

The implementation correctly handles the edge case of parsing invalid numeric strings, using the nullish coalescing operator to provide an appropriate fallback value.

@hardyjosh
Copy link
Contributor

@hardingjam this doesn't work for me on the preview url - when i refresh i lose the selection

Screen.Recording.2025-05-01.at.07.20.00.mov

@hardingjam
Copy link
Contributor Author

@hardingjam this doesn't work for me on the preview url - when i refresh i lose the selection

Screen.Recording.2025-05-01.at.07.20.00.mov

this PR only introduces the store and adds the "hide empty vaults" setting. The other settings stores are added in #1700

@hardyjosh hardyjosh enabled auto-merge May 4, 2025 18:09
@hardyjosh hardyjosh merged commit d008123 into main May 4, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants