You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

component.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { UseQueryResult, useQuery } from '@tanstack/react-query';
  21. import { getTasksForComponent } from '../api/ce';
  22. import { getMeasuresWithMetrics } from '../api/measures';
  23. import { BranchParameters } from '../types/branch-like';
  24. import { MeasuresAndMetaWithMetrics } from '../types/measures';
  25. import { Component } from '../types/types';
  26. const TASK_RETRY = 10_000;
  27. type QueryKeyData = {
  28. metricKeys: string[];
  29. branchParameters: BranchParameters;
  30. };
  31. function getComponentQueryKey(key: string, type: 'tasks'): string[];
  32. function getComponentQueryKey(key: string, type: 'measures', data: QueryKeyData): string[];
  33. function getComponentQueryKey(key: string, type: string, data?: QueryKeyData): string[] {
  34. return ['component', key, type, JSON.stringify(data)];
  35. }
  36. function extractQueryKeyData(queryKey: string[]): { key: string; data?: QueryKeyData } {
  37. const [, key, , data] = queryKey;
  38. return { key, data: JSON.parse(data ?? 'null') };
  39. }
  40. export function useTaskForComponentQuery(component: Component) {
  41. return useQuery({
  42. queryKey: getComponentQueryKey(component.key, 'tasks'),
  43. queryFn: ({ queryKey }) => {
  44. const { key } = extractQueryKeyData(queryKey);
  45. return getTasksForComponent(key);
  46. },
  47. refetchInterval: TASK_RETRY,
  48. });
  49. }
  50. export function useComponentMeasuresWithMetricsQuery(
  51. key: string,
  52. metricKeys: string[],
  53. branchParameters: BranchParameters,
  54. enabled = true,
  55. ): UseQueryResult<MeasuresAndMetaWithMetrics> {
  56. return useQuery({
  57. enabled,
  58. queryKey: getComponentQueryKey(key, 'measures', {
  59. metricKeys,
  60. branchParameters,
  61. }),
  62. queryFn: ({ queryKey }) => {
  63. const { key, data } = extractQueryKeyData(queryKey);
  64. return data && getMeasuresWithMetrics(key, data.metricKeys, data.branchParameters);
  65. },
  66. });
  67. }