UNPKG

@wordpress/block-editor

Version:
93 lines (91 loc) 2.79 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/link-control/use-rich-url-data.js var use_rich_url_data_exports = {}; __export(use_rich_url_data_exports, { default: () => use_rich_url_data_default }); module.exports = __toCommonJS(use_rich_url_data_exports); var import_store = require("../../store"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); function reducer(state, action) { switch (action.type) { case "RESOLVED": return { ...state, isFetching: false, richData: action.richData }; case "ERROR": return { ...state, isFetching: false, richData: null }; case "LOADING": return { ...state, isFetching: true }; default: throw new Error(`Unexpected action type ${action.type}`); } } function useRemoteUrlData(url) { const [state, dispatch] = (0, import_element.useReducer)(reducer, { richData: null, isFetching: false }); const { fetchRichUrlData } = (0, import_data.useSelect)((select) => { const { getSettings } = select(import_store.store); return { fetchRichUrlData: getSettings().__experimentalFetchRichUrlData }; }, []); (0, import_element.useEffect)(() => { if (url?.length && fetchRichUrlData && typeof AbortController !== "undefined") { dispatch({ type: "LOADING" }); const controller = new window.AbortController(); const signal = controller.signal; fetchRichUrlData(url, { signal }).then((urlData) => { dispatch({ type: "RESOLVED", richData: urlData }); }).catch(() => { if (!signal.aborted) { dispatch({ type: "ERROR" }); } }); return () => { controller.abort(); }; } }, [url]); return state; } var use_rich_url_data_default = useRemoteUrlData; //# sourceMappingURL=use-rich-url-data.js.map