LibreChat/client/src/hooks/Files/useSharePointToken.ts
Danny Avila d786b1b419
📁 feat: Integrate SharePoint File Picker and Download Workflow (#8651)
* feat(sharepoint): integrate SharePoint file picker and download workflow
Introduces end‑to‑end SharePoint import support:
* Token exchange with Microsoft Graph and scope management (`useSharePointToken`)
* Re‑usable hooks: `useSharePointPicker`, `useSharePointDownload`,
  `useSharePointFileHandling`
* FileSearch dropdown now offers **From Local Machine** / **From SharePoint**
  sources and gracefully falls back when SharePoint is disabled
* Agent upload model, `AttachFileMenu`, and `DropdownPopup` extended for
  SharePoint files and sub‑menus
* Blurry overlay with progress indicator and `maxSelectionCount` limit during
  downloads
* Cache‑flush utility (`config/flush-cache.js`) supporting Redis & filesystem,
  with dry‑run and npm script
* Updated `SharePointIcon` (uses `currentColor`) and new i18n keys
* Bug fixes: placeholder syntax in progress message, picker event‑listener
  cleanup
* Misc style and performance optimizations

* Fix ESLint warnings

---------

Co-authored-by: Atef Bellaaj <slalom.bellaaj@external.daimlertruck.com>
2025-08-07 02:52:33 -04:00

46 lines
1.2 KiB
TypeScript

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,
};
}