1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-18 21:09:40 +02:00
portainer/app/react/kubernetes/helm/queries/useHelmChartList.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.8 KiB
TypeScript
Raw Normal View History

import { compact } from 'lodash';
import { useQuery } from '@tanstack/react-query';
import axios from '@/portainer/services/axios';
import { withGlobalError } from '@/react-tools/react-query';
import { Chart, HelmChartsResponse } from '../types';
/**
* React hook to fetch helm charts from the provided HTTP repository.
* Charts are loaded from the specified repository URL.
*
* @param userId User ID
* @param repository Repository URL to fetch charts from
* @param enabled Flag indicating if the query should be enabled
* @returns Query result containing helm charts
*/
export function useHelmHTTPChartList(
userId: number,
repository: string,
enabled: boolean
) {
return useQuery({
queryKey: [userId, repository, 'helm-charts'],
queryFn: () => getChartsFromRepo(repository),
enabled: !!userId && !!repository && enabled,
// one request takes a long time, so fail early to get feedback to the user faster
retry: false,
...withGlobalError(`Unable to retrieve Helm charts from ${repository}`),
});
}
async function getChartsFromRepo(repo: string): Promise<Chart[]> {
try {
// Construct the URL with required repo parameter
const response = await axios.get<HelmChartsResponse>('templates/helm', {
params: { repo },
});
return compact(
Object.values(response.data.entries).map((versions) =>
versions[0]
? {
...versions[0],
repo,
// versions are within this response too, so we don't need a new query to fetch versions when this is used
versions: versions.map((v) => v.version),
}
: null
)
);
} catch (error) {
// Ignore errors from chart repositories as some may error but others may not
return [];
}
}