Files
tnsu-approval-route/frontend/src/webparts/approvalAdmin/hooks/useSharePointSiteTitle.ts
T
2026-07-31 12:15:15 +00:00

37 lines
1.1 KiB
TypeScript

import * as React from 'react';
import { SharePointListsClient } from '../services/sharePointLists';
export interface SharePointSiteTitleState {
title: string | null;
isLoading: boolean;
}
/** Заголовок сайта SharePoint по его url — для отображения «Название — url» вместо голого url. */
export function useSharePointSiteTitle(
client: SharePointListsClient | undefined,
siteUrl: string | undefined | null
): SharePointSiteTitleState {
const [title, setTitle] = React.useState<string | null>(null);
const [isLoading, setIsLoading] = React.useState(false);
React.useEffect(() => {
if (!client || !siteUrl?.trim()) {
setTitle(null);
setIsLoading(false);
return undefined;
}
let cancelled = false;
setIsLoading(true);
client.getSiteTitle(siteUrl.trim())
.then(result => { if (!cancelled) setTitle(result); })
.catch(() => { if (!cancelled) setTitle(null); })
.finally(() => { if (!cancelled) setIsLoading(false); });
return () => { cancelled = true; };
}, [client, siteUrl]);
return { title, isLoading };
}