Skip to content

Commit

Permalink
UML diagram caching
Browse files Browse the repository at this point in the history
Fixes #2124
  • Loading branch information
tom2drum committed Aug 9, 2024
1 parent cec56f1 commit 4a7d397
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/api/useApiQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UseQueryOptions } from '@tanstack/react-query';
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';

import type { ResourceError, ResourceName, ResourcePayload } from './resources';
Expand All @@ -7,6 +7,7 @@ import useApiFetch from './useApiFetch';

export interface Params<R extends ResourceName, E = unknown, D = ResourcePayload<R>> extends ApiFetchParams<R> {
queryOptions?: Omit<UseQueryOptions<ResourcePayload<R>, ResourceError<E>, D>, 'queryKey' | 'queryFn'>;
queryKey?: QueryKey;
}

export function getResourceKey<R extends ResourceName>(resource: R, { pathParams, queryParams }: Params<R> = {}) {
Expand All @@ -19,13 +20,13 @@ export function getResourceKey<R extends ResourceName>(resource: R, { pathParams

export default function useApiQuery<R extends ResourceName, E = unknown, D = ResourcePayload<R>>(
resource: R,
{ queryOptions, pathParams, queryParams, fetchParams }: Params<R, E, D> = {},
{ queryOptions, pathParams, queryParams, queryKey, fetchParams }: Params<R, E, D> = {},
) {
const apiFetch = useApiFetch();

return useQuery<ResourcePayload<R>, ResourceError<E>, D>({
// eslint-disable-next-line @tanstack/query/exhaustive-deps
queryKey: getResourceKey(resource, { pathParams, queryParams }),
queryKey: queryKey || getResourceKey(resource, { pathParams, queryParams }),
queryFn: async() => {
// all errors and error typing is handled by react-query
// so error response will never go to the data
Expand Down
1 change: 1 addition & 0 deletions ui/sol2uml/Sol2UmlDiagram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Sol2UmlDiagram = ({ addressHash }: Props) => {
sources: composeSources(contractQuery.data),
},
},
queryKey: [ 'visualize_sol2uml', addressHash ],
queryOptions: {
enabled: Boolean(contractQuery.data),
refetchOnMount: false,
Expand Down

0 comments on commit 4a7d397

Please sign in to comment.