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

[TypeScript] Add types to SpreadsheetProvider #4409

Merged
merged 4 commits into from
Feb 20, 2025

Conversation

joel-jeremy
Copy link
Contributor

No description provided.

@actual-github-bot actual-github-bot bot changed the title [TypeScript] SpreadsheetProvider [WIP] [TypeScript] SpreadsheetProvider Feb 19, 2025
Copy link

netlify bot commented Feb 19, 2025

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit 5dd6e19
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/67b6676ece79410008f5c2bc
😎 Deploy Preview https://deploy-preview-4409.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Feb 19, 2025

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
17 6.94 MB → 6.97 MB (+36.87 kB) +0.52%
Changeset
File Δ Size
node_modules/lru-cache/dist/esm/index.js 🆕 +53.55 kB 0 B → 53.55 kB
src/components/spreadsheet/useSheetValue.ts 📈 +184 B (+14.78%) 1.22 kB → 1.4 kB
home/runner/work/actual/actual/packages/loot-core/src/client/SpreadsheetProvider.tsx 📈 +254 B (+8.46%) 2.93 kB → 3.18 kB
node_modules/dnd-core/dist/reducers/index.js 📉 -2 B (-0.14%) 1.38 kB → 1.38 kB
node_modules/dnd-core/dist/utils/js_utils.js 📉 -2 B (-0.18%) 1.11 kB → 1.11 kB
src/components/budget/envelope/HoldMenu.tsx 📉 -191 B (-10.30%) 1.81 kB → 1.62 kB
node_modules/yallist/yallist.js 🔥 -8.44 kB (-100%) 8.44 kB → 0 B
node_modules/lru-cache/index.js 🔥 -8.13 kB (-100%) 8.13 kB → 0 B
node_modules/yallist/iterator.js 🔥 -356 B (-100%) 356 B → 0 B
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
static/js/index.js 4.31 MB → 4.35 MB (+36.87 kB) +0.84%

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/en-GB.js 99.42 kB 0%
static/js/fr.js 72.19 kB 0%
static/js/nl.js 98.54 kB 0%
static/js/de.js 111.72 kB 0%
static/js/en.js 100.65 kB 0%
static/js/workbox-window.prod.es5.js 5.69 kB 0%
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/resize-observer.js 18.37 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/pt-BR.js 106.43 kB 0%
static/js/uk.js 111.11 kB 0%
static/js/useAccountPreviewTransactions.js 1.69 kB 0%
static/js/AppliedFilters.js 10.52 kB 0%
static/js/narrow.js 85.57 kB 0%
static/js/wide.js 102.8 kB 0%
static/js/ReportRouter.js 1.59 MB 0%

Copy link
Contributor

github-actions bot commented Feb 19, 2025

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.33 MB → 1.34 MB (+8.05 kB) +0.59%
Changeset
File Δ Size
node_modules/lru-cache/dist/esm/index.js 🆕 +53.61 kB 0 B → 53.61 kB
packages/loot-core/src/server/main.ts 📈 +78 B (+0.11%) 69.76 kB → 69.84 kB
packages/loot-core/src/server/db/index.ts 📈 +19 B (+0.09%) 20.52 kB → 20.54 kB
packages/loot-core/src/platform/server/sqlite/unicodeLike.ts 📉 -7 B (-0.85%) 824 B → 817 B
node_modules/lru-cache/node_modules/yallist/yallist.js 🔥 -8.23 kB (-100%) 8.23 kB → 0 B
node_modules/lru-cache/index.js 🔥 -7.99 kB (-100%) 7.99 kB → 0 B
node_modules/lru-cache/node_modules/yallist/iterator.js 🔥 -207 B (-100%) 207 B → 0 B
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
kcab.worker.js 1.33 MB → 1.34 MB (+8.05 kB) +0.59%

Smaller

No assets were smaller

Unchanged

No assets were unchanged

@joel-jeremy joel-jeremy changed the title [WIP] [TypeScript] SpreadsheetProvider [WIP] [TypeScript] Add types to SpreadsheetProvider Feb 19, 2025
@joel-jeremy joel-jeremy changed the title [WIP] [TypeScript] Add types to SpreadsheetProvider [TypeScript] Add types to SpreadsheetProvider Feb 19, 2025
setAmount(integerToCurrency(Math.max(node.value as number, 0)));
})();
}, []);
useSheetValue<'envelope-budget', 'to-budget'>('to-budget', ({ value }) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced with useSheetValue which does the same thing

Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Walkthrough

This pull request implements several updates across multiple modules. In the desktop client, the HoldMenu.tsx component now uses useSheetValue instead of useSpreadsheet, streamlining the sheet value retrieval and removing the asynchronous effect. The useSheetValue hook is simplified by removing the query property from its result type and its associated logic. In the loot-core package, the lru-cache dependency is upgraded and its imports are modified to use named imports with explicit type parameters. The SpreadsheetProvider.tsx and related server-side files are updated with enhanced type annotations, improved error handling, and method signature adjustments. Additionally, the getCells handler returns more descriptive data, and a new Webpack rule is added to handle JavaScript files without fully specified imports.

Possibly related PRs

Suggested labels

:sparkles: Merged

Suggested reviewers

  • joel-jeremy
  • matt-fidd
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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.

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/loot-core/src/client/SpreadsheetProvider.tsx (1)

28-38: Consider improving type organization and documentation.

The TODO comment suggests making the Binding type generic. This would improve type flexibility and reusability.

Consider updating the type definition to be generic:

-type Binding =
-  | string
-  | { name: string; value?: unknown | null; query?: Query | undefined };
+type Binding<T = unknown> =
+  | string
+  | { name: string; value?: T | null; query?: Query | undefined };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 04b5bdc and 4f960d3.

⛔ Files ignored due to path filters (2)
  • upcoming-release-notes/4409.md is excluded by !**/*.md
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (9)
  • packages/desktop-client/src/components/budget/envelope/HoldMenu.tsx (1 hunks)
  • packages/desktop-client/src/components/spreadsheet/useSheetValue.ts (1 hunks)
  • packages/loot-core/package.json (1 hunks)
  • packages/loot-core/src/client/SpreadsheetProvider.tsx (5 hunks)
  • packages/loot-core/src/platform/server/sqlite/unicodeLike.ts (1 hunks)
  • packages/loot-core/src/server/db/index.ts (3 hunks)
  • packages/loot-core/src/server/main.ts (1 hunks)
  • packages/loot-core/src/types/server-handlers.d.ts (1 hunks)
  • packages/loot-core/webpack/webpack.browser.config.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Visual regression
  • GitHub Check: Functional
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-latest)
🔇 Additional comments (14)
packages/loot-core/src/platform/server/sqlite/unicodeLike.ts (1)

1-3: LGTM! Type-safe LRU cache implementation.

The changes properly implement type safety for the LRU cache by:

  1. Using named import LRUCache from the upgraded package
  2. Adding appropriate type parameters <string, RegExp> to specify key-value types
packages/loot-core/src/server/db/index.ts (2)

11-11: LGTM! Type-safe LRU cache import.

The import statement correctly uses the named import LRUCache from the upgraded package.


135-135: LGTM! Type-safe query cache implementation.

The changes properly implement type safety for the query cache by:

  1. Adding appropriate type parameters <string, string> to specify key-value types
  2. Maintaining consistency in cache initialization across the codebase

Also applies to: 148-148

packages/loot-core/package.json (1)

33-33:

❓ Verification inconclusive

Verify compatibility with lru-cache v11.0.2.

The upgrade from v5.1.1 to v11.0.2 is a major version bump that might introduce breaking changes.


🌐 Web query:

What are the breaking changes between lru-cache v5 and v11?

💡 Result:

The lru-cache library has undergone several significant breaking changes between versions 5 and 11:


Version 7

  • New algorithm/data structure for better performance ([1][7])
  • API renames:
    • staleallowStale
    • maxAgettl
    • lengthsizeCalculation
    • del()delete()
    • reset()clear()
  • Deprecated positional parameters in set(), get(), and has() in favor of options objects
  • Changed serialization format for dump()/load() ([1])

Version 8

  • TypeScript rewrite with major type definition changes ([1][7])
  • Node.js requirement bumped to ≥16.14 (removed AbortController polyfill) ([1][7])
  • Keys/values can no longer be null or undefined ([1][7])
  • Renamed fetchContext option to context ([1][7])
  • Made internal properties truly private ([1])

Version 9

  • Removed default export (only named exports available) ([1][7])
  • Reintroduced minimal polyfill with usage warnings ([1])

Version 10

  • Changed cache.fetch() return type from Promise<V | void> to Promise<V | undefined> (TypeScript impact) ([1])
  • Made cache.set(key, undefined) equivalent to deletion ([1])

Version 11

  • Dropped Node.js <20 support ([1][3])
  • Removed deprecated APIs and legacy polyfills ([1])

Key architectural impacts:

  1. Migration from JavaScript to TypeScript
  2. Multiple option/method renames
  3. Progressive tightening of Node.js version requirements
  4. Serialization format incompatibility between versions
  5. Changed type definitions affecting TypeScript users

Developers upgrading should check both their usage of renamed APIs and their Node.js runtime compatibility. The library now requires modern JavaScript environments but offers better performance and type safety ([1][7][3]).

Citations:


Attention: Verify lru-cache v11 Compatibility

The upgrade from v5.1.1 to v11.0.2 is a major change that introduces several breaking modifications. Please confirm that the usage of lru-cache across the codebase is updated accordingly. In particular:

  • API Renames & Options Changes:
    • Update option names: maxAgettl, staleallowStale, and lengthsizeCalculation.
    • Adjust method calls: del() should now be delete(), and reset() should be replaced with clear().
  • TypeScript & Internal Changes:
    • Ensure TypeScript definitions are properly aligned given the complete rewrite in newer versions.
  • Node.js Runtime:
    • Verify that the deployment environment meets the new requirement (Node.js ≥20).

Please review all locations where lru-cache is used to confirm the new API is correctly integrated.

packages/desktop-client/src/components/budget/envelope/HoldMenu.tsx (2)

1-1: LGTM! Import statements are now more focused and type-safe.

The changes improve code organization by:

  • Explicitly importing types from React
  • Replacing useSpreadsheet with the more specific useSheetValue hook

Also applies to: 12-12


21-23: LGTM! Sheet value handling is now more direct and efficient.

The implementation:

  • Directly uses useSheetValue for accessing the budget amount
  • Properly handles type conversion with integerToCurrency
  • Ensures non-negative values with Math.max
packages/desktop-client/src/components/spreadsheet/useSheetValue.ts (1)

14-20: LGTM! Type definitions are now more focused and precise.

The SheetValueResult type is streamlined by removing the unnecessary query property, making the interface cleaner and more maintainable.

packages/loot-core/webpack/webpack.browser.config.js (1)

65-70:

❓ Verification inconclusive

Verify the impact of disabling fully specified imports.

The new rule allows for more flexible module resolution by setting fullySpecified: false. While this can improve developer experience, it's important to ensure it doesn't introduce any unexpected side effects.

Let's check for any potential issues with module resolution:


🏁 Script executed:

#!/bin/bash
# Search for .js imports that might be affected by this change
rg -t typescript -t javascript "from ['\"](\.{1,2}/[^'\"]+)(?<!\.js)['\"]" -g '!dist' -g '!node_modules'

Length of output: 380


Action: Verify module resolution behavior with updated regex tooling

The webpack rule update setting fullySpecified: false is intended to ease module imports, but our initial verification command couldn't run as expected due to regex engine limitations (unsupported look-around). To accurately check if any relative .js imports might be inadvertently affected, please re-run the search with PCRE2 enabled. For example:

#!/bin/bash
# Re-run the search using PCRE2 to support look-around assertions
rg --pcre2 -t typescript -t javascript "from ['\"](\.{1,2}/[^'\"]+)(?<!\.js)['\"]" -g '!dist' -g '!node_modules'

If this adjusted command produces no output, it would indicate that no problematic imports are present. However, manual verification is recommended to ensure no unexpected side effects occur.

packages/loot-core/src/client/SpreadsheetProvider.tsx (3)

15-18: LGTM! Context type safety is improved.

The SpreadsheetContext is now properly typed with undefined, making the type system more accurate and preventing potential runtime errors.


20-26: LGTM! Error handling for context usage is now more robust.

The useSpreadsheet hook now properly validates context availability with a clear error message.


41-43: LGTM! Cache implementation is now properly typed.

The LRU cache initialization is now type-safe with proper generic type parameters.

packages/loot-core/src/types/server-handlers.d.ts (3)

133-136: LGTM! Type definition enhances type safety.

The return type is well-defined and properly references the SpreadsheetNode type for both name and value properties.


138-142: LGTM! Type definition provides flexibility with optional value.

The return type is well-structured with:

  • Required name property for identification
  • Optional value property for flexibility

144-146: LGTM! Type definition is clear and focused.

The return type correctly specifies an array of cell names using the SpreadsheetNode['name'] type.

Comment on lines +58 to +62
const newCastedResult = {
name: newResult.name,
// TODO: Spreadsheets, SheetNames, SheetFields, etc must be moved to the loot-core package
value: newResult.value as Spreadsheets[SheetName][FieldName],
};
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Consider moving type definitions to improve code organization.

The TODO comment indicates that type definitions should be moved to the loot-core package. This would improve code organization and make types more accessible across the project.

Let's verify the current location of type definitions:


🏁 Script executed:

#!/bin/bash
# Search for type definitions across the codebase
rg -t typescript "type (Spreadsheets|SheetNames|SheetFields)" -g '!dist' -g '!node_modules'

Length of output: 128


I've noticed that the initial search command failed because of an unrecognized file type. To gather more reliable information we should rerun the search using a glob that targets TypeScript files. For example:

Once you verify where these type definitions are currently declared (or if they're even declared here), please confirm whether they might benefit from being centralized in the loot-core package as suggested by the TODO comment.


Action: Verify the location of the type definitions for Spreadsheets, SheetNames, and SheetFields, and consider migrating them into the loot-core package for improved organization and reusability.

  • File in question: packages/desktop-client/src/components/spreadsheet/useSheetValue.ts (Lines 58-62)
  • Suggestion: Centralize these type definitions (currently defined locally or imported inconsistently) in the loot-core package to avoid duplication and improve maintainability.

🏁 Script executed:

[run_scripts]
#!/bin/bash
# Corrected search for type definitions across TypeScript files in the codebase
rg "type\s+(Spreadsheets|SheetNames|SheetFields)" -g "*.{ts,tsx}" -g '!dist' -g '!node_modules'

Length of output: 2241


Consolidate Spreadsheet Type Definitions Across Packages

  • The TODO in packages/desktop-client/src/components/spreadsheet/useSheetValue.ts advises moving spreadsheet-related types to the loot-core package.
  • Verification shows that SheetNames and SheetFields are already defined in packages/loot-core/src/client/queries.ts, while Spreadsheets is still defined under the desktop-client (see packages/desktop-client/src/components/spreadsheet/index.ts).
  • To improve maintainability and ensure all related types are centralized, consider moving the Spreadsheets type—and possibly consolidating all spreadsheet types—into the loot-core package and updating the imports accordingly.

Co-authored-by: Michael Clark <5285928+MikesGlitch@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants