Skip to content

Commit 0e74309

Browse files
authored
Reorganized “search anywhere” files in entities to follow project conventions (#7402)
1 parent 98b0474 commit 0e74309

21 files changed

+190
-86
lines changed

frontend/app/src/entities/ipam/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export const IP_ADDRESS_AVAILABLE_KIND = "InternalIPRangeAvailable" as const;
88
export const IP_PREFIX_GENERIC = "BuiltinIPPrefix";
99
export const IP_PREFIX_AVAILABLE_KIND = "InternalIPPrefixAvailable";
1010

11-
export const POOLS_PEER = [IP_ADDRESS_GENERIC, IP_PREFIX_GENERIC];
12-
1311
export const TREE_ROOT_ID = "root" as const;
1412

1513
export const IPAM_QSP = {

frontend/app/src/entities/nodes/api/search.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { gql } from "@apollo/client";
2+
3+
import type { SearchQuery } from "@/shared/api/graphql/generated/graphql";
4+
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
5+
import type { ContextParams } from "@/shared/api/types";
6+
7+
export const SEARCH = gql`
8+
query Search($search: String!) {
9+
InfrahubSearchAnywhere(q: $search, limit: 4, partial_match: true) {
10+
count
11+
edges {
12+
node {
13+
id
14+
kind
15+
}
16+
}
17+
}
18+
}
19+
`;
20+
21+
export interface SearchAnywhereFromApiParams extends ContextParams {
22+
search: string;
23+
}
24+
25+
export function searchAnywhereFromApi({ search, branchName, atDate }: SearchAnywhereFromApiParams) {
26+
return graphqlClient.query<SearchQuery>({
27+
query: SEARCH,
28+
variables: { search },
29+
context: {
30+
branch: branchName,
31+
date: atDate,
32+
},
33+
});
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { SearchAnywhereParams } from "@/entities/search-anywhere/domain/search-anywhere";
2+
import type { SearchDocsParams } from "@/entities/search-anywhere/domain/search-docs";
3+
4+
export const searchAnywhereQueryKeys = {
5+
objects: ({ branchName, search, atDate }: SearchAnywhereParams) => [
6+
branchName,
7+
atDate,
8+
"search-objects",
9+
search,
10+
],
11+
docs: ({ query, limit }: SearchDocsParams) => ["search-docs", query, limit],
12+
} as const;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { queryOptions, useQuery } from "@tanstack/react-query";
2+
import { useAtomValue } from "jotai";
3+
4+
import type { ContextParams, QueryConfig } from "@/shared/api/types";
5+
import { datetimeAtom } from "@/shared/stores/time.atom";
6+
7+
import { useCurrentBranch } from "@/entities/branches/ui/branches-provider";
8+
import {
9+
type SearchAnywhereParams,
10+
searchAnywhere,
11+
} from "@/entities/search-anywhere/domain/search-anywhere";
12+
import { searchAnywhereQueryKeys } from "@/entities/search-anywhere/domain/search-anywhere.query-keys";
13+
14+
export function searchAnywhereQueryOptions(params: SearchAnywhereParams) {
15+
return queryOptions({
16+
queryKey: searchAnywhereQueryKeys.objects(params),
17+
queryFn: () => searchAnywhere(params),
18+
});
19+
}
20+
21+
export function useGetSearchAnywhere(
22+
params: Omit<SearchAnywhereParams, keyof ContextParams>,
23+
config?: QueryConfig<typeof searchAnywhereQueryOptions>
24+
) {
25+
const { currentBranch } = useCurrentBranch();
26+
const timeMachineDate = useAtomValue(datetimeAtom);
27+
28+
return useQuery({
29+
...searchAnywhereQueryOptions({
30+
branchName: currentBranch.name,
31+
atDate: timeMachineDate,
32+
...params,
33+
}),
34+
...config,
35+
});
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
type SearchAnywhereFromApiParams,
3+
searchAnywhereFromApi,
4+
} from "@/entities/search-anywhere/api/search";
5+
6+
export type SearchAnywhereParams = SearchAnywhereFromApiParams;
7+
8+
export type ObjectResult = { id: string; kind: string };
9+
10+
export type SearchAnywhere = (params: SearchAnywhereParams) => Promise<{
11+
count: number;
12+
matchingObjects: Array<ObjectResult>;
13+
}>;
14+
15+
export const searchAnywhere: SearchAnywhere = async (params) => {
16+
const { data, errors } = await searchAnywhereFromApi(params);
17+
18+
if (errors) {
19+
throw new Error(errors.map((e) => e.message).join("; "));
20+
}
21+
22+
const { InfrahubSearchAnywhere } = data;
23+
24+
return {
25+
count: InfrahubSearchAnywhere.count,
26+
matchingObjects: InfrahubSearchAnywhere.edges?.map(({ node }) => node) ?? [],
27+
};
28+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { queryOptions, useQuery } from "@tanstack/react-query";
2+
3+
import type { QueryConfig } from "@/shared/api/types";
4+
5+
import { searchAnywhereQueryKeys } from "@/entities/search-anywhere/domain/search-anywhere.query-keys";
6+
import { type SearchDocsParams, searchDocs } from "@/entities/search-anywhere/domain/search-docs";
7+
8+
export function searchDocsQueryOptions({ query, limit = 3 }: SearchDocsParams) {
9+
return queryOptions({
10+
queryKey: searchAnywhereQueryKeys.docs({ query, limit }),
11+
queryFn: () => searchDocs({ query, limit }),
12+
});
13+
}
14+
15+
export function useGetSearchDocs(
16+
params: SearchDocsParams,
17+
config?: QueryConfig<typeof searchDocsQueryOptions>
18+
) {
19+
return useQuery({ ...searchDocsQueryOptions(params), ...config });
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CONFIG } from "@/config/config";
2+
3+
import { fetchUrl } from "@/shared/api/rest/fetch";
4+
5+
export interface SearchDocsParams {
6+
query: string;
7+
limit?: number;
8+
}
9+
10+
export interface SearchDocResult {
11+
title: string;
12+
url: string;
13+
breadcrumb: string[];
14+
}
15+
16+
export type SearchDocs = (params: SearchDocsParams) => Promise<Array<SearchDocResult>>;
17+
18+
export const searchDocs: SearchDocs = async ({ query, limit }) => {
19+
return fetchUrl(CONFIG.SEARCH_URL(query, limit));
20+
};

frontend/app/src/shared/components/search/search-actions.tsx renamed to frontend/app/src/entities/search-anywhere/ui/search-actions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { useId, useMemo } from "react";
66
import { constructPath } from "@/shared/api/rest/fetch";
77
import type { MenuItem } from "@/shared/components/layout/menu-navigation/types";
88
import { useMenu } from "@/shared/components/menu/domain/get-menu.query";
9-
import { SearchAnywhereGroup } from "@/shared/components/search/search-anywhere-group";
10-
import { SearchAnywhereItem } from "@/shared/components/search/search-anywhere-item";
119
import { Badge } from "@/shared/components/ui/badge";
1210

1311
import { genericSchemasAtom, nodeSchemasAtom } from "@/entities/schema/stores/schema.atom";
1412
import type { ModelSchema } from "@/entities/schema/types";
13+
import { SearchAnywhereGroup } from "@/entities/search-anywhere/ui/search-anywhere-group";
14+
import { SearchAnywhereItem } from "@/entities/search-anywhere/ui/search-anywhere-item";
1515

1616
export const SearchActions = () => {
1717
const query = useCommandState((state) => state.search);

0 commit comments

Comments
 (0)