chenzhaoyang
2025-12-17 d3e5a4b7658ece4f845bbc0c4f95acf3fbdf8a61
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { useQuery } from "@tanstack/react-query";
 
// Extend Window interface to include DataManager properties
declare global {
  interface Window {
    DM?: {
      store?: {
        apiCall: (method: string, params?: any) => Promise<any>;
      };
      apiCall?: (method: string, params?: any) => Promise<any>;
    };
  }
}
 
interface Action {
  id: string;
  title: string;
  order: number;
  hidden?: boolean;
  dialog?: {
    type?: string;
    text?: string;
    form?: any;
    title?: string;
  };
  children?: Action[];
  disabled?: boolean;
  disabledReason?: string;
  isSeparator?: boolean;
  isTitle?: boolean;
  callback?: (selection: any, action: Action) => void;
}
 
interface UseActionsOptions {
  projectId?: string;
  enabled?: boolean;
  staleTime?: number;
  cacheTime?: number;
}
 
/**
 * Hook to fetch available actions from the DataManager API
 * Uses TanStack Query for data fetching and caching
 *
 * @param options - Configuration options for the query
 * @returns Object containing actions data, loading state, error state, and refetch function
 */
export const useActions = (options: UseActionsOptions = {}) => {
  const {
    enabled = true,
    staleTime = 5 * 60 * 1000, // 5 minutes
    cacheTime = 10 * 60 * 1000, // 10 minutes
    projectId,
  } = options;
 
  const queryKey = ["actions", projectId];
 
  const { data, isLoading, isError, error, refetch, isFetching } = useQuery({
    queryKey,
    queryFn: async () => {
      // Use the correct DataManager API pattern - window.DM is the AppStore
      const store = window?.DM?.store || window?.DM;
 
      if (!store) {
        throw new Error("DataManager store not available");
      }
 
      const response = await store.apiCall?.("actions");
 
      if (!response) {
        throw new Error("No actions found in response or response is invalid");
      }
 
      return response as Action[];
    },
    enabled,
    staleTime,
    cacheTime,
  });
 
  const actions = data ?? [];
 
  return {
    actions,
    isLoading,
    isError,
    error,
    refetch,
    isFetching,
  };
};