Skip to content

refactor: [M3-9384] - Move Quotas queries #12221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tech Stories
---

Move quotas related queries and dependencies to shared `queries` package ([#12221](https://github.com/linode/manager/pull/12221))
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const queryMocks = vi.hoisted(() => ({
useQuotasQuery: vi.fn().mockReturnValue({}),
}));

vi.mock('src/queries/quotas/quotas', () => {
const actual = vi.importActual('src/queries/quotas/quotas');
vi.mock('@linode/queries', async () => {
const actual = await vi.importActual('@linode/queries');
return {
...actual,
quotaQueries: queryMocks.quotaQueries,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { quotaQueries, useQuotasQuery } from '@linode/queries';
import { Dialog, ErrorState } from '@linode/ui';
import { useQueries } from '@tanstack/react-query';
import * as React from 'react';
Expand All @@ -12,8 +13,6 @@ import { TableRow } from 'src/components/TableRow/TableRow';
import { TableRowEmpty } from 'src/components/TableRowEmpty/TableRowEmpty';
import { TableRowLoading } from 'src/components/TableRowLoading/TableRowLoading';
import { usePagination } from 'src/hooks/usePagination';
import { useQuotasQuery } from 'src/queries/quotas/quotas';
import { quotaQueries } from 'src/queries/quotas/quotas';

import { QuotasIncreaseForm } from './QuotasIncreaseForm';
import { QuotasTableRow } from './QuotasTableRow';
Expand Down
5 changes: 5 additions & 0 deletions packages/queries/.changeset/pr-12221-added-1747257990334.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/queries": Added
---

`quotas/` directory and migrated relevant query keys and hook ([#12221](https://github.com/linode/manager/pull/12221))
1 change: 1 addition & 0 deletions packages/queries/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './networking';
export * from './nodebalancers';
export * from './placementGroups';
export * from './profile';
export * from './quotas';
export * from './regions';
export * from './stackscripts';
export * from './support';
Expand Down
3 changes: 3 additions & 0 deletions packages/queries/src/quotas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './keys';
export * from './quotas';
export * from './requests';
30 changes: 30 additions & 0 deletions packages/queries/src/quotas/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getQuota, getQuotas, getQuotaUsage } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';

import { getAllQuotas } from './requests';

import type { Filter, Params, QuotaType } from '@linode/api-v4';

export const quotaQueries = createQueryKeys('quotas', {
service: (type: QuotaType) => ({
contextQueries: {
all: (params: Params = {}, filter: Filter = {}) => ({
queryFn: () => getAllQuotas(type, params, filter),
queryKey: [params, filter],
}),
paginated: (params: Params = {}, filter: Filter = {}) => ({
queryFn: () => getQuotas(type, params, filter),
queryKey: [params, filter],
}),
quota: (id: number) => ({
queryFn: () => getQuota(type, id),
queryKey: [id],
}),
usage: (id: number) => ({
queryFn: () => getQuotaUsage(type, id),
queryKey: [id],
}),
},
queryKey: [type],
}),
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { getQuota, getQuotas, getQuotaUsage } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';
import { keepPreviousData, useQuery } from '@tanstack/react-query';

import { getAllQuotas } from './requests';
import { quotaQueries } from './keys';

import type {
APIError,
Expand All @@ -14,30 +12,6 @@ import type {
ResourcePage,
} from '@linode/api-v4';

export const quotaQueries = createQueryKeys('quotas', {
service: (type: QuotaType) => ({
contextQueries: {
all: (params: Params = {}, filter: Filter = {}) => ({
queryFn: () => getAllQuotas(type, params, filter),
queryKey: [params, filter],
}),
paginated: (params: Params = {}, filter: Filter = {}) => ({
queryFn: () => getQuotas(type, params, filter),
queryKey: [params, filter],
}),
quota: (id: number) => ({
queryFn: () => getQuota(type, id),
queryKey: [id],
}),
usage: (id: number) => ({
queryFn: () => getQuotaUsage(type, id),
queryKey: [id],
}),
},
queryKey: [type],
}),
});

export const useQuotaQuery = (service: QuotaType, id: number, enabled = true) =>
useQuery<Quota, APIError[]>({
...quotaQueries.service(service)._ctx.quota(id),
Expand All @@ -48,7 +22,7 @@ export const useQuotasQuery = (
service: QuotaType,
params: Params = {},
filter: Filter,
enabled = true
enabled = true,
) =>
useQuery<ResourcePage<Quota>, APIError[]>({
...quotaQueries.service(service)._ctx.paginated(params, filter),
Expand All @@ -60,7 +34,7 @@ export const useAllQuotasQuery = (
service: QuotaType,
params: Params = {},
filter: Filter,
enabled = true
enabled = true,
) =>
useQuery<Quota[], APIError[]>({
...quotaQueries.service(service)._ctx.all(params, filter),
Expand All @@ -70,7 +44,7 @@ export const useAllQuotasQuery = (
export const useQuotaUsageQuery = (
service: QuotaType,
id: number,
enabled = true
enabled = true,
) =>
useQuery<QuotaUsage, APIError[]>({
...quotaQueries.service(service)._ctx.usage(id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type { Filter, Params, Quota, QuotaType } from '@linode/api-v4';
export const getAllQuotas = (
service: QuotaType,
passedParams: Params = {},
passedFilter: Filter = {}
passedFilter: Filter = {},
) =>
getAll<Quota>((params, filter) =>
getQuotas(
service,
{ ...params, ...passedParams },
{ ...filter, ...passedFilter }
)
{ ...filter, ...passedFilter },
),
)().then((data) => data.data);