MCPcopy Index your code
hub / github.com/simstudioai/sim / useSelectorOptions

Function useSelectorOptions

apps/sim/hooks/selectors/use-selector-query.ts:54–147  ·  view source on GitHub ↗
(
  key: SelectorKey,
  args: SelectorHookArgs
)

Source from the content-addressed store, hash-verified

52const MAX_AUTO_DRAIN_PAGES = 50
53
54export function useSelectorOptions(
55 key: SelectorKey,
56 args: SelectorHookArgs
57): SelectorOptionsResult {
58 const definition = getSelectorDefinition(key)
59 const queryArgs: SelectorQueryArgs = {
60 key,
61 context: args.context,
62 search: args.search,
63 }
64 const isEnabled = args.enabled ?? (definition.enabled ? definition.enabled(queryArgs) : true)
65 const supportsPagination = Boolean(definition.fetchPage)
66
67 const flatQuery = useQuery<SelectorOption[]>({
68 queryKey: definition.getQueryKey(queryArgs),
69 queryFn: ({ signal }) =>
70 definition.fetchList?.({ ...queryArgs, signal }) ?? Promise.resolve([]),
71 enabled: !supportsPagination && isEnabled,
72 staleTime: definition.staleTime ?? 30_000,
73 })
74
75 const pagedQuery = useInfiniteQuery<SelectorPage>({
76 queryKey: [...definition.getQueryKey(queryArgs), 'paged'],
77 queryFn: ({ pageParam, signal }) => {
78 if (!definition.fetchPage) return Promise.resolve(EMPTY_PAGE)
79 return definition.fetchPage({
80 ...queryArgs,
81 cursor: pageParam as string | undefined,
82 signal,
83 })
84 },
85 getNextPageParam: (last) => last.nextCursor,
86 initialPageParam: undefined as string | undefined,
87 enabled: supportsPagination && isEnabled,
88 staleTime: definition.staleTime ?? 30_000,
89 })
90
91 const { hasNextPage, isFetchingNextPage, fetchNextPage, isError } = pagedQuery
92 const pageCount = pagedQuery.data?.pages.length ?? 0
93 const reachedDrainCap = pageCount >= MAX_AUTO_DRAIN_PAGES
94 useEffect(() => {
95 if (!supportsPagination) return
96 if (isError) return
97 if (reachedDrainCap) {
98 if (hasNextPage) {
99 logger.warn('Selector hit auto-drain cap; option list is truncated', {
100 key,
101 pages: pageCount,
102 })
103 }
104 return
105 }
106 if (hasNextPage && !isFetchingNextPage) {
107 void fetchNextPage()
108 }
109 }, [
110 supportsPagination,
111 hasNextPage,

Callers 4

ConnectorSelectorFieldFunction · 0.90
DependentFieldSelectorFunction · 0.90
SelectorComboboxFunction · 0.90
useSelectorDisplayNameFunction · 0.90

Calls 3

getSelectorDefinitionFunction · 0.90
resolveMethod · 0.65
warnMethod · 0.65

Tested by

no test coverage detected