2025-07-13 10:37:43 +12:00
|
|
|
import { compact } from 'lodash';
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2025-05-13 22:15:04 +12:00
|
|
|
|
2025-06-09 20:08:50 +12:00
|
|
|
import axios from '@/portainer/services/axios';
|
2025-05-13 22:15:04 +12:00
|
|
|
import { withGlobalError } from '@/react-tools/react-query';
|
|
|
|
|
2025-06-09 20:08:50 +12:00
|
|
|
import { Chart, HelmChartsResponse } from '../types';
|
2025-05-13 22:15:04 +12:00
|
|
|
|
|
|
|
/**
|
2025-07-13 10:37:43 +12:00
|
|
|
* React hook to fetch helm charts from the provided HTTP repository.
|
|
|
|
* Charts are loaded from the specified repository URL.
|
2025-05-13 22:15:04 +12:00
|
|
|
*
|
|
|
|
* @param userId User ID
|
2025-07-13 10:37:43 +12:00
|
|
|
* @param repository Repository URL to fetch charts from
|
|
|
|
* @param enabled Flag indicating if the query should be enabled
|
|
|
|
* @returns Query result containing helm charts
|
2025-05-13 22:15:04 +12:00
|
|
|
*/
|
2025-07-13 10:37:43 +12:00
|
|
|
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}`),
|
2025-05-13 22:15:04 +12:00
|
|
|
});
|
|
|
|
}
|
2025-06-09 20:08:50 +12:00
|
|
|
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
}
|