update deps including useSWR 1.0

This commit is contained in:
spencerwooo
2022-02-02 16:43:17 +08:00
parent 89dcc1f614
commit 9794fb2df4
4 changed files with 724 additions and 641 deletions
+7 -20
View File
@@ -1,5 +1,7 @@
import axios from 'axios'
import useSWR, { cache, Key, useSWRInfinite } from 'swr'
import useSWRInfinite from 'swr/infinite'
import type { OdAPIResponse } from '../types'
import { getStoredToken } from './protectedRouteHandler'
@@ -17,22 +19,6 @@ export async function fetcher(url: string, token?: string): Promise<any> {
throw { status: err.response.status, message: err.response.data }
}
}
/**
* Use stale SWR instead of revalidating on each request. Not ideal for this scenario but have to do
* if fetching serverside props from component instead of pages.
* @param url request url
* @returns useSWR instance
*/
export function useStaleSWR({ url, path = '' }: { url: Key; path?: string }) {
const revalidationOptions = {
revalidateOnMount: !(cache.has(`arg@"${url}"@null`) || cache.has(url)),
revalidateOnFocus: false,
revalidateOnReconnect: true,
}
const hashedToken = getStoredToken(path)
return useSWR([url, hashedToken], fetcher, revalidationOptions)
}
/**
* Paging with useSWRInfinite + protected token support
@@ -49,7 +35,7 @@ export function useProtectedSWRInfinite(path: string = '') {
* @param path Directory path
* @returns API to the next page
*/
function getNextKey(pageIndex, previousPageData): (string | null)[] | null {
function getNextKey(pageIndex: number, previousPageData: OdAPIResponse): (string | null)[] | null {
// Reached the end of the collection
if (previousPageData && !previousPageData.folder) return null
@@ -60,11 +46,12 @@ export function useProtectedSWRInfinite(path: string = '') {
return [`/api?path=${path}&next=${previousPageData.next}`, hashedToken]
}
// Disable auto-revalidate, these options are equivalent to useSWRImmutable
// https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
const revalidationOptions = {
revalidateOnMount: !(cache.has(`arg@"/api?path=${path}"@null`) || cache.has(`/api?path=${path}`)),
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: true,
}
// return useSWRInfinite(getNextKey, fetcher)
return useSWRInfinite(getNextKey, fetcher, revalidationOptions)
}