File size: 1,621 Bytes
9b906ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { SecretsService } from "#/api/secrets-service";
import { CustomSecretWithoutValue } from "#/api/secrets-service.types";
import { useActiveBackend } from "#/contexts/active-backend-context";

interface UseSearchSecretsOptions {
  nameContains?: string;
  enabled?: boolean;
}

/**
 * Hook for searching/filtering secrets.
 * Since the agent-server API doesn't support server-side filtering or pagination,
 * all filtering is done client-side.
 */
export const useSearchSecrets = (options: UseSearchSecretsOptions = {}) => {
  const { nameContains, enabled = true } = options;
  const active = useActiveBackend();

  const query = useQuery<CustomSecretWithoutValue[], Error>({
    queryKey: ["secrets", active.backend.id, active.orgId],
    queryFn: SecretsService.getSecrets,
    enabled,
    staleTime: 1000 * 60 * 5,
    gcTime: 1000 * 60 * 15,
  });

  // Client-side filtering since agent-server doesn't support search params
  const filteredSecrets = useMemo(() => {
    if (!query.data) return [];
    if (!nameContains) return query.data;
    const lowerFilter = nameContains.toLowerCase();
    return query.data.filter((secret) =>
      secret.name.toLowerCase().includes(lowerFilter),
    );
  }, [query.data, nameContains]);

  return {
    data: filteredSecrets,
    isLoading: query.isLoading,
    isError: query.isError,
    // Agent-server API doesn't support pagination
    hasNextPage: false,
    isFetchingNextPage: false,
    fetchNextPage: () => {},
    onLoadMore: () => {},
    refetch: query.refetch,
  };
};