Skip to content

Commit

Permalink
refactor: push the logic for reading query params to react hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Jun 7, 2024
1 parent 6a2a799 commit 179023b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 113 deletions.
51 changes: 22 additions & 29 deletions src/api/query-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { toastError } from "../../components/Toast/toast";
import { getIncidentHistory } from "../services/IncidentsHistory";
import { getAllAgents } from "../services/agents";
import {
CatalogChangesSearchResponse,
GetConfigsRelatedChangesParams,
ConfigsTagList,
getConfig,
getConfigAnalysis,
getConfigChanges,
Expand All @@ -16,7 +15,6 @@ import {
getConfigLabelsList,
getConfigName,
getConfigTagsList,
getConfigsChanges,
getTopologyRelatedInsights
} from "../services/configs";
import { getHypothesisResponse } from "../services/hypothesis";
Expand Down Expand Up @@ -296,20 +294,6 @@ export const useGetHypothesisQuery = (
);
};

export function useGetConfigsChangesQuery(
props: GetConfigsRelatedChangesParams,
queryOptions: UseQueryOptions<CatalogChangesSearchResponse> = {
enabled: true,
keepPreviousData: true
}
) {
return useQuery({
queryKey: ["configs", "changes", props],
queryFn: () => getConfigsChanges(props),
...queryOptions
});
}

export function useGetConfigChangesByConfigIdQuery(
id: string,
pageIndex?: number,
Expand Down Expand Up @@ -366,23 +350,32 @@ export function useGetConfigByIdQuery(id: string) {
);
}

export function useGetConfigTagsListQuery() {
return useQuery(["configs", "tags", "list"], async () => {
const { error, data } = await getConfigTagsList();
if (error) {
throw error;
}
return data ?? [];
export function useGetConfigTagsListQuery(
options?: UseQueryOptions<ConfigsTagList[]>
) {
return useQuery({
queryKey: ["configs", "tags", "list"],
queryFn: async () => {
const { error, data } = await getConfigTagsList();
if (error) {
throw error;
}
return data ?? [];
},
...options
});
}

export function useGetConfigLabelsListQuery() {
return useQuery(["configs", "tags", "list"], async () => {
const { error, data } = await getConfigLabelsList();
if (error) {
throw error;
return useQuery({
queryKey: ["configs", "tags", "list"],
queryFn: async () => {
const { error, data } = await getConfigLabelsList();
if (error) {
throw error;
}
return data ?? [];
}
return data ?? [];
});
}

Expand Down
144 changes: 144 additions & 0 deletions src/api/query-hooks/useGetConfigsChangesQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { configChangesDefaultDateFilter } from "@flanksource-ui/components/Configs/Changes/ConfigChangesFilters/ConfigChangesDateRangeFIlter";
import { useHideDeletedConfigChanges } from "@flanksource-ui/components/Configs/Changes/ConfigsRelatedChanges/FilterBar/ConfigChangesToggledDeletedItems";
import useTimeRangeParams from "@flanksource-ui/ui/TimeRangePicker/useTimeRangeParams";
import { UseQueryOptions, useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { useParams, useSearchParams } from "react-router-dom";
import {
CatalogChangesSearchResponse,
GetConfigsRelatedChangesParams,
getConfigsChanges
} from "../services/configs";

export function useGetAllConfigsChangesQuery(
queryOptions: UseQueryOptions<CatalogChangesSearchResponse> = {
enabled: true,
keepPreviousData: true
}
) {
const hideDeletedConfigChanges = useHideDeletedConfigChanges();
const { timeRangeValue } = useTimeRangeParams(configChangesDefaultDateFilter);
const [params] = useSearchParams({
sortBy: "created_at",
sortDirection: "desc"
});
const changeType = params.get("changeType") ?? undefined;
const severity = params.get("severity") ?? undefined;
const configType = params.get("configType") ?? undefined;
const from = timeRangeValue?.from ?? undefined;
const to = timeRangeValue?.to ?? undefined;
const sortBy = params.get("sortBy") ?? undefined;
const sortDirection = params.get("sortDirection") ?? "desc";
const configTypes = params.get("configTypes") ?? "all";
const page = params.get("page") ?? "1";
const pageSize = params.get("pageSize") ?? "200";
const configId = params.get("id") ?? undefined;
const changeSummary = params.get("summary") ?? undefined;
const source = params.get("source") ?? undefined;
const createdBy = params.get("created_by") ?? undefined;
const externalCreatedBy = params.get("external_created_by") ?? undefined;

const arbitraryFilter = useMemo(() => {
const filter = new Map<string, string>();
if (configId) {
filter.set("id", configId);
}
if (changeSummary) {
filter.set("summary", changeSummary);
}
if (source) {
filter.set("source", source);
}
if (createdBy) {
filter.set("created_by", createdBy);
}
if (externalCreatedBy) {
filter.set("created_by", externalCreatedBy);
}
return Object.fromEntries(filter);
}, [changeSummary, configId, createdBy, externalCreatedBy, source]);

const props = {
include_deleted_configs: !hideDeletedConfigChanges,
changeType,
severity,
from,
to,
configTypes,
configType,
sortBy,
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize,
arbitraryFilter
} satisfies GetConfigsRelatedChangesParams;

return useQuery({
queryKey: ["configs", "changes", props],
queryFn: () => getConfigsChanges(props),
...queryOptions
});
}

export function useGetConfigChangesByIDQuery(
queryOptions: UseQueryOptions<CatalogChangesSearchResponse> = {
enabled: true,
keepPreviousData: true
}
) {
const { id } = useParams();
const hideDeletedConfigChanges = useHideDeletedConfigChanges();
const { timeRangeValue } = useTimeRangeParams(configChangesDefaultDateFilter);
const [params] = useSearchParams({
downstream: "true",
upstream: "false",
sortBy: "created_at",
sortDirection: "desc"
});
const change_type = params.get("changeType") ?? undefined;
const severity = params.get("severity") ?? undefined;
const from = timeRangeValue?.from ?? undefined;
const to = timeRangeValue?.to ?? undefined;
const sortBy = params.get("sortBy") ?? undefined;
const sortDirection = params.get("sortDirection") ?? "desc";
const configTypes = params.get("configTypes") ?? "all";
const page = params.get("page") ?? "1";
const pageSize = params.get("pageSize") ?? "200";
const upstream = params.get("upstream") === "true";
const downstream = params.get("downstream") === "true";
const all = upstream && downstream;

const relationshipType = useMemo(() => {
if (all) {
return "all";
}
if (upstream) {
return "upstream";
}
if (downstream) {
return "downstream";
}
return undefined;
}, [all, upstream, downstream]);

const props = {
id,
type_filter: relationshipType,
include_deleted_configs: hideDeletedConfigChanges,
changeType: change_type,
severity,
from,
to,
configTypes,
sortBy,
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize
} satisfies GetConfigsRelatedChangesParams;

return useQuery({
queryKey: ["configs", "changes", props],
queryFn: () => getConfigsChanges(props),
...queryOptions
});
}
36 changes: 5 additions & 31 deletions src/pages/config/ConfigChangesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useGetConfigsChangesQuery } from "@flanksource-ui/api/query-hooks";
import { useGetAllConfigsChangesQuery } from "@flanksource-ui/api/query-hooks/useGetConfigsChangesQuery";
import { ConfigChangeHistory } from "@flanksource-ui/components/Configs/Changes/ConfigChangeHistory";
import { configChangesDefaultDateFilter } from "@flanksource-ui/components/Configs/Changes/ConfigChangesFilters/ConfigChangesDateRangeFIlter";
import { ConfigChangeFilters } from "@flanksource-ui/components/Configs/Changes/ConfigChangesFilters/ConfigChangesFilters";
import ConfigPageTabs from "@flanksource-ui/components/Configs/ConfigPageTabs";
import { Head } from "@flanksource-ui/components/Head/Head";
Expand All @@ -13,30 +12,21 @@ import {
} from "@flanksource-ui/ui/BreadcrumbNav";
import { PaginationOptions } from "@flanksource-ui/ui/DataTable";
import { refreshButtonClickedTrigger } from "@flanksource-ui/ui/SlidingSideBar/SlidingSideBar";
import useTimeRangeParams from "@flanksource-ui/ui/TimeRangePicker/useTimeRangeParams";
import { useAtom } from "jotai";
import { useMemo } from "react";
import { useSearchParams } from "react-router-dom";

export function ConfigChangesPage() {
const { timeRangeValue } = useTimeRangeParams(configChangesDefaultDateFilter);
const [, setRefreshButtonClickedTrigger] = useAtom(
refreshButtonClickedTrigger
);
const [params, setParams] = useSearchParams({
sortBy: "created_at",
sortDirection: "desc"
});
const configTypes = params.get("configTypes") ?? undefined;
const configType = params.get("configType") ?? undefined;
const changeType = params.get("changeType") ?? undefined;
const severity = params.get("severity") ?? undefined;
const from = timeRangeValue?.from ?? undefined;
const to = timeRangeValue?.to ?? undefined;

const page = params.get("page") ?? "1";
const pageSize = params.get("pageSize") ?? "200";
const sortBy = params.get("sortBy") ?? undefined;
const sortDirection = params.get("sortDirection") === "asc" ? "asc" : "desc";
const configId = params.get("id") ?? undefined;
const changeSummary = params.get("summary") ?? undefined;
const source = params.get("source") ?? undefined;
Expand Down Expand Up @@ -64,25 +54,9 @@ export function ConfigChangesPage() {
}, [changeSummary, configId, createdBy, externalCreatedBy, source]);

const { data, isLoading, error, isRefetching, refetch } =
useGetConfigsChangesQuery(
{
include_deleted_configs: false,
changeType,
severity,
from,
to,
configTypes,
configType,
sortBy,
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize,
arbitraryFilter
},
{
keepPreviousData: true
}
);
useGetAllConfigsChangesQuery({
keepPreviousData: true
});

const changes = (data?.changes ?? []).map((changes) => ({
...changes,
Expand Down
58 changes: 5 additions & 53 deletions src/pages/config/details/ConfigDetailsChangesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,76 +1,28 @@
import { useGetConfigsChangesQuery } from "@flanksource-ui/api/query-hooks";
import { useGetConfigChangesByIDQuery } from "@flanksource-ui/api/query-hooks/useGetConfigsChangesQuery";
import { ConfigChangeHistory } from "@flanksource-ui/components/Configs/Changes/ConfigChangeHistory";
import { configChangesDefaultDateFilter } from "@flanksource-ui/components/Configs/Changes/ConfigChangesFilters/ConfigChangesDateRangeFIlter";
import { areDeletedConfigChangesHidden } from "@flanksource-ui/components/Configs/Changes/ConfigsRelatedChanges/FilterBar/ConfigChangesToggledDeletedItems";
import { ConfigRelatedChangesFilters } from "@flanksource-ui/components/Configs/Changes/ConfigsRelatedChanges/FilterBar/ConfigRelatedChangesFilters";
import { ConfigDetailsTabs } from "@flanksource-ui/components/Configs/ConfigDetailsTabs";
import { InfoMessage } from "@flanksource-ui/components/InfoMessage";
import { PaginationOptions } from "@flanksource-ui/ui/DataTable";
import useTimeRangeParams from "@flanksource-ui/ui/TimeRangePicker/useTimeRangeParams";
import { useAtom } from "jotai";
import { useMemo } from "react";
import { useParams, useSearchParams } from "react-router-dom";

export function ConfigDetailsChangesPage() {
const { id } = useParams();
const [hideDeletedConfigChanges] = useAtom(areDeletedConfigChangesHidden);
const { timeRangeValue } = useTimeRangeParams(configChangesDefaultDateFilter);
const [params, setParams] = useSearchParams({
downstream: "true",
upstream: "false",
sortBy: "created_at",
sortDirection: "desc"
});
const change_type = params.get("changeType") ?? undefined;
const severity = params.get("severity") ?? undefined;
const from = timeRangeValue?.from ?? undefined;
const to = timeRangeValue?.to ?? undefined;
const sortBy = params.get("sortBy") ?? undefined;
const sortDirection = params.get("sortDirection") ?? "desc";
const configTypes = params.get("configTypes") ?? "all";
const page = params.get("page") ?? "1";
const pageSize = params.get("pageSize") ?? "200";

const upstream = params.get("upstream") === "true";
const downstream = params.get("downstream") === "true";

const all = upstream && downstream;

const hideConfigColumn = !downstream && !upstream;

const relationshipType = useMemo(() => {
if (all) {
return "all";
}
if (upstream) {
return "upstream";
}
if (downstream) {
return "downstream";
}
return undefined;
}, [all, upstream, downstream]);

const { data, isLoading, error, refetch } = useGetConfigsChangesQuery(
{
id: id!,
type_filter: relationshipType,
include_deleted_configs: hideDeletedConfigChanges !== "yes",
changeType: change_type,
severity,
from,
to,
configTypes,
sortBy,
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize
},
{
keepPreviousData: true,
enabled: !!id
}
);
const { data, isLoading, error, refetch } = useGetConfigChangesByIDQuery({
keepPreviousData: true,
enabled: !!id
});

const changes = data?.changes ?? [];

Expand Down

0 comments on commit 179023b

Please sign in to comment.