Skip to content

feat: add refresh button to ProblemSwitcher#934

Closed
boomzero wants to merge 5 commits intodevfrom
feat/problem-switcher-refresh-button
Closed

feat: add refresh button to ProblemSwitcher#934
boomzero wants to merge 5 commits intodevfrom
feat/problem-switcher-refresh-button

Conversation

@boomzero
Copy link
Member

@boomzero boomzero commented Mar 14, 2026

Summary

  • Adds a (refresh) button at the bottom of the ProblemSwitcher panel
  • Clicking it clears all localStorage cache entries for the current contest (UserScript-Contest-{cid}-*)
  • Triggers a page reload so contest problem data is re-fetched fresh

Fixes #927

Test plan

  • Open a contest problem page with ProblemSwitcher enabled
  • Verify the button appears at the bottom of the switcher panel
  • Click the button and confirm localStorage entries for the contest are cleared
  • Confirm the page reloads and problem data is re-fetched correctly
  • Verify the switcher renders normally after refresh

🤖 Generated with Claude Code

Summary by Sourcery

New Features:

  • Add a refresh button to the ProblemSwitcher panel that clears contest-specific localStorage cache and reloads the page.

Summary by cubic

Added a refresh (↻) button to the ProblemSwitcher that clears contest cache (UserScript-Contest-{cid}-*) and reloads the page to fetch a fresh problem list. Updated to 3.3.2 (prerelease) and refreshed release notes and timestamp in Update.json.

  • Refactors
    • Hide the button when cid is missing to avoid accidental cache deletion.
    • Capture cid once and use a local prefix variable to simplify the handler.

Written for commit 31a0319. Summary will update on new commits.

Adds a ↻ button at the bottom of the ProblemSwitcher panel that clears
all localStorage cache entries for the current contest and reloads the page,
allowing users to force-fetch updated contest problem data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 14, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a refresh button to the ProblemSwitcher panel that clears contest-specific cached data from localStorage and reloads the page so problem data is re-fetched.

Sequence diagram for ProblemSwitcher refresh button click handling

sequenceDiagram
  actor User
  participant Browser
  participant RefreshButton
  participant Script
  participant LocalStorage
  participant Server

  User->>Browser: Click_refresh_button
  Browser->>RefreshButton: click_event
  RefreshButton->>Script: Event_listener_callback
  Script->>Browser: Prevent_default_navigation
  Script->>Browser: Get_SearchParams_cid
  loop For_each_localStorage_key
    Script->>LocalStorage: key(index)
    LocalStorage-->>Script: key_value
    Script->>Script: Check_prefix_UserScript_Contest_cid
    alt Key_matches_contest_prefix
      Script->>LocalStorage: removeItem(key_value)
    end
  end
  Script->>Browser: location_reload
  Browser->>Server: Request_contest_problem_page
  Server-->>Browser: HTML_and_problem_data
  Browser-->>User: Render_updated_ProblemSwitcher_with_fresh_data
Loading

Flow diagram for ProblemSwitcher cache refresh logic

flowchart TD
  A[Click_refresh_button] --> B[Prevent_default_on_link]
  B --> C[Get_cid_from_SearchParams]
  C --> D[Initialize_keysToRemove_array]
  D --> E{Iterate_over_localStorage_keys}
  E -->|More_keys| F[Read_key_at_index]
  F --> G{Key_starts_with_UserScript_Contest_cid_prefix}
  G -->|Yes| H[Push_key_to_keysToRemove]
  G -->|No| E
  H --> E
  E -->|No_more_keys| I[Remove_each_key_in_keysToRemove_from_localStorage]
  I --> J[Reload_page_with_location_reload]
  J --> K[Browser_refetches_contest_problem_data]
  K --> L[Render_updated_ProblemSwitcher_panel]
Loading

File-Level Changes

Change Details Files
Add a refresh control to ProblemSwitcher that clears contest-specific cache and reloads the page.
  • Create a new anchor element styled like existing ProblemSwitcher buttons, labeled with a refresh icon and placed at the bottom of the panel
  • Attach a click handler that prevents default navigation, reads the current contest id from SearchParams, and scans localStorage for keys matching the contest-specific prefix
  • Collect matching cache keys into a list, remove them from localStorage, and trigger a full page reload after cache invalidation
  • Append the refresh button to the ProblemSwitcher container after the list of problem links
XMOJ.user.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@hendragon-bot hendragon-bot bot added the user-script This issue or pull request is related to the main user script label Mar 14, 2026
@pull-request-size pull-request-size bot added size/M and removed size/S labels Mar 14, 2026
@boomzero boomzero mentioned this pull request Mar 14, 2026
1 task
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider handling the case where SearchParams.get("cid") returns null or undefined so the refresh button is a no-op or hidden when no contest ID is present, rather than relying on the prefix filter to avoid accidental deletions.
  • The localStorage key prefix "UserScript-Contest-" is duplicated inline here; if there is already a central definition or helper for these cache keys, reusing it would help keep the prefix consistent across future changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider handling the case where `SearchParams.get("cid")` returns `null` or `undefined` so the refresh button is a no-op or hidden when no contest ID is present, rather than relying on the prefix filter to avoid accidental deletions.
- The localStorage key prefix `"UserScript-Contest-"` is duplicated inline here; if there is already a central definition or helper for these cache keys, reusing it would help keep the prefix consistent across future changes.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="package.json">

<violation number="1" location="package.json:3">
P2: Do not bump the package version manually here; the release workflow is responsible for updating and synchronizing version numbers.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

{
"name": "xmoj-script",
"version": "3.3.1",
"version": "3.3.2",
Copy link

@cubic-dev-ai cubic-dev-ai bot Mar 14, 2026

Choose a reason for hiding this comment

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

P2: Do not bump the package version manually here; the release workflow is responsible for updating and synchronizing version numbers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At package.json, line 3:

<comment>Do not bump the package version manually here; the release workflow is responsible for updating and synchronizing version numbers.</comment>

<file context>
@@ -1,6 +1,6 @@
 {
   "name": "xmoj-script",
-  "version": "3.3.1",
+  "version": "3.3.2",
   "description": "an improvement script for xmoj.tech",
   "main": "AddonScript.js",
</file context>
Suggested change
"version": "3.3.2",
"version": "3.3.1",
Fix with Cubic

boomzero and others added 2 commits March 14, 2026 22:10
- Capture cid at button-creation time from the outer null-checked scope
  instead of re-reading SearchParams inside the click handler
- Hide the button (no-op) if cid is unexpectedly null, avoiding reliance
  on the prefix filter as an accidental-deletion guard
- Extract a local `prefix` variable in the handler to avoid repeating the
  "UserScript-Contest-{cid}-" string literal

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@boomzero boomzero closed this Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M user-script This issue or pull request is related to the main user script

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant