Shadcn Table with Saved Views
Tabs like My open, High priority and Archived each remember their filters, sort, visible columns and density. Change anything and the tab shows it; reset, save, or keep it as a new view. The pattern Linear and Notion use, on TanStack Table state.
$ npx shadcn add https://www.shad-table.dev/r/saved-views-table.json| ID | ||||||
|---|---|---|---|---|---|---|
| ENG-107 | Webhook retries double-send | Todo | Urgent | Priya Shah | 3 | Nov 29 |
| ENG-116 | Mobile table overflows | In review | Urgent | You | 2 | Nov 29 |
| ENG-101 | Checkout fails on saved cards | In progress | Urgent | You | 3 | Nov 28 |
| ENG-105 | Migrate auth sessions to Redis | In progress | High | Jonas Weber | 8 | Nov 28 |
| ENG-102 | Rate limit the public API | Todo | High | You | 5 | Nov 27 |
| ENG-110 | SSO with Okta | In review | High | Priya Shah | 8 | Nov 27 |
| ENG-103 | Flaky invoice export test | In review | Medium | You | 1 | Nov 26 |
| ENG-112 | Slow dashboard on large teams | Todo | High | Maya Chen | 5 | Nov 26 |
| ENG-104 | Dark mode for settings | Done | Low | Maya Chen | 2 | Nov 25 |
| ENG-124 | Duplicate key in audit log | Done | Urgent | Jonas Weber | 2 | Nov 25 |
| ENG-111 | CSV import drops last row | Done | High | You | 2 | Nov 24 |
| ENG-114 | Billing emails in user locale | In progress | Medium | Priya Shah | 3 | Nov 23 |
| ENG-121 | Trial extension requests | Todo | High | Maya Chen | 2 | Nov 22 |
| ENG-115 | Remove legacy v1 endpoints | Todo | Medium | Jonas Weber | 5 | Nov 21 |
| ENG-108 | Onboarding checklist copy | Todo | Medium | You | 1 | Nov 20 |
| ENG-122 | Session timeout too short | Backlog | High | You | 1 | Nov 19 |
| ENG-109 | Search ignores accents | Backlog | Medium | Jonas Weber | 2 | Nov 18 |
| ENG-123 | Export to Parquet | Backlog | Medium | Priya Shah | 5 | Nov 15 |
| ENG-106 | Audit color tokens | Backlog | Low | Maya Chen | 3 | Nov 12 |
| ENG-113 | Keyboard shortcuts help panel | Backlog | Low | You | 2 | Nov 10 |
How it works
1.A view is TanStack's own state, saved as JSON
Filter State Shape shows how to keep filters in one value. A view goes one step further and keeps every slice the table needs to look the same again: columnFilters, sorting and columnVisibility exactly as TanStack stores them, plus density. Nothing is converted on the way in or out, so a view can be stored anywhere that takes JSON.
export type ViewState = {columnFilters: ColumnFiltersStatesorting: SortingStatecolumnVisibility: VisibilityStatedensity: Density}
2.Edits go into a draft per view
The table reads the active view's draft if there is one, otherwise the saved state, and every change writes a draft for that view. Switching tabs never throws work away: go back and your edits are still there, with a dot on the tab until you save or reset.
const state = drafts[activeView.id] ?? activeView.statefunction update(change: Partial<ViewState>) {setDrafts((current) => ({...current,[activeView.id]: { ...state, ...change },}))}
3."Changed" means changed in effect
Ticking a filter and unticking it again, or picking the same filters in another order, should not mark the view as changed. Each state is turned into a normalized string, with filters and values sorted, empty filters dropped and only hidden columns listed, and two states are compared by that string.
export const isModified = (view: SavedView, draft: ViewState | undefined) =>draft !== undefined && fingerprint(draft) !== fingerprint(view.state)
4.Save as view leaves the original alone
Saving a new view copies the current draft into it and opens it, and the view you started from goes back to its saved state, which is what you expect after "Save as". Built-in views cannot be overwritten; your own views get Save and Delete. Custom views are kept in localStorage, read after mount so server rendering matches; in an app you would save them per user on the server.
function saveAsView(name: string) {const view: SavedView = { id: `view-${Date.now()}`, name, state }changeViews([...views, view])dropDraft(activeView.id)setActiveId(view.id)}
5.Counts on every tab, from the same filter code
Each tab shows how many issues it holds, including unsaved changes. The counts run the same matchesFilter function the columns use as their filterFn, so a tab never promises a different number than the table shows when you open it.
export function countMatching(rows: Issue[], filters: ColumnFiltersState) {return rows.filter((row) =>filters.every((f) => matchesFilter(row[f.id as keyof Issue], f.value)),).length}