@excentone/spfx-react
Version:
Contains custom ReactJs components and hooks intended to use when developing SharePoint Framework (SPFx) Web components.
35 lines (33 loc) • 1.27 kB
JavaScript
import { useComponentContext } from "../../contexts";
import { useCallback, useMemo } from "react";
/**
* `usePageUrls` returns an object containing a key-value pair of urls and a function that can be called to go to the url with the specified key.
* @param options The options to be passed to the custom hook.
* @returns
*/
export const usePageUrls = (options) => {
const { pageUrls } = useComponentContext();
const urls = useMemo(() => {
const otherUrls = options && (options.otherUrls || {});
return {
...pageUrls,
...otherUrls
};
}, [pageUrls, options]);
const goTo = useCallback((key, gotoOptions) => {
const { fallbackUrl, fallbackAction } = { ...options, ...gotoOptions };
const getUrl = urls[key];
const targetUrl = typeof getUrl === 'string' ? getUrl : getUrl();
const backupUrl = typeof fallbackUrl === 'string' ? fallbackUrl : fallbackUrl();
let url = targetUrl || backupUrl;
if (url) {
url = decodeURI(url);
location.assign(url);
return;
}
if (fallbackAction)
fallbackAction(pageUrls);
}, [urls, options]);
return { goTo };
};
//# sourceMappingURL=usePageUrls.js.map