File size: 1,073 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
import { useQuery } from "@tanstack/react-query";
import { useActiveBackend } from "#/contexts/active-backend-context";
import PluginsService, { type PluginFileContent } from "#/api/plugins-service";

/**
 * Query hook for one plugin file's content, shown in the plugin detail
 * modal's file viewer. Disabled until a file is selected. Local backend only
 * (on cloud the plugins page carries no contents, so this never fires).
 */
export const usePluginFileContent = (
  basePath: string | null,
  relativePath: string | null,
) => {
  const { backend, orgId } = useActiveBackend();

  return useQuery<PluginFileContent>({
    queryKey: [
      "plugin-file-content",
      basePath,
      relativePath,
      backend.id,
      orgId,
    ],
    queryFn: () => {
      if (!basePath || !relativePath) throw new Error("No file selected");
      return PluginsService.getPluginFileContent(basePath, relativePath);
    },
    enabled: Boolean(basePath && relativePath),
    retry: false,
    staleTime: 1000 * 60 * 10, // 10 minutes
    refetchOnWindowFocus: false,
  });
};