File size: 1,218 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useAuthContext } from '~/hooks/AuthContext';
import { useGraphTokenQuery, useGetStartupConfig } from '~/data-provider';

interface UseSharePointTokenProps {
  enabled?: boolean;
  purpose: 'Pick' | 'Download';
}

interface UseSharePointTokenReturn {
  token: any;
  isLoading: boolean;
  error: any;
  refetch: () => Promise<any>;
}

export default function useSharePointToken({
  enabled = true,
  purpose,
}: UseSharePointTokenProps): UseSharePointTokenReturn {
  const { user } = useAuthContext();
  const { data: startupConfig } = useGetStartupConfig();

  const sharePointBaseUrl = startupConfig?.sharePointBaseUrl;
  const sharePointPickerGraphScope = startupConfig?.sharePointPickerGraphScope;
  const sharePointPickerSharePointScope = startupConfig?.sharePointPickerSharePointScope;

  const isEntraIdUser = user?.provider === 'openid';
  const graphScopes =
    purpose === 'Pick' ? sharePointPickerSharePointScope : sharePointPickerGraphScope;
  const {
    data: token,
    isLoading,
    error,
    refetch,
  } = useGraphTokenQuery({
    scopes: graphScopes,
    enabled: enabled && isEntraIdUser && !!sharePointBaseUrl,
  });

  return {
    token,
    isLoading,
    error,
    refetch,
  };
}