File size: 5,393 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Data Library and Layers

## Overview

The dashboard uses a light approach to data fetching and state management. It leverages [TanStack Query](https://tanstack.com/query) (formerly React Query) for server state management and avoids Redux in favor of a more direct, hooks-based approach.

## Architecture

The data library is organized around these key principles:

1. **REST API-centric**: All data is fetched from the WordPress.com REST API
2. **Type safety**: Strong TypeScript types for all data structures
3. **Data separation**: Clear separation between fetching logic and UI components
4. **Caching**: Simple caching strategies for optimized performance: Load from local cache first and refetch on navigation/focus.
5. **Loader pattern**: Data is prefetched through route loaders where possible

The data library consist of two layers:

- data source
- data queries (with state management)

## Data source: `client/dashboard/data/`

This layer exports the API integration with the REST API, and the data types that are consumed by the dashboard components.

### API integration

The data source layer uses `wpcom` from `calypso/lib/wp` for REST API calls. Each data requirement has corresponding fetch functions in `client/dashboard/data/<endpoint-group>.ts`. For example:

`client/dashboard/data/site-reset.ts`:
```typescript
export async function fetchSiteResetStatus( siteId: number ): Promise< SiteResetStatus > {
	return wpcom.req.get( {
		path: `/sites/${ siteId }/reset-site/status`,
		apiNamespace: 'wpcom/v2',
	} );
}
```

We usually return the raw response from the API. If you need to process the response (e.g. filter), you can do so in the data fetching layer via TanStack's [select](https://tanstack.com/query/latest/docs/framework/react/guides/render-optimizations#select) option.

### Data types

The data source layer defines clear TypeScript interfaces for the data structures returned / consumed by the endpoints, in the same file as the fetch functions. For example:

`client/dashboard/data/site-reset.ts`:
```typescript
export type SiteResetStatus = {
	status: 'in-progress' | 'ready' | 'completed';
	progress: number;
};
```

An "index" `client/dashboard/data/types.ts` is provided, which re-exports all data types from all endpoints. This allows for easy import of all data types from the dashboard components.

## Data queries (fetching and mutation): `client/dashboard/app/queries/`

The dashboard uses a combination of route loaders and component-level queries to fetch data, all using TanStack Query. This allows for both prefetching data for routes and fetching data on-demand within components.

### Route loaders

The primary data-fetching pattern uses route loaders to prefetch data before rendering components:

```typescript
const siteSettingsPHPRoute = createRoute( {
	getParentRoute: () => siteRoute,
	path: 'settings/php',
	loader: async ( { params: { siteSlug } } ) => {
		const site = await queryClient.ensureQueryData( siteBySlugQuery( siteSlug ) );
		if ( hasHostingFeature( site, HostingFeatures.PHP ) ) {
			await queryClient.ensureQueryData( sitePHPVersionQuery( site.ID ) );
		}
	},
} ).lazy( () =>
	import( '../sites/settings-php' ).then( ( d ) =>
		createLazyRoute( 'site-settings-php' )( {
			component: () => <d.default siteSlug={ siteRoute.useParams().siteSlug } />,
		} )
	)
);
```

TanStack's `queryClient.ensureQueryData` checks if data is already cached before fetching, improving performance by avoiding unnecessary requests.

### Component-level queries

At the component level, we use the `useQuery` hook to fetch data. This includes the data that we preloaded in the route loader:


```typescript
const { data: currentVersion } = useQuery( {
	...sitePHPVersionQuery( site.ID ),
	enabled: hasHostingFeature( site, HostingFeatures.PHP ),
} );
```

as well as data that are specific to a component or that need more dynamic control:

```typescript
const { data: siteContentSummary } = useQuery(
	siteResetContentSummaryQuery( site.ID )
);
```

### Queries centralization

Queries are reused between loaders and components. To encourage cache reusability between different parts of the app, we centralize the queries definitions in the `client/dashboard/app/queries/` directory.

## Adding new data sources

To add a new data source to the dashboard:

1. Create fetch functions and related types in `client/dashboard/data/<endpoint-group>.ts`.
3. Define a new query in `client/dashboard/app/queries/` if needed.
4. Use the query in a router loader or component.

### Example: adding a new data entity

`client/dashboard/data/entity.ts`:
```typescript
// Define the type
export interface NewEntity {
  id: string;
  name: string;
  // other properties...
}

// Create the fetch function
export async function fetchNewEntity( id: string ): Promise< NewEntity > {
  return wpcom.req.get( {
    path: `/entity/${id}`,
    apiNamespace: 'rest/v1.1',
  } );
}
```

`client/dashboard/app/queries/entity.ts`:
```typescript
// Define the query
export const newEntityQuery = ( id: string ) => ( {
	queryKey: [ 'newEntity', id ],
	queryFn: () => fetchNewEntity( id ),
	staleTime: 5 * 60 * 1000, // 5 minutes
} );
```

```typescript
// Use in a component
const { data, isLoading, error } = useQuery( newEntityQuery( id ) );

// Use in a route loader
loader: ( { params: { id } } ) =>
  queryClient.ensureQueryData( newEntityQuery( id ) ),
```