-
Notifications
You must be signed in to change notification settings - Fork 0
[tasks] persist filter state on back navigation #37
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,11 +12,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Button } from "$lib/components/ui/button"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Project, Task } from "$lib/types/api"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ACTIVE_STATUSES } from "$lib/types/api"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getSnapshot, saveSnapshot } from "$lib/stores/task-filters.svelte"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // svelte-ignore a11y_click_events_have_key_events | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let { params = {} }: { params?: Record<string, string> } = $props(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let slug = $derived(params.slug || ""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let routeKey = $derived(`/projects/${slug}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let project = $state<Project | null>(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tasks = $state<Task[]>([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -31,6 +33,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let filterPriorities = $state<string[]>([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let searchQuery = $state(""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let saved = getSnapshot(routeKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (saved) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterStatuses = saved.filterStatuses; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterPriorities = saved.filterPriorities; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort = saved.sort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset = saved.offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function toggleStatus(s: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (filterStatuses.includes(s)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterStatuses = filterStatuses.filter((x) => x !== s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -84,6 +94,15 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $effect(load); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $effect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveSnapshot(routeKey, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterStatuses: [...filterStatuses], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterPriorities: [...filterPriorities], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Prevent saving old state to the new project during navigation. When navigating between projects, Add a guard to ensure state is only saved when the route key is stable and fully synchronized with the component state. This implementation assumes 🐛 Proposed fix $effect(() => {
+ if (routeKey !== currentRouteKey) return;
+
saveSnapshot(routeKey, {
filterStatuses: [...filterStatuses],
filterPriorities: [...filterPriorities],
sort,
offset,
});
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Prevent saving old state to the new project during navigation. When navigating between projects, Add a guard to ensure state is only saved when the route key is stable. This assumes 🐛 Proposed fix $effect(() => {
+ if (routeKey !== currentRouteKey) return;
+
saveSnapshot(routeKey, {
filterStatuses: [...filterStatuses],
filterPriorities: [...filterPriorities],
sort,
offset,
});
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function handleSort(field: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort = sort === field ? `-${field}` : field; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| export type FilterSnapshot = { | ||
| filterProject?: string | ||
| filterStatuses: string[] | ||
| filterPriorities: string[] | ||
| sort: string | ||
| offset: number | ||
| } | ||
|
|
||
| let store = $state<Record<string, FilterSnapshot>>({}) | ||
|
|
||
| export function getSnapshot(path: string): FilterSnapshot | null { | ||
| return store[path] ?? null | ||
| } | ||
|
|
||
| export function saveSnapshot(path: string, snapshot: FilterSnapshot): void { | ||
| store[path] = snapshot | ||
| } | ||
|
|
||
| export function clearSnapshot(path: string): void { | ||
| delete store[path] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Make snapshot restoration reactive to route changes.
Svelte 5 component script blocks run only once upon initialization. Because
routeKeyis derived from dynamic route parameters (params.slug), navigating from one project directly to another reuses the same component instance and updatesrouteKeyreactively, but this top-level initialization block will not re-execute. This causes the new project to incorrectly inherit the previous project's filter state.To fix this, wrap the restoration in an
$effectso it reacts to dynamic parameter changes. EnsuresearchQueryandsortare cleared when no snapshot exists to prevent UI state from leaking across projects.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Make snapshot restoration reactive to route changes.
Svelte 5 component script blocks only run once on initialization. Because
routeKeyis derived from dynamic route parameters (params.slug), navigating between projects reuses the component and updatesrouteKey, but this initialization block will not re-run. This causes the new project to inherit the previous project's state.Wrap the restoration in an
$effectto handle dynamic parameter changes, and ensuresearchQueryandsortare cleared to prevent state leaking when no snapshot exists.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents