Skip to content

Commit 956bd10

Browse files
committed
refactor(frontend): create composables for resource list & get
Create useResourceList and useResourceGet composables to get a useWatch (now useResourceWatch) like API for get/list tasks Signed-off-by: Edward Sammut Alessi <[email protected]>
1 parent 59f4fff commit 956bd10

27 files changed

+220
-78
lines changed

frontend/src/components/SideBar/TSideBar.vue

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
TalosServiceType,
3333
} from '@/api/resources'
3434
import UserInfo from '@/components/common/UserInfo/UserInfo.vue'
35-
import { useWatch } from '@/components/common/Watch/useWatch'
3635
import type { SideBarItem } from '@/components/SideBar/TSideBarList.vue'
3736
import TSidebarList from '@/components/SideBar/TSideBarList.vue'
3837
import { getContext } from '@/context'
@@ -46,6 +45,7 @@ import {
4645
} from '@/methods/auth'
4746
import { useFeatures, useInstallationMediaEnabled } from '@/methods/features'
4847
import { useIdentity } from '@/methods/identity'
48+
import { useResourceWatch } from '@/methods/useResourceWatch'
4949
import ExposedServiceSideBar from '@/views/cluster/ExposedService/ExposedServiceSideBar.vue'
5050
5151
const route = useRoute()
@@ -60,7 +60,7 @@ const { canSyncKubernetesManifests, canManageClusterFeatures } = setupClusterPer
6060
computed(() => route.params.cluster as string),
6161
)
6262
63-
const { data: machineMetrics } = useWatch<MachineStatusMetricsSpec>({
63+
const { data: machineMetrics } = useResourceWatch<MachineStatusMetricsSpec>({
6464
resource: {
6565
namespace: EphemeralNamespace,
6666
type: MachineStatusMetricsType,
@@ -69,7 +69,7 @@ const { data: machineMetrics } = useWatch<MachineStatusMetricsSpec>({
6969
runtime: Runtime.Omni,
7070
})
7171
72-
const { data: infraProviderStatuses } = useWatch<InfraProviderStatusSpec>(() => ({
72+
const { data: infraProviderStatuses } = useResourceWatch<InfraProviderStatusSpec>(() => ({
7373
skip: !canReadMachines.value,
7474
resource: {
7575
namespace: InfraProviderNamespace,
@@ -78,19 +78,18 @@ const { data: infraProviderStatuses } = useWatch<InfraProviderStatusSpec>(() =>
7878
runtime: Runtime.Omni,
7979
}))
8080
81-
const { data: kubernetesUpgradeManifestStatus } = useWatch<KubernetesUpgradeManifestStatusSpec>(
82-
() => ({
81+
const { data: kubernetesUpgradeManifestStatus } =
82+
useResourceWatch<KubernetesUpgradeManifestStatusSpec>(() => ({
8383
skip: !route.params.cluster,
8484
runtime: Runtime.Omni,
8585
resource: {
8686
namespace: DefaultNamespace,
8787
type: KubernetesUpgradeManifestStatusType,
8888
id: route.params.cluster as string,
8989
},
90-
}),
91-
)
90+
}))
9291
93-
const { data: cluster } = useWatch<ClusterSpec>(() => ({
92+
const { data: cluster } = useResourceWatch<ClusterSpec>(() => ({
9493
skip: !route.params.cluster,
9594
runtime: Runtime.Omni,
9695
resource: {
@@ -100,7 +99,7 @@ const { data: cluster } = useWatch<ClusterSpec>(() => ({
10099
},
101100
}))
102101
103-
const { data: services } = useWatch(() => ({
102+
const { data: services } = useResourceWatch(() => ({
104103
skip: !route.params.machine,
105104
resource: {
106105
type: TalosServiceType,

frontend/src/components/common/OngoingTasks/OngoingTasks.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import { KubernetesUpgradeStatusSpecPhase, type OngoingTaskSpec } from '@/api/om
1414
import { EphemeralNamespace, OngoingTaskType } from '@/api/resources'
1515
import { itemID } from '@/api/watch'
1616
import TIcon from '@/components/common/Icon/TIcon.vue'
17-
import { useWatch } from '@/components/common/Watch/useWatch'
1817
import IconHeaderDropdownLoading from '@/components/icons/IconHeaderDropdownLoading.vue'
1918
import { formatISO } from '@/methods/time'
19+
import { useResourceWatch } from '@/methods/useResourceWatch'
2020
21-
const { data } = useWatch<OngoingTaskSpec>(() => ({
21+
const { data } = useResourceWatch<OngoingTaskSpec>(() => ({
2222
resource: {
2323
namespace: EphemeralNamespace,
2424
type: OngoingTaskType,

frontend/src/components/common/Watch/Watch.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { computed } from 'vue'
1818
import type { Resource } from '@/api/grpc'
1919
import type { WatchJoinOptions, WatchOptions, WatchOptionsSingle } from '@/api/watch'
2020
import TSpinner from '@/components/common/Spinner/TSpinner.vue'
21-
import { useWatch } from '@/components/common/Watch/useWatch'
2221
import TAlert from '@/components/TAlert.vue'
22+
import { useResourceWatch } from '@/methods/useResourceWatch'
2323
2424
type Props = {
2525
opts: TOptions
@@ -41,7 +41,7 @@ defineSlots<{
4141
}): unknown
4242
}>()
4343
44-
const { data, err, loading } = useWatch<TSpec, TStatus>(() => props.opts)
44+
const { data, err, loading } = useResourceWatch<TSpec, TStatus>(() => props.opts)
4545
4646
const hasData = computed(() => (Array.isArray(data.value) ? !!data.value.length : !!data.value))
4747
</script>

frontend/src/methods/features.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ResourceService } from '@/api/grpc'
1111
import type { FeaturesConfigSpec } from '@/api/omni/specs/omni.pb'
1212
import { withRuntime } from '@/api/options'
1313
import { DefaultNamespace, FeaturesConfigID, FeaturesConfigType } from '@/api/resources'
14-
import { useWatch } from '@/components/common/Watch/useWatch'
14+
import { useResourceWatch } from '@/methods/useResourceWatch'
1515

1616
const resource = {
1717
type: FeaturesConfigType,
@@ -20,7 +20,7 @@ const resource = {
2020
}
2121

2222
export function useFeatures() {
23-
return useWatch<FeaturesConfigSpec>({
23+
return useResourceWatch<FeaturesConfigSpec>({
2424
resource,
2525
runtime: Runtime.Omni,
2626
})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { type MaybeRefOrGetter, ref, toValue, watchEffect } from 'vue'
2+
3+
import { Runtime } from '@/api/common/omni.pb'
4+
import type { fetchOption } from '@/api/fetch.pb'
5+
import { type Resource, ResourceService } from '@/api/grpc'
6+
import type { GetRequest } from '@/api/omni/resources/resources.pb'
7+
import { withAbortController, withContext, withRuntime, withSelectors } from '@/api/options'
8+
import type { WatchContext } from '@/api/watch'
9+
10+
export interface GetOptions {
11+
resource: GetRequest
12+
runtime: Runtime
13+
context?: WatchContext
14+
selectors?: string[]
15+
skip?: boolean
16+
}
17+
18+
export function useResourceGet<TSpec = unknown, TStatus = unknown>(
19+
opts: MaybeRefOrGetter<GetOptions>,
20+
) {
21+
const data = ref<Resource<TSpec, TStatus>>()
22+
const loading = ref(true)
23+
const error = ref<Error>()
24+
25+
watchEffect(async () => {
26+
const options = toValue(opts)
27+
28+
if (!options.skip) {
29+
await loadData()
30+
}
31+
})
32+
33+
return { data, loading, error, loadData }
34+
35+
async function loadData(abortController?: AbortController) {
36+
const options = toValue(opts)
37+
38+
error.value = undefined
39+
40+
const fetchOptions: fetchOption[] = []
41+
42+
if (options.runtime) {
43+
fetchOptions.push(withRuntime(options.runtime))
44+
}
45+
46+
if (options.selectors) {
47+
fetchOptions.push(withSelectors(options.selectors))
48+
}
49+
50+
if (options.context) {
51+
fetchOptions.push(withContext(options.context))
52+
}
53+
54+
if (abortController) {
55+
fetchOptions.push(withAbortController(abortController))
56+
}
57+
58+
try {
59+
data.value = await ResourceService.Get<Resource<TSpec, TStatus>>(
60+
options.resource,
61+
...fetchOptions,
62+
)
63+
} catch (e) {
64+
error.value = e instanceof Error ? e : new Error(JSON.stringify(e))
65+
} finally {
66+
loading.value = true
67+
}
68+
69+
return data.value
70+
}
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { type MaybeRefOrGetter, ref, toValue, watchEffect } from 'vue'
2+
3+
import { Runtime } from '@/api/common/omni.pb'
4+
import type { fetchOption } from '@/api/fetch.pb'
5+
import { type Resource, ResourceService } from '@/api/grpc'
6+
import type { ListRequest } from '@/api/omni/resources/resources.pb'
7+
import { withAbortController, withContext, withRuntime, withSelectors } from '@/api/options'
8+
import type { WatchContext } from '@/api/watch'
9+
10+
export interface ListOptions {
11+
resource: ListRequest
12+
runtime: Runtime
13+
context?: WatchContext
14+
selectors?: string[]
15+
skip?: boolean
16+
}
17+
18+
export function useResourceList<TSpec = unknown, TStatus = unknown>(
19+
opts: MaybeRefOrGetter<ListOptions>,
20+
) {
21+
const data = ref<Resource<TSpec, TStatus>[]>()
22+
const loading = ref(true)
23+
const error = ref<Error>()
24+
25+
watchEffect(async () => {
26+
const options = toValue(opts)
27+
28+
if (!options.skip) {
29+
await loadData()
30+
}
31+
})
32+
33+
return { data, loading, error, loadData }
34+
35+
async function loadData(abortController?: AbortController) {
36+
const options = toValue(opts)
37+
38+
error.value = undefined
39+
40+
const fetchOptions: fetchOption[] = []
41+
42+
if (options.runtime) {
43+
fetchOptions.push(withRuntime(options.runtime))
44+
}
45+
46+
if (options.selectors) {
47+
fetchOptions.push(withSelectors(options.selectors))
48+
}
49+
50+
if (options.context) {
51+
fetchOptions.push(withContext(options.context))
52+
}
53+
54+
if (abortController) {
55+
fetchOptions.push(withAbortController(abortController))
56+
}
57+
58+
try {
59+
data.value = await ResourceService.List<Resource<TSpec, TStatus>>(
60+
options.resource,
61+
...fetchOptions,
62+
)
63+
} catch (e) {
64+
error.value = e instanceof Error ? e : new Error(JSON.stringify(e))
65+
} finally {
66+
loading.value = true
67+
}
68+
69+
return data.value
70+
}
71+
}

frontend/src/components/common/Watch/useWatch.ts renamed to frontend/src/methods/useResourceWatch.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ interface WatchMulti<TSpec, TStatus> extends WatchBase {
2626
data: Ref<Resource<TSpec, TStatus>[]>
2727
}
2828

29-
export function useWatch<TSpec = unknown, TStatus = unknown>(
29+
export function useResourceWatch<TSpec = unknown, TStatus = unknown>(
3030
opts: MaybeRefOrGetter<WatchOptionsSingle>,
3131
): WatchSingle<TSpec, TStatus>
3232

33-
export function useWatch<TSpec = unknown, TStatus = unknown>(
33+
export function useResourceWatch<TSpec = unknown, TStatus = unknown>(
3434
opts: MaybeRefOrGetter<WatchOptionsMulti>,
3535
): WatchMulti<TSpec, TStatus>
3636

37-
export function useWatch<TSpec = unknown, TStatus = unknown>(
37+
export function useResourceWatch<TSpec = unknown, TStatus = unknown>(
3838
opts: MaybeRefOrGetter<WatchJoinOptions[]>,
3939
): WatchMulti<TSpec, TStatus>
4040

41-
export function useWatch<TSpec = unknown, TStatus = unknown>(
41+
export function useResourceWatch<TSpec = unknown, TStatus = unknown>(
4242
opts: MaybeRefOrGetter<WatchOptions | WatchJoinOptions[]>,
4343
): WatchSingle<TSpec, TStatus> | WatchMulti<TSpec, TStatus>
4444

45-
export function useWatch<TSpec, TStatus>(
45+
export function useResourceWatch<TSpec, TStatus>(
4646
opts: MaybeRefOrGetter<WatchOptions | WatchJoinOptions[]>,
4747
) {
4848
if (isWatchJoinOptions(opts)) return useWatchJoin<TSpec, TStatus>(opts)

frontend/src/views/cluster/ClusterMachines/ClusterMachines.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
MachineSetType,
1818
} from '@/api/resources'
1919
import { itemID } from '@/api/watch'
20-
import { useWatch } from '@/components/common/Watch/useWatch'
2120
import Watch from '@/components/common/Watch/Watch.vue'
2221
import { sortMachineSetIds } from '@/methods/machineset'
22+
import { useResourceWatch } from '@/methods/useResourceWatch'
2323
2424
import MachineSet from './MachineSet.vue'
2525
@@ -28,7 +28,7 @@ const { clusterID } = defineProps<{
2828
isSubgrid?: boolean
2929
}>()
3030
31-
const { data: machineSets } = useWatch<MachineSetSpec>(() => ({
31+
const { data: machineSets } = useResourceWatch<MachineSetSpec>(() => ({
3232
resource: {
3333
type: MachineSetType,
3434
namespace: DefaultNamespace,
@@ -37,7 +37,7 @@ const { data: machineSets } = useWatch<MachineSetSpec>(() => ({
3737
selectors: [`${LabelCluster}=${clusterID}`],
3838
}))
3939
40-
const { data: clusterDiagnostics } = useWatch<ClusterDiagnosticsSpec>(() => ({
40+
const { data: clusterDiagnostics } = useResourceWatch<ClusterDiagnosticsSpec>(() => ({
4141
runtime: Runtime.Omni,
4242
resource: {
4343
namespace: DefaultNamespace,

frontend/src/views/cluster/ClusterMachines/MachineSet.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ import TButton from '@/components/common/Button/TButton.vue'
3838
import TIcon from '@/components/common/Icon/TIcon.vue'
3939
import TSpinner from '@/components/common/Spinner/TSpinner.vue'
4040
import TInput from '@/components/common/TInput/TInput.vue'
41-
import { useWatch } from '@/components/common/Watch/useWatch'
4241
import { setupClusterPermissions } from '@/methods/auth'
4342
import {
4443
controlPlaneMachineSetId,
4544
defaultWorkersMachineSetId,
4645
machineSetTitle,
4746
scaleMachineSet,
4847
} from '@/methods/machineset'
48+
import { useResourceWatch } from '@/methods/useResourceWatch'
4949
import { showError } from '@/notification'
5050
5151
import ClusterMachine from './ClusterMachine.vue'
@@ -95,7 +95,7 @@ const hiddenMachinesCount = computed(() => {
9595
return Math.max(0, (machineSet.value.spec.machines?.total || 0) - showMachinesCount.value)
9696
})
9797
98-
const { data: machines } = useWatch<ClusterMachineStatusSpec>(() => ({
98+
const { data: machines } = useResourceWatch<ClusterMachineStatusSpec>(() => ({
9999
resource: {
100100
namespace: DefaultNamespace,
101101
type: ClusterMachineStatusType,
@@ -108,7 +108,7 @@ const { data: machines } = useWatch<ClusterMachineStatusSpec>(() => ({
108108
limit: showMachinesCount.value,
109109
}))
110110
111-
const { data: requests } = useWatch<ClusterMachineRequestStatusSpec>(() => ({
111+
const { data: requests } = useResourceWatch<ClusterMachineRequestStatusSpec>(() => ({
112112
skip: !machineSet.value.spec.machine_allocation,
113113
resource: {
114114
namespace: DefaultNamespace,

frontend/src/views/cluster/ExposedService/ExposedServiceSideBar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import IconButton from '@/components/common/Button/IconButton.vue'
1717
import TIcon from '@/components/common/Icon/TIcon.vue'
1818
import TMenuItem from '@/components/common/MenuItem/TMenuItem.vue'
1919
import Tooltip from '@/components/common/Tooltip/Tooltip.vue'
20-
import { useWatch } from '@/components/common/Watch/useWatch'
20+
import { useResourceWatch } from '@/methods/useResourceWatch'
2121
2222
const route = useRoute()
2323
24-
const { data: exposedServices } = useWatch<ExposedServiceSpec>(() => ({
24+
const { data: exposedServices } = useResourceWatch<ExposedServiceSpec>(() => ({
2525
runtime: Runtime.Omni,
2626
resource: {
2727
namespace: DefaultNamespace,

0 commit comments

Comments
 (0)