Skip to content

Commit

Permalink
fix: query only when container view is active
Browse files Browse the repository at this point in the history
  • Loading branch information
mikededo committed Nov 3, 2023
1 parent afde215 commit c8b1e3c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useQuery } from '@tanstack/react-query';
import { useDebounce } from 'use-debounce';

import type { DockerResponse } from '@/editor/domain/docker';
import { useJobView } from '@/editor/hooks';

const fetchDockerHub = async (searchTerm: string): Promise<DockerResponse> => {
const response = await fetch(`/api/docker?page_size=25&q=${searchTerm}`, {
Expand All @@ -17,11 +18,12 @@ const fetchDockerHub = async (searchTerm: string): Promise<DockerResponse> => {

export const useDockerHub = (term: string) => {
const [debouncedTerm] = useDebounce(term, 350);
const isContainerViewActive = useJobView('con');

const { data, isLoading, isPending, error } = useQuery({
queryKey: ['docker-images', debouncedTerm],
queryFn: () => fetchDockerHub(debouncedTerm),
enabled: !!debouncedTerm,
enabled: !!debouncedTerm && isContainerViewActive,
});

return { data, isLoading: (isLoading || isPending) && term !== '', error };
Expand Down
1 change: 1 addition & 0 deletions src/apps/editor/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { useChangesStack } from './use-changes-stack';
export { useDraggable } from './use-draggable';
export { useExpandable } from './use-expandable';
export { useSelectedJobId } from './use-selected-job-id';
export { useJobView } from './use-job-view';
export { useSizeObserver } from './use-size-observer';
export { useToggler } from './use-toggler';
export { useViewport } from './use-viewport';
8 changes: 8 additions & 0 deletions src/apps/editor/hooks/use-job-view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useSearchParam } from '@/chain';

import type { Views } from '../components/job-dialog/types';

export const useJobView = (view: Views) => {
const { getParam } = useSearchParam();
return getParam('view') === view;
};

0 comments on commit c8b1e3c

Please sign in to comment.