1
- import { useGetProjects } from '@squonk/data-manager-client/project' ;
1
+ import { QueryClient } from 'react-query' ;
2
+ import { dehydrate } from 'react-query/hydration' ;
2
3
3
- import { withPageAuthRequired } from '@auth0/nextjs-auth0' ;
4
+ import { getFiles , getGetFilesQueryKey } from '@squonk/data-manager-client/file' ;
5
+ import { getGetProjectsQueryKey , getProjects } from '@squonk/data-manager-client/project' ;
6
+
7
+ import { getAccessToken , withPageAuthRequired } from '@auth0/nextjs-auth0' ;
4
8
import { css } from '@emotion/react' ;
5
9
import { Box , Container , Grid , Typography } from '@material-ui/core' ;
6
- import dynamic from 'next/dynamic ' ;
10
+ import type { GetServerSideProps } from 'next' ;
7
11
import Head from 'next/head' ;
8
12
import Image from 'next/image' ;
9
13
10
- import { CenterLoader } from '../components/CenterLoader' ;
11
14
import Layout from '../components/Layout' ;
12
- import type { ProjectTableProps } from '../components/ProjectTable' ;
13
- import type { ProjectFileUploadProps } from '../components/ProjectTable/ProjectFileUpload' ;
14
- import type { ProjectAutocompleteProps } from '../components/userContext/ProjectAutocomplete' ;
15
+ import { ProjectSelection } from '../components/ProjectSelection' ;
16
+ import { ProjectTable } from '../components/ProjectTable' ;
17
+ import { ProjectFileUpload } from '../components/ProjectTable/ProjectFileUpload' ;
18
+ import { ProjectAutocomplete } from '../components/userContext/ProjectAutocomplete' ;
15
19
import { useCurrentProject } from '../hooks/projectHooks' ;
16
20
import { RoleRequired } from '../utils/RoleRequired' ;
21
+ import { options } from '../utils/ssrQueryOptions' ;
22
+
23
+ export const getServerSideProps : GetServerSideProps = async ( { req, res, query } ) => {
24
+ const queryClient = new QueryClient ( ) ;
25
+
26
+ try {
27
+ const { accessToken } = await getAccessToken ( req , res ) ;
17
28
18
- const ProjectSelection = dynamic < unknown > (
19
- ( ) => import ( '../components/ProjectSelection' ) . then ( ( mod ) => mod . ProjectSelection ) ,
20
- { loading : ( ) => < CenterLoader /> } ,
21
- ) ;
29
+ const projectId = query . project as string | undefined ;
30
+ const path = query . path as string [ ] | undefined ;
22
31
23
- const ProjectTable = dynamic < ProjectTableProps > (
24
- ( ) => import ( '../components/ProjectTable' ) . then ( ( mod ) => mod . ProjectTable ) ,
25
- { loading : ( ) => < CenterLoader /> } ,
26
- ) ;
32
+ if ( projectId && accessToken ) {
33
+ const filesParam = { project_id : projectId , path : '/' + ( path ?. join ( '/' ) ?? '' ) } ;
27
34
28
- const ProjectFileUpload = dynamic < ProjectFileUploadProps > (
29
- ( ) => import ( '../components/ProjectTable/ProjectFileUpload' ) . then ( ( mod ) => mod . ProjectFileUpload ) ,
30
- { loading : ( ) => < CenterLoader /> } ,
31
- ) ;
35
+ // Prefetch some data
36
+ const queries = [
37
+ queryClient . prefetchQuery ( getGetProjectsQueryKey ( ) , ( ) =>
38
+ getProjects ( options ( accessToken ) ) ,
39
+ ) ,
40
+ queryClient . prefetchQuery ( getGetFilesQueryKey ( filesParam ) , ( ) =>
41
+ getFiles ( filesParam , options ( accessToken ) ) ,
42
+ ) ,
43
+ ] ;
32
44
33
- const ProjectAutocomplete = dynamic < ProjectAutocompleteProps > (
34
- ( ) =>
35
- import ( '../components/userContext/ProjectAutocomplete' ) . then ( ( mod ) => mod . ProjectAutocomplete ) ,
36
- { loading : ( ) => < CenterLoader /> } ,
37
- ) ;
45
+ // Make the queries in parallel
46
+ await Promise . allSettled ( queries ) ;
47
+ }
48
+ } catch ( error ) {
49
+ // TODO: smarter handling
50
+ console . error ( error ) ;
51
+ }
52
+
53
+ return {
54
+ props : {
55
+ dehydratedState : dehydrate ( queryClient ) ,
56
+ } ,
57
+ } ;
58
+ } ;
38
59
39
60
/**
40
61
* The project page display and allows the user to manage files inside a project.
41
62
*/
42
63
const Project = ( ) => {
43
64
const currentProject = useCurrentProject ( ) ;
44
- const { isLoading } = useGetProjects ( ) ;
45
65
46
66
return (
47
67
< >
@@ -51,9 +71,7 @@ const Project = () => {
51
71
< RoleRequired roles = { process . env . NEXT_PUBLIC_KEYCLOAK_DM_USER_ROLE ?. split ( ' ' ) } >
52
72
< Layout >
53
73
< Container >
54
- { isLoading ? (
55
- < CenterLoader />
56
- ) : currentProject ? (
74
+ { currentProject ? (
57
75
< >
58
76
< Grid
59
77
container
0 commit comments