Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightly_studio_view/src/lib/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ export {
useImageAnnotationCounts,
useImageAnnotationCountsQueryKey
} from '$lib/hooks/useImageAnnotationCounts/useImageAnnotationCounts';
export { useImageAnnotationCountsBySampleTags } from '$lib/hooks/useImageAnnotationCountsBySampleTags/useImageAnnotationCountsBySampleTags.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createQuery } from '@tanstack/svelte-query';
import type {
AnnotationCountMode,
AnnotationType,
ImageFilter,
SampleTagAnnotationCountsView
} from '$lib/api/lightly_studio_local';
import {
countImageAnnotationsBySampleTagsOptions,
countImageAnnotationsBySampleTagsQueryKey
} from '$lib/api/lightly_studio_local/@tanstack/svelte-query.gen';
import { countImageAnnotationsBySampleTags } from '$lib/api/lightly_studio_local/sdk.gen';
import { useImageAnnotationCountsQueryKey } from '../useImageAnnotationCounts/useImageAnnotationCounts';

interface GroupedAnnotationCountsParams {
collectionId: string;
sampleTagIds: string[];
filter?: ImageFilter;
annotationType?: AnnotationType;
countMode?: AnnotationCountMode;
}

export function buildImageAnnotationCountsBySampleTagsRequest({
collectionId,
sampleTagIds,
filter,
annotationType,
countMode
}: GroupedAnnotationCountsParams) {
return {
path: { collection_id: collectionId },
body: {
sample_tag_ids: sampleTagIds,
...(filter ? { filter } : {}),
...(annotationType ? { annotation_type: annotationType } : {}),
...(countMode ? { count_mode: countMode } : {})
}
};
}

export function buildImageAnnotationCountsBySampleTagsQueryKey(
params: GroupedAnnotationCountsParams
): ReturnType<typeof countImageAnnotationsBySampleTagsQueryKey> {
return [
...useImageAnnotationCountsQueryKey,
'by-sample-tags',
buildImageAnnotationCountsBySampleTagsRequest(params)
] as unknown as ReturnType<typeof countImageAnnotationsBySampleTagsQueryKey>;
}

export const useImageAnnotationCountsBySampleTags = (
getParams: () => GroupedAnnotationCountsParams & { enabled?: boolean }
) => {
return createQuery(() => {
const { enabled, ...params } = getParams();
const requestOptions = buildImageAnnotationCountsBySampleTagsRequest(params);

return {
...countImageAnnotationsBySampleTagsOptions(requestOptions),
queryKey: buildImageAnnotationCountsBySampleTagsQueryKey(params),
queryFn: async ({ signal }: { signal: AbortSignal }) => {
const { data } = await countImageAnnotationsBySampleTags({
...requestOptions,
signal,
throwOnError: true
});
return data;
},
enabled: (enabled ?? true) && params.sampleTagIds.length > 0,
placeholderData: (previousData: SampleTagAnnotationCountsView[] | undefined) =>
previousData
};
});
};