-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: use clustering library for more performant marker rendering * fix: tidyup cluster impl * feat: performance optimisation * feat: ssg for static loo props gen * feat: ssg using SchemaLink connecting to db * fix: config
- Loading branch information
Showing
10 changed files
with
451 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import * as Types from './graphql'; | ||
|
||
import * as Operations from './graphql'; | ||
import { NextPage } from 'next'; | ||
import { NextRouter, useRouter } from 'next/router'; | ||
import { QueryHookOptions, useQuery } from '@apollo/client'; | ||
import * as Apollo from '@apollo/client'; | ||
import type React from 'react'; | ||
import { getApolloClient } from '../components/withStaticApollo'; | ||
export async function getServerPageFindLooById( | ||
options: Omit<Apollo.QueryOptions<Types.FindLooByIdQueryVariables>, 'query'>, | ||
ctx?: any | ||
) { | ||
const apolloClient = getApolloClient(ctx); | ||
|
||
const data = await apolloClient.query<Types.FindLooByIdQuery>({ | ||
...options, | ||
query: Operations.FindLooByIdDocument, | ||
}); | ||
|
||
const apolloState = apolloClient.cache.extract(); | ||
|
||
return { | ||
props: { | ||
apolloState: apolloState, | ||
data: data?.data, | ||
error: data?.error ?? data?.errors ?? null, | ||
}, | ||
}; | ||
} | ||
export const useFindLooById = ( | ||
optionsFunc?: ( | ||
router: NextRouter | ||
) => QueryHookOptions<Types.FindLooByIdQuery, Types.FindLooByIdQueryVariables> | ||
) => { | ||
const router = useRouter(); | ||
const options = optionsFunc ? optionsFunc(router) : {}; | ||
return useQuery(Operations.FindLooByIdDocument, options); | ||
}; | ||
export type PageFindLooByIdComp = React.FC<{ | ||
data?: Types.FindLooByIdQuery; | ||
error?: Apollo.ApolloError; | ||
}>; | ||
export const withPageFindLooById = | ||
( | ||
optionsFunc?: ( | ||
router: NextRouter | ||
) => QueryHookOptions< | ||
Types.FindLooByIdQuery, | ||
Types.FindLooByIdQueryVariables | ||
> | ||
) => | ||
(WrappedComponent: PageFindLooByIdComp): NextPage => | ||
(props) => { | ||
const router = useRouter(); | ||
const options = optionsFunc ? optionsFunc(router) : {}; | ||
const { data, error } = useQuery(Operations.FindLooByIdDocument, options); | ||
return <WrappedComponent {...props} data={data} error={error} />; | ||
}; | ||
export const ssrFindLooById = { | ||
getServerPage: getServerPageFindLooById, | ||
withPage: withPageFindLooById, | ||
usePage: useFindLooById, | ||
}; | ||
export async function getServerPageFindLoosNearby( | ||
options: Omit< | ||
Apollo.QueryOptions<Types.FindLoosNearbyQueryVariables>, | ||
'query' | ||
>, | ||
ctx?: any | ||
) { | ||
const apolloClient = getApolloClient(ctx); | ||
|
||
const data = await apolloClient.query<Types.FindLoosNearbyQuery>({ | ||
...options, | ||
query: Operations.FindLoosNearbyDocument, | ||
}); | ||
|
||
const apolloState = apolloClient.cache.extract(); | ||
|
||
return { | ||
props: { | ||
apolloState: apolloState, | ||
data: data?.data, | ||
error: data?.error ?? data?.errors ?? null, | ||
}, | ||
}; | ||
} | ||
export const useFindLoosNearby = ( | ||
optionsFunc?: ( | ||
router: NextRouter | ||
) => QueryHookOptions< | ||
Types.FindLoosNearbyQuery, | ||
Types.FindLoosNearbyQueryVariables | ||
> | ||
) => { | ||
const router = useRouter(); | ||
const options = optionsFunc ? optionsFunc(router) : {}; | ||
return useQuery(Operations.FindLoosNearbyDocument, options); | ||
}; | ||
export type PageFindLoosNearbyComp = React.FC<{ | ||
data?: Types.FindLoosNearbyQuery; | ||
error?: Apollo.ApolloError; | ||
}>; | ||
export const withPageFindLoosNearby = | ||
( | ||
optionsFunc?: ( | ||
router: NextRouter | ||
) => QueryHookOptions< | ||
Types.FindLoosNearbyQuery, | ||
Types.FindLoosNearbyQueryVariables | ||
> | ||
) => | ||
(WrappedComponent: PageFindLoosNearbyComp): NextPage => | ||
(props) => { | ||
const router = useRouter(); | ||
const options = optionsFunc ? optionsFunc(router) : {}; | ||
const { data, error } = useQuery( | ||
Operations.FindLoosNearbyDocument, | ||
options | ||
); | ||
return <WrappedComponent {...props} data={data} error={error} />; | ||
}; | ||
export const ssrFindLoosNearby = { | ||
getServerPage: getServerPageFindLoosNearby, | ||
withPage: withPageFindLoosNearby, | ||
usePage: useFindLoosNearby, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,78 @@ | ||
import { useMemo, useCallback } from 'react'; | ||
import React, { | ||
useMemo, | ||
useCallback, | ||
useState, | ||
useEffect, | ||
useRef, | ||
forwardRef, | ||
} from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import { useFindLoosNearbyQuery } from '../../api-client/graphql'; | ||
import { Marker, useMapEvents, useMap } from 'react-leaflet'; | ||
import ToiletMarkerIcon from './ToiletMarkerIcon'; | ||
|
||
import MarkerClusterGroup from 'react-leaflet-markercluster'; | ||
const KEY_ENTER = 13; | ||
|
||
function mapToFindVars(map) { | ||
return { | ||
lat: map.getCenter().lat, | ||
lng: map.getCenter().lng, | ||
radius: Math.ceil( | ||
map.getBounds().getNorthEast().distanceTo(map.getCenter()) | ||
), | ||
}; | ||
} | ||
|
||
const Markers = ({ focus }) => { | ||
const router = useRouter(); | ||
const map = useMap(); | ||
|
||
const { data, refetch } = useFindLoosNearbyQuery({ | ||
variables: { | ||
...mapToFindVars(map), | ||
}, | ||
const [mapFind, _] = useState({ | ||
lat: 54.093409, | ||
lng: -2.89479, | ||
radius: 1000000, | ||
}); | ||
|
||
useMapEvents({ | ||
moveend: () => refetch(mapToFindVars(map)), | ||
const { data } = useFindLoosNearbyQuery({ | ||
fetchPolicy: 'cache-first', | ||
nextFetchPolicy: 'cache-only', | ||
variables: mapFind, | ||
}); | ||
|
||
const memoizedMarkers = useMemo(() => { | ||
if (!data?.loosByProximity) { | ||
return null; | ||
} | ||
return data.loosByProximity.map((toilet) => ( | ||
<Marker | ||
key={toilet.id} | ||
position={toilet.location} | ||
zIndexOffset={toilet.id === focus?.id ? 1000 : 0} | ||
icon={ | ||
new ToiletMarkerIcon({ | ||
isHighlighted: toilet.id === focus?.id, | ||
toiletId: toilet.id, | ||
isUseOurLoosCampaign: toilet.campaignUOL, | ||
}) | ||
} | ||
alt={toilet.name || 'Unnamed toilet'} | ||
eventHandlers={{ | ||
click: () => { | ||
router.push(`/loos/${toilet.id}`); | ||
}, | ||
keydown: (event: { originalEvent: { keyCode: number } }) => { | ||
if (event.originalEvent.keyCode === KEY_ENTER) { | ||
return data.loosByProximity.map((toilet) => { | ||
return ( | ||
<Marker | ||
key={toilet.id} | ||
position={toilet.location} | ||
zIndexOffset={toilet.id === focus?.id ? 1000 : 0} | ||
icon={ | ||
new ToiletMarkerIcon({ | ||
isHighlighted: toilet.id === focus?.id, | ||
toiletId: toilet.id, | ||
isUseOurLoosCampaign: toilet.campaignUOL, | ||
}) | ||
} | ||
alt={toilet.name || 'Unnamed toilet'} | ||
eventHandlers={{ | ||
click: () => { | ||
router.push(`/loos/${toilet.id}`); | ||
} | ||
}, | ||
}} | ||
keyboard={false} | ||
/> | ||
)); | ||
}, [data, router, focus]); | ||
}, | ||
keydown: (event: { originalEvent: { keyCode: number } }) => { | ||
if (event.originalEvent.keyCode === KEY_ENTER) { | ||
router.push(`/loos/${toilet.id}`); | ||
} | ||
}, | ||
}} | ||
keyboard={false} | ||
/> | ||
); | ||
}); | ||
}, [data, focus, router]); | ||
|
||
return <>{memoizedMarkers}</>; | ||
return ( | ||
<MarkerClusterGroup | ||
chunkedLoading={true} | ||
animateAddingMarkers={false} | ||
removeOutsideVisibleBounds={true} | ||
maxClusterRadius={150} | ||
> | ||
{memoizedMarkers} | ||
</MarkerClusterGroup> | ||
); | ||
}; | ||
|
||
export default Markers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextPage } from 'next'; | ||
import { | ||
ApolloClient, | ||
NormalizedCacheObject, | ||
InMemoryCache, | ||
ApolloProvider, | ||
createHttpLink, | ||
} from '@apollo/client'; | ||
import { SchemaLink } from '@apollo/client/link/schema'; | ||
import { schema } from '../pages/api'; | ||
|
||
export const withApollo = (Comp: NextPage) => | ||
function WithApollo(props: any) { | ||
return ( | ||
<ApolloProvider client={getApolloClient()}> | ||
<Comp /> | ||
</ApolloProvider> | ||
); | ||
}; | ||
|
||
export function getApolloClient(ctx: any): ApolloClient<NormalizedCacheObject> { | ||
return new ApolloClient({ | ||
ssrMode: true, | ||
link: new SchemaLink({ schema }), | ||
cache: new InMemoryCache({}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.