2024-04-14 14:54:25 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2023-07-10 15:56:12 +00:00
|
|
|
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
|
2024-06-10 18:54:31 +00:00
|
|
|
import { buildDockerUrl } from '../../queries/utils/buildDockerUrl';
|
|
|
|
|
2023-07-10 15:56:12 +00:00
|
|
|
import { queryKeys } from './queryKeys';
|
|
|
|
|
2023-10-03 12:55:23 +00:00
|
|
|
export interface ImagesListResponse {
|
|
|
|
created: number;
|
|
|
|
nodeName?: string;
|
|
|
|
id: string;
|
|
|
|
size: number;
|
2024-01-05 20:33:42 +00:00
|
|
|
tags?: string[];
|
2023-07-10 15:56:12 +00:00
|
|
|
|
|
|
|
/**
|
2023-10-03 12:55:23 +00:00
|
|
|
* Used is true if the image is used by at least one container.
|
|
|
|
* supplied only when withUsage is true
|
2023-07-10 15:56:12 +00:00
|
|
|
*/
|
2023-10-03 12:55:23 +00:00
|
|
|
used: boolean;
|
2023-07-10 15:56:12 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:54:31 +00:00
|
|
|
/**
|
|
|
|
* Used in ImagesDatatable
|
|
|
|
*
|
|
|
|
* Query /api/docker/{envId}/images
|
|
|
|
*/
|
2023-10-03 12:55:23 +00:00
|
|
|
export function useImages<T = Array<ImagesListResponse>>(
|
2023-07-10 15:56:12 +00:00
|
|
|
environmentId: EnvironmentId,
|
2023-10-03 12:55:23 +00:00
|
|
|
withUsage = false,
|
2023-07-10 15:56:12 +00:00
|
|
|
{
|
|
|
|
select,
|
|
|
|
enabled,
|
2023-10-03 12:55:23 +00:00
|
|
|
refetchInterval,
|
|
|
|
}: {
|
|
|
|
select?(data: Array<ImagesListResponse>): T;
|
|
|
|
enabled?: boolean;
|
|
|
|
refetchInterval?: number;
|
|
|
|
} = {}
|
2023-07-10 15:56:12 +00:00
|
|
|
) {
|
|
|
|
return useQuery(
|
2023-10-03 12:55:23 +00:00
|
|
|
queryKeys.list(environmentId, { withUsage }),
|
|
|
|
() => getImages(environmentId, { withUsage }),
|
|
|
|
{ select, enabled, refetchInterval }
|
2023-07-10 15:56:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:55:23 +00:00
|
|
|
async function getImages(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
{ withUsage }: { withUsage?: boolean } = {}
|
|
|
|
) {
|
2023-07-10 15:56:12 +00:00
|
|
|
try {
|
2023-10-03 12:55:23 +00:00
|
|
|
const { data } = await axios.get<Array<ImagesListResponse>>(
|
2024-06-10 18:54:31 +00:00
|
|
|
buildDockerUrl(environmentId, 'images'),
|
2023-10-03 12:55:23 +00:00
|
|
|
{ params: { withUsage } }
|
2023-07-10 15:56:12 +00:00
|
|
|
);
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
2024-05-13 03:34:00 +00:00
|
|
|
throw parseAxiosError(err, 'Unable to retrieve images');
|
2023-07-10 15:56:12 +00:00
|
|
|
}
|
|
|
|
}
|