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

⚡️ Use infinite query on polls page #1200

Merged
merged 4 commits into from
Jul 10, 2024
Merged

⚡️ Use infinite query on polls page #1200

merged 4 commits into from
Jul 10, 2024

Conversation

lukevella
Copy link
Owner

@lukevella lukevella commented Jul 9, 2024

Should help performance for users with many open polls

Summary by CodeRabbit

  • New Features

    • Implemented paginated fetching for user polls to enhance loading performance and user experience.
    • Introduced a VisibilityTrigger component to trigger actions when elements become visible in the viewport.
  • Improvements

    • Updated user polls to use infinite scrolling, enabling seamless data fetching and display.

Copy link

vercel bot commented Jul 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 9, 2024 9:27pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
landing ⬜️ Ignored (Inspect) Visit Preview Jul 9, 2024 9:27pm

Copy link
Contributor

coderabbitai bot commented Jul 9, 2024

Walkthrough

The updates optimize the user polling functionality by switching from trpc.polls.list.useQuery to trpc.polls.infiniteList.useInfiniteQuery for efficient data fetching with pagination. A new VisibilityTrigger component has been introduced to trigger actions when an element comes into view. Additionally, polls.ts on the backend has been extended to support an infiniteList procedure for paginated poll fetching.

Changes

Files Change Summaries
apps/web/src/app/[locale]/...(admin)/polls/user-polls.tsx Refactored polling component to use trpc.polls.infiniteList.useInfiniteQuery for paginated data fetching, introducing pagination handling variables.
apps/web/src/components/visibility-trigger.tsx Introduced new VisibilityTrigger component using the Intersection Observer API to trigger callbacks when visible in the viewport.
packages/backend/trpc/routers/polls.ts Added new infiniteList procedure to the polls router for fetching paginated poll data based on user ID and status criteria.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI as User-Polls Component
    participant Backend as Polls Router
    participant DB as Database

    User->>UI: Open Polls Page
    UI->>Backend: Use `trpc.polls.infiniteList.useInfiniteQuery`
    Backend->>DB: Fetch polls with limit and cursor
    DB-->>Backend: Return polls data and next cursor
    Backend-->>UI: Provide polls and next cursor for pagination
    Note over UI: Render initial poll data

    UI->>UI: VisibilityTrigger becomes visible
    UI->>Backend: Fetch next page using next cursor
    Backend->>DB: Fetch polls with new cursor
    DB-->>Backend: Return next batch of polls and cursor
    Backend-->>UI: Provide additional polls and new cursor
    Note over UI: Append new poll data to the existing list
Loading

Poem

In the realm of code so bright,
Polls now load with infinite might,
As you scroll, new data arrives,
With seamless flow, your app thrives.
Visibility triggers in the view,
Making paginated polls anew!


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>.
    • 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 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 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 as 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.

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

Outside diff range and nitpick comments (1)
packages/database/prisma/seed.ts (1)

Line range hint 63-109: Optimize the creation of votes and comments.

Creating votes and comments in a loop might introduce performance issues. Consider using bulk operations or transactions to optimize this process.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e985a5d and 07fcd99.

Files selected for processing (4)
  • apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx (2 hunks)
  • apps/web/src/components/visibility-trigger.tsx (1 hunks)
  • packages/backend/trpc/routers/polls.ts (1 hunks)
  • packages/database/prisma/seed.ts (1 hunks)
Additional context used
GitHub Check: Linting
apps/web/src/components/visibility-trigger.tsx

[failure] 1-1:
Run autofix to sort these imports!


[warning] 30-30:
The ref value 'triggerRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'triggerRef.current' to a variable inside the effect, and use that variable in the cleanup function

Additional comments not posted (10)
apps/web/src/components/visibility-trigger.tsx (3)

1-1: Imports look good.

The import statements are correct and necessary for the component.

Tools
GitHub Check: Linting

[failure] 1-1:
Run autofix to sort these imports!


3-5: Interface definition is correct.

The interface VisibilityTriggerProps is simple and correctly typed.


38-38: Export statement is correct.

The VisibilityTrigger component is correctly exported as the default export.

packages/database/prisma/seed.ts (1)

10-61: Ensure uniqueness of generated ids.

The use of faker for generating ids and other attributes might introduce non-unique values. Consider using a more robust method for generating unique ids.

apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx (4)

26-26: Imports look good.

The import statement for VisibilityTrigger is correct and necessary for the component.


Line range hint 33-62: Ensure proper handling of fetchNextPage and hasNextPage.

The component logic is correct, but ensure that the fetchNextPage and hasNextPage are handled correctly to avoid potential issues with infinite scrolling.


61-61: Usage of VisibilityTrigger component is correct.

The VisibilityTrigger component is correctly used to trigger fetching the next page of data.


55-62: Rendering logic is correct.

The rendering logic appropriately handles various states, including displaying a spinner when data is being fetched.

packages/backend/trpc/routers/polls.ts (2)

97-103: Input validation logic is correct.

The input validation logic ensures that the required parameters for the infiniteList procedure are provided.


104-146: Procedure logic is correct.

The infiniteList procedure correctly fetches polls based on the provided parameters and handles pagination appropriately.

apps/web/src/components/visibility-trigger.tsx Outdated Show resolved Hide resolved
lukevella and others added 2 commits July 9, 2024 21:51
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: CHILL

Commits

Files that changed from the base of the PR and between 07fcd99 and a39ba2e.

Files selected for processing (2)
  • apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx (1 hunks)
  • packages/backend/trpc/routers/polls.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/backend/trpc/routers/polls.ts
Additional context used
GitHub Check: Linting
apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx

[failure] 33-33:
'isFetching' is assigned a value but never used

Additional comments not posted (2)
apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx (2)

33-43: LGTM! Ensure proper handling of paginated data.

The changes to support infinite queries for pagination look good. Ensure that the paginated data is handled correctly and that the getNextPageParam function correctly identifies the next cursor.

Tools
GitHub Check: Linting

[failure] 33-33:
'isFetching' is assigned a value but never used


50-60: Good use of VisibilityTrigger for infinite scrolling.

The implementation correctly uses the VisibilityTrigger component to fetch the next page when the trigger becomes visible. This enhances the user experience by enabling lazy loading of poll data.

apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a39ba2e and c6b2eec.

Files selected for processing (3)
  • apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx (1 hunks)
  • apps/web/src/components/visibility-trigger.tsx (1 hunks)
  • packages/backend/trpc/routers/polls.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • apps/web/src/app/[locale]/(admin)/polls/user-polls.tsx
  • apps/web/src/components/visibility-trigger.tsx
  • packages/backend/trpc/routers/polls.ts

@lukevella lukevella merged commit 43c450a into main Jul 10, 2024
7 checks passed
@lukevella lukevella deleted the infinite-scroll branch July 10, 2024 07:34
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