Skip to content

Commit 5947cfc

Browse files
committed
prefetch first page
1 parent 3db46b2 commit 5947cfc

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
import { Suspense } from 'react'
22
import LoadingLayout from '@/features/dashboard/loading-layout'
3+
import {
4+
TEMPLATES_DEFAULT_SORT,
5+
TEMPLATES_PAGE_SIZE,
6+
} from '@/features/dashboard/templates/list/constants'
37
import TemplatesTable from '@/features/dashboard/templates/list/table'
8+
import { HydrateClient, prefetch, trpc } from '@/trpc/server'
9+
10+
export default async function TemplatesListPage({
11+
params,
12+
}: PageProps<'/dashboard/[teamSlug]/templates/list'>) {
13+
const { teamSlug } = await params
14+
15+
prefetch(
16+
trpc.templates.getTemplates.infiniteQueryOptions({
17+
teamSlug,
18+
limit: TEMPLATES_PAGE_SIZE,
19+
sort: TEMPLATES_DEFAULT_SORT,
20+
})
21+
)
422

5-
export default function TemplatesListPage() {
623
return (
7-
<Suspense fallback={<LoadingLayout />}>
8-
<TemplatesTable />
9-
</Suspense>
24+
<HydrateClient>
25+
<Suspense fallback={<LoadingLayout />}>
26+
<TemplatesTable />
27+
</Suspense>
28+
</HydrateClient>
1029
)
1130
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { SortingState } from '@tanstack/react-table'
2+
import type { TemplatesSort } from '@/core/modules/templates/models'
3+
4+
export const TEMPLATES_PAGE_SIZE = 50
5+
6+
export const TEMPLATES_DEFAULT_SORT_COLUMN_ID = 'createdAt'
7+
export const TEMPLATES_DEFAULT_SORT_BASE = 'created_at'
8+
export const TEMPLATES_DEFAULT_SORT_DESC = true
9+
10+
export const TEMPLATES_DEFAULT_SORT: TemplatesSort = `${TEMPLATES_DEFAULT_SORT_BASE}_${
11+
TEMPLATES_DEFAULT_SORT_DESC ? 'desc' : 'asc'
12+
}`
13+
14+
export const TEMPLATES_DEFAULT_SORTING: SortingState = [
15+
{ id: TEMPLATES_DEFAULT_SORT_COLUMN_ID, desc: TEMPLATES_DEFAULT_SORT_DESC },
16+
]

src/features/dashboard/templates/list/stores/table-store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { OnChangeFn, SortingState } from '@tanstack/react-table'
22
import { create } from 'zustand'
33
import { createJSONStorage, persist } from 'zustand/middleware'
44
import { createHashStorage } from '@/lib/utils/store'
5+
import { TEMPLATES_DEFAULT_SORTING } from '../constants'
56
import { trackTemplateTableInteraction } from '../table-config'
67

78
interface TemplateTableState {
@@ -33,7 +34,7 @@ type Store = TemplateTableState & TemplateTableActions
3334

3435
const initialState: TemplateTableState = {
3536
// Table state
36-
sorting: [{ id: 'updatedAt', desc: true }],
37+
sorting: TEMPLATES_DEFAULT_SORTING,
3738
globalFilter: '',
3839
// Filter state
3940
cpuCount: undefined,

src/features/dashboard/templates/list/table.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ import {
3131
import ErrorBoundary from '@/ui/error'
3232
import HelpTooltip from '@/ui/help-tooltip'
3333
import { SIDEBAR_TRANSITION_CLASSNAMES } from '@/ui/primitives/sidebar'
34+
import {
35+
TEMPLATES_DEFAULT_SORT_BASE,
36+
TEMPLATES_DEFAULT_SORT_DESC,
37+
TEMPLATES_PAGE_SIZE,
38+
} from './constants'
3439
import TemplatesHeader from './header'
3540
import { useTemplateTableStore } from './stores/table-store'
3641
import { TemplatesTableBody as TableBody } from './table-body'
3742
import { fallbackData, templatesTableConfig, useColumns } from './table-config'
3843

39-
const PAGE_SIZE = 50
40-
4144
const COLUMN_TO_SORT_BASE: Record<string, string> = {
4245
name: 'name',
4346
cpuCount: 'cpu_count',
@@ -59,9 +62,12 @@ export default function TemplatesTable() {
5962
// Derive the single server sort token from the active sort column + direction.
6063
const sortColumn = sorting[0]
6164
const sortBase =
62-
(sortColumn && COLUMN_TO_SORT_BASE[sortColumn.id]) ?? 'created_at'
63-
const sort =
64-
`${sortBase}_${sortColumn?.desc === false ? 'asc' : 'desc'}` as TemplatesSort
65+
(sortColumn && COLUMN_TO_SORT_BASE[sortColumn.id]) ??
66+
TEMPLATES_DEFAULT_SORT_BASE
67+
const isDesc = sortColumn
68+
? sortColumn.desc !== false
69+
: TEMPLATES_DEFAULT_SORT_DESC
70+
const sort = `${sortBase}_${isDesc ? 'desc' : 'asc'}` as TemplatesSort
6571

6672
const {
6773
data,
@@ -74,7 +80,7 @@ export default function TemplatesTable() {
7480
trpc.templates.getTemplates.infiniteQueryOptions(
7581
{
7682
teamSlug,
77-
limit: PAGE_SIZE,
83+
limit: TEMPLATES_PAGE_SIZE,
7884
cpuCount,
7985
memoryMB,
8086
public: isPublic,

0 commit comments

Comments
 (0)