Skip to content

Commit 0a68faa

Browse files
refactor: refactored useTaskRuns.ts
1 parent 4dc6fca commit 0a68faa

4 files changed

Lines changed: 61 additions & 121 deletions

File tree

src/components/hooks/useTaskRuns.ts

Lines changed: 44 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export type UseTaskRunsOptions = {
6969
pipelineRunFinished?: boolean;
7070
pipelineRunManagedBy?: string;
7171
skipFetch?: boolean;
72+
name?: string /* used for fetching a single task run by metadata.name */;
73+
limit?: number /* used for fetching a limited number of task runs */;
7274
};
7375

7476
export const useTaskRuns = (
@@ -78,7 +80,7 @@ export const useTaskRuns = (
7880
pipelineRunUid?: string,
7981
options?: UseTaskRunsOptions,
8082
): [TaskRunKind[], boolean, boolean, Error | undefined, boolean, boolean] => {
81-
const { pipelineRunFinished, pipelineRunManagedBy, skipFetch } =
83+
const { pipelineRunFinished, pipelineRunManagedBy, skipFetch, name, limit } =
8284
options || {};
8385
const selector: Selector = useMemo(() => {
8486
if (pipelineRunName && pipelineRunUid) {
@@ -106,9 +108,10 @@ export const useTaskRuns = (
106108
error,
107109
pendingAdmission,
108110
proxyUnavailable,
109-
] = useTaskRuns2(
111+
] = useRuns<TaskRunKind>(
112+
TASK_RUN_GVK,
110113
namespace,
111-
{ selector, skipFetch },
114+
{ selector, skipFetch, name, limit },
112115
pipelineRunFinished,
113116
pipelineRunManagedBy,
114117
);
@@ -150,24 +153,6 @@ export const useTaskRuns = (
150153
);
151154
};
152155

153-
export const useTaskRuns2 = (
154-
namespace: string,
155-
options?: {
156-
selector?: Selector;
157-
limit?: number;
158-
skipFetch?: boolean;
159-
},
160-
pipelineRunFinished?: boolean,
161-
pipelineRunManagedBy?: string,
162-
): [TaskRunKind[], boolean, boolean, Error | undefined, boolean, boolean] =>
163-
useRuns<TaskRunKind>(
164-
TASK_RUN_GVK,
165-
namespace,
166-
options,
167-
pipelineRunFinished,
168-
pipelineRunManagedBy,
169-
);
170-
171156
export const useApprovalTasks = (
172157
namespace: string,
173158
pipelineRunName?: string,
@@ -205,32 +190,14 @@ export const usePipelineRuns = (
205190
options?: {
206191
selector?: Selector;
207192
limit?: number;
193+
name?: string;
208194
},
209195
): [PipelineRunKind[], boolean, boolean, Error | undefined, boolean, boolean] =>
210-
useRuns<PipelineRunKind>(PIPELINE_RUN_GVK, namespace, options);
211-
212-
export const usePipelineRun = (
213-
namespace: string,
214-
pipelineRunName: string,
215-
): [PipelineRunKind, boolean, boolean, Error | undefined] => {
216-
const result = usePipelineRuns(
217-
namespace,
218-
useMemo(
219-
() => ({
220-
name: pipelineRunName,
221-
limit: 1,
222-
}),
223-
[pipelineRunName],
224-
),
225-
) as unknown as [PipelineRunKind[], boolean, boolean, Error | undefined];
226-
227-
return [
228-
result[0]?.[0],
229-
result[1],
230-
result[2],
231-
result[0]?.[0] ? undefined : result[3],
232-
];
233-
};
196+
useRuns<PipelineRunKind>(PIPELINE_RUN_GVK, namespace, {
197+
selector: options?.selector,
198+
limit: options?.limit /* similar to one present in UseTaskRunsOptions */,
199+
name: options?.name /* similar to one present in UseTaskRunsOptions */,
200+
});
234201

235202
export const useRuns = <Kind extends K8sResourceKind>(
236203
groupVersionKind: K8sGroupVersionKind,
@@ -380,84 +347,45 @@ export const useRuns = <Kind extends K8sResourceKind>(
380347
optionsMemo?.skipFetch,
381348
);
382349

383-
return useMemo(() => {
384-
// dedupe PLR by name since UIDs differ between hub and spoke clusters; for other cases(TR) dedupe by UID
350+
// dedupe PLR by name since UIDs differ between hub and spoke clusters; for other cases(TR) dedupe by UID
385351

386-
const rResources: Kind[] =
387-
runs && trResources
388-
? !isTaskRunQuery
389-
? uniqBy([...runs, ...trResources], (r) => r.metadata.name)
390-
: uniqBy([...runs, ...trResources], (r) => r.metadata.uid)
391-
: runs || trResources;
352+
const rResources: Kind[] =
353+
runs && trResources
354+
? !isTaskRunQuery
355+
? uniqBy([...runs, ...trResources], (r) => r.metadata.name)
356+
: uniqBy([...runs, ...trResources], (r) => r.metadata.uid)
357+
: runs || trResources;
392358

393-
/* Refactoring the nesting as it is causing cognitive damage */
394-
let resolvedError: Error | undefined = undefined;
359+
/* Refactoring the nesting as it is causing cognitive damage */
360+
let resolvedError: Error | undefined = undefined;
395361

396-
if (namespace) {
397-
if (queryTr) {
398-
if (isList) {
399-
resolvedError = trError && effectiveError;
400-
} else {
401-
// when searching by name, return an error if we have no result
402-
if (trError && trLoaded && !trResources?.length) {
403-
resolvedError = effectiveError;
404-
}
405-
}
362+
if (namespace) {
363+
if (queryTr) {
364+
if (isList) {
365+
resolvedError = trError && effectiveError;
406366
} else {
407-
resolvedError = effectiveError;
367+
// when searching by name, return an error if we have no result
368+
if (trError && trLoaded && !trResources?.length) {
369+
resolvedError = effectiveError;
370+
}
408371
}
409-
} else if (effectiveError) {
372+
} else {
410373
resolvedError = effectiveError;
411374
}
375+
} else if (effectiveError) {
376+
resolvedError = effectiveError;
377+
}
412378

413-
const pendingAdmission = shouldUseMultiCluster ? mcPendingAdmission : false;
414-
const proxyUnavailable = shouldUseMultiCluster ? mcProxyUnavailable : false;
415-
416-
const isTrLoaded = trLoaded || !!trError;
417-
return [
418-
rResources,
419-
effectiveLoaded,
420-
isTrLoaded,
421-
resolvedError,
422-
pendingAdmission,
423-
proxyUnavailable,
424-
];
425-
}, [
426-
runs,
427-
trResources,
428-
loaded,
429-
trLoaded,
430-
isTektonResultEnabled,
431-
namespace,
432-
queryTr,
433-
isList,
434-
trError,
435-
error,
436-
]);
437-
};
438-
439-
export const useTaskRun = (
440-
namespace: string,
441-
taskRunName: string,
442-
): [TaskRunKind, boolean, boolean, string] => {
443-
const result = useTaskRuns2(
444-
namespace,
445-
useMemo(
446-
() => ({
447-
name: taskRunName,
448-
limit: 1,
449-
}),
450-
[taskRunName],
451-
),
452-
) as unknown as [TaskRunKind[], boolean, boolean, string];
379+
const pendingAdmission = shouldUseMultiCluster ? mcPendingAdmission : false;
380+
const proxyUnavailable = shouldUseMultiCluster ? mcProxyUnavailable : false;
453381

454-
return useMemo(
455-
() => [
456-
result[0]?.[0],
457-
result[1],
458-
result[2],
459-
result[0]?.[0] ? undefined : result[3],
460-
],
461-
[result],
462-
);
382+
const isTrLoaded = trLoaded || !!trError;
383+
return [
384+
rResources,
385+
effectiveLoaded,
386+
isTrLoaded,
387+
resolvedError,
388+
pendingAdmission,
389+
proxyUnavailable,
390+
];
463391
};

src/components/pipelineRuns-details/PipelineRunDetailsPage.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
import PipelineRunParametersForm from './PipelineRunParametersForm';
3232
import { PipelineRunLogsWithActiveTask } from './PipelineRunLogs';
3333
import PipelineRunEvents from './PipelineRunEvents';
34-
import { usePipelineRun } from '../hooks/useTaskRuns';
34+
import { usePipelineRuns } from '../hooks/useTaskRuns';
3535
import { getReferenceForModel } from '../pipelines-overview/utils';
3636
import { LazyActionMenu } from '@openshift-console/dynamic-plugin-sdk-internal';
3737
import { ActionMenuVariant } from '@openshift-console/dynamic-plugin-sdk-internal/lib/api/internal-types';
@@ -46,7 +46,11 @@ const PipelineRunDetailsPage: FC<PipelineRunDetailsPageProps> = ({
4646
namespace,
4747
}) => {
4848
const { t } = useTranslation('plugin__pipelines-console-plugin');
49-
const [pipelineRun, k8sLoaded, trLoaded] = usePipelineRun(namespace, name);
49+
const [pipelineRuns, k8sLoaded, trLoaded] = usePipelineRuns(namespace, {
50+
name,
51+
limit: 1,
52+
});
53+
const pipelineRun = pipelineRuns?.[0];
5054
/* this needs decoupling */
5155
const pipelineRunLoaded = k8sLoaded || trLoaded;
5256

src/components/pipelineRuns-details/PipelineRunVisualization.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const PipelineRunVisualization: FC<PipelineRunVisualizationProps> = ({
2323
plrStatus !== ComputedStatus.Running &&
2424
plrStatus !== ComputedStatus.Pending &&
2525
plrStatus !== ComputedStatus.Cancelling;
26-
const [taskRuns, k8sLoaded, trLoaded, pendingAdmission, proxyUnavailable] =
26+
const [taskRuns, k8sLoaded, trLoaded, , pendingAdmission, proxyUnavailable] =
2727
useTaskRuns(
2828
pipelineRun?.metadata?.namespace,
2929
pipelineRun?.metadata?.name,

src/components/pipelines-tasks/tasks-details-pages/TaskRunDetailsPage.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useTranslation } from 'react-i18next';
1616
import TaskRunDetails from './TaskRunDetails';
1717
import TaskRunLogsTab from './TaskRunLogsTab';
1818
import TaskRunEvents from './events/TaskRunEvents';
19-
import { useTaskRun } from '../../hooks/useTaskRuns';
19+
import { useTaskRuns } from '../../hooks/useTaskRuns';
2020
import { navFactory } from '../../utils/horizontal-nav';
2121
import ResourceYAMLEditorViewOnly from '../../yaml-editor/ResourceYAMLEditorViewOnly';
2222
import {
@@ -31,7 +31,15 @@ const TaskRunDetailsPage = () => {
3131
const { t } = useTranslation('plugin__pipelines-console-plugin');
3232
const params = useParams();
3333
const { name, ns: namespace } = params;
34-
const [data, k8sLoaded, trLoaded] = useTaskRun(namespace, name);
34+
/* Returning 1 taskrun */
35+
const [taskRuns, k8sLoaded, trLoaded] = useTaskRuns(
36+
namespace,
37+
undefined, // pipelineRunName
38+
undefined, // taskName — this is a label filter so can't be used for fetching 1 item without some refactoring
39+
undefined, // pipelineRunUid
40+
{ name, limit: 1 },
41+
);
42+
const data = taskRuns?.[0];
3543
const loaded = k8sLoaded || trLoaded;
3644
const trStatus = useMemo(
3745
() => loaded && data && taskRunStatus(data),

0 commit comments

Comments
 (0)