mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
refactor(containers): migrate view to react [EE-2212] (#6577)
Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
This commit is contained in:
parent
5ee570e075
commit
bed4257194
71 changed files with 1616 additions and 875 deletions
50
app/react/docker/containers/queries/containers.ts
Normal file
50
app/react/docker/containers/queries/containers.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import { EnvironmentId } from '@/portainer/environments/types';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { urlBuilder } from '../containers.service';
|
||||
import { DockerContainerResponse } from '../types/response';
|
||||
import { parseViewModel } from '../utils';
|
||||
|
||||
import { Filters } from './types';
|
||||
import { queryKeys } from './query-keys';
|
||||
|
||||
export function useContainers(
|
||||
environmentId: EnvironmentId,
|
||||
all = true,
|
||||
filters?: Filters,
|
||||
autoRefreshRate?: number
|
||||
) {
|
||||
return useQuery(
|
||||
queryKeys.filters(environmentId, all, filters),
|
||||
() => getContainers(environmentId, all, filters),
|
||||
{
|
||||
meta: {
|
||||
title: 'Failure',
|
||||
message: 'Unable to retrieve containers',
|
||||
},
|
||||
refetchInterval() {
|
||||
return autoRefreshRate ?? false;
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function getContainers(
|
||||
environmentId: EnvironmentId,
|
||||
all = true,
|
||||
filters?: Filters
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<DockerContainerResponse[]>(
|
||||
urlBuilder(environmentId, undefined, 'json'),
|
||||
{
|
||||
params: { all, filters: filters && JSON.stringify(filters) },
|
||||
}
|
||||
);
|
||||
return data.map((c) => parseViewModel(c));
|
||||
} catch (error) {
|
||||
throw parseAxiosError(error as Error, 'Unable to retrieve containers');
|
||||
}
|
||||
}
|
29
app/react/docker/containers/queries/gpus.tsx
Normal file
29
app/react/docker/containers/queries/gpus.tsx
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import { EnvironmentId } from '@/portainer/environments/types';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { queryKeys } from './query-keys';
|
||||
|
||||
async function getContainerGpus(
|
||||
environmentId: EnvironmentId,
|
||||
containerId: string
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<{ gpus: string }>(
|
||||
`/docker/${environmentId}/containers/${containerId}/gpus`
|
||||
);
|
||||
return data.gpus;
|
||||
} catch (err) {
|
||||
throw parseAxiosError(err as Error);
|
||||
}
|
||||
}
|
||||
|
||||
export function useContainerGpus(
|
||||
environmentId: EnvironmentId,
|
||||
containerId: string
|
||||
) {
|
||||
return useQuery(queryKeys.gpus(environmentId, containerId), () =>
|
||||
getContainerGpus(environmentId, containerId)
|
||||
);
|
||||
}
|
19
app/react/docker/containers/queries/query-keys.ts
Normal file
19
app/react/docker/containers/queries/query-keys.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { EnvironmentId } from '@/portainer/environments/types';
|
||||
|
||||
import { queryKeys as dockerQueryKeys } from '../../queries/utils';
|
||||
|
||||
import { Filters } from './types';
|
||||
|
||||
export const queryKeys = {
|
||||
list: (environmentId: EnvironmentId) =>
|
||||
[dockerQueryKeys.root(environmentId), 'containers'] as const,
|
||||
|
||||
filters: (environmentId: EnvironmentId, all?: boolean, filters?: Filters) =>
|
||||
[...queryKeys.list(environmentId), { all, filters }] as const,
|
||||
|
||||
container: (environmentId: EnvironmentId, id: string) =>
|
||||
[...queryKeys.list(environmentId), id] as const,
|
||||
|
||||
gpus: (environmentId: EnvironmentId, id: string) =>
|
||||
[...queryKeys.container(environmentId, id), 'gpus'] as const,
|
||||
};
|
6
app/react/docker/containers/queries/types.ts
Normal file
6
app/react/docker/containers/queries/types.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { NetworkId } from '../../networks/types';
|
||||
|
||||
export interface Filters {
|
||||
label?: string[];
|
||||
network?: NetworkId[];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue