one
Version:
One is a React framework for web, iOS, and Android apps.
270 lines (268 loc) • 9.25 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Head_ios_exports = {};
__export(Head_ios_exports, { Head: () => Head });
module.exports = __toCommonJS(Head_ios_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_core = require("@react-navigation/core");
var import_react = __toESM(require("react"), 1);
var import_hooks = require("../hooks.native.js");
var import_HeadModule = require("./HeadModule.native.js");
var import_url = require("./url.native.js");
function urlToId(url) {
return url.replace(/[^a-zA-Z0-9]/g, "-");
}
function getLastSegment(path) {
var _path_split_pop;
var lastSegment = (_path_split_pop = path.split("/").pop()) !== null && _path_split_pop !== void 0 ? _path_split_pop : "";
return lastSegment.replace(/\.[^/.]+$/, "").split("?")[0];
}
function useAddressableLink() {
var pathname = (0, import_hooks.useUnstableGlobalHref)();
var params = (0, import_hooks.useParams)();
var url = (0, import_url.getStaticUrlFromOneRouter)(pathname);
return {
url,
pathname,
params
};
}
function useMetaChildren(children) {
return import_react.default.useMemo(function() {
var renderableChildren = [];
var metaChildren = [];
import_react.default.Children.forEach(children, function(child) {
if (!/* @__PURE__ */ import_react.default.isValidElement(child)) {
return;
}
if (typeof child.type === "string") {
metaChildren.push(child);
} else {
renderableChildren.push(child);
}
});
return {
children: renderableChildren,
metaChildren
};
}, [children]);
}
function serializedMetaChildren(meta) {
var validMeta = meta.filter(function(child) {
return child.type === "meta" || child.type === "title";
});
return validMeta.map(function(child) {
if (child.type === "title") {
return {
type: "title",
props: { children: child.props && typeof child.props === "object" && "children" in child.props && typeof child.props.children === "string" ? child.props.children : void 0 }
};
}
return {
type: "meta",
props: {
property: child.props && typeof child.props === "object" && "property" in child.props && typeof child.props.property === "string" ? child.props.property : void 0,
content: child.props && typeof child.props === "object" && "content" in child.props && typeof child.props.content === "string" ? child.props.content : void 0
}
};
});
}
function useActivityFromMetaChildren(meta) {
var { url: href, pathname } = useAddressableLink();
var previousMeta = import_react.default.useRef([]);
var cachedActivity = import_react.default.useRef({});
var sortedMeta = import_react.default.useMemo(function() {
return serializedMetaChildren(meta);
}, [meta]);
var url = import_react.default.useMemo(function() {
var urlMeta = sortedMeta.find(function(child) {
return child.type === "meta" && child.props.property === "og:url";
});
if (urlMeta) {
var _urlMeta_props_content;
if ((_urlMeta_props_content = urlMeta.props.content) === null || _urlMeta_props_content === void 0 ? void 0 : _urlMeta_props_content.startsWith("/")) {
return (0, import_url.getStaticUrlFromOneRouter)(urlMeta.props.content);
}
return urlMeta.props.content;
}
return href;
}, [sortedMeta, href]);
var title = import_react.default.useMemo(function() {
var titleTag = sortedMeta.find(function(child) {
return child.type === "title";
});
if (titleTag) {
var _titleTag_props_children;
return (_titleTag_props_children = titleTag.props.children) !== null && _titleTag_props_children !== void 0 ? _titleTag_props_children : "";
}
var titleMeta = sortedMeta.find(function(child) {
return child.type === "meta" && child.props.property === "og:title";
});
if (titleMeta) {
var _titleMeta_props_content;
return (_titleMeta_props_content = titleMeta.props.content) !== null && _titleMeta_props_content !== void 0 ? _titleMeta_props_content : "";
}
return getLastSegment(pathname);
}, [sortedMeta, pathname]);
var activity = import_react.default.useMemo(function() {
if (!!previousMeta.current && !!cachedActivity.current && deepObjectCompare(previousMeta.current, sortedMeta)) {
return cachedActivity.current;
}
previousMeta.current = sortedMeta;
var userActivity = {};
sortedMeta.forEach(function(child) {
if (child.type === "meta") {
var { property, content } = child.props;
switch (property) {
case "og:description":
userActivity.description = content;
break;
case "expo:handoff":
userActivity.isEligibleForHandoff = isTruthy(content);
break;
case "expo:spotlight":
userActivity.isEligibleForSearch = isTruthy(content);
break;
}
}
});
cachedActivity.current = userActivity;
return userActivity;
}, [sortedMeta]);
var parsedActivity = {
keywords: [title],
...activity,
title,
webpageURL: url,
activityType: Head.activities.INDEXED_ROUTE,
userInfo: { href }
};
return parsedActivity;
}
function isTruthy(value) {
return [true, "true"].includes(value);
}
function HeadNative(props) {
var isFocused = (0, import_core.useIsFocused)();
if (!isFocused) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(UnfocusedHead, {});
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FocusedHead, { ...props });
}
function UnfocusedHead(props) {
var { children } = useMetaChildren(props.children);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
}
function FocusedHead(props) {
var { metaChildren, children } = useMetaChildren(props.children);
var activity = useActivityFromMetaChildren(metaChildren);
useRegisterCurrentActivity(activity);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
}
var activities = /* @__PURE__ */ new Map();
function useRegisterCurrentActivity(activity) {
var activityId = urlToId((0, import_hooks.usePathname)() || "/");
var cascadingId = urlToId((0, import_hooks.useSegments)().join("-") || "-");
var cascadingActivity = import_react.default.useMemo(function() {
var cascadingActivity2 = activities.has(cascadingId) ? {
...activities.get(cascadingId),
...activity,
id: activityId
} : {
...activity,
id: activityId
};
activities.set(cascadingId, cascadingActivity2);
return cascadingActivity2;
}, [
cascadingId,
activityId,
activity
]);
var previousActivity = import_react.default.useRef(null);
import_react.default.useEffect(function() {
if (!cascadingActivity) {
return function() {};
}
if (!!previousActivity.current && deepObjectCompare(previousActivity.current, cascadingActivity)) {
return function() {};
}
previousActivity.current = cascadingActivity;
if (!cascadingActivity.id) {
throw new Error("Activity must have an ID");
}
if (cascadingActivity.isEligibleForHandoff || cascadingActivity.isEligibleForSearch) {
Head === null || Head === void 0 ? void 0 : Head.createActivity(cascadingActivity);
}
return function() {};
}, [cascadingActivity]);
import_react.default.useEffect(function() {
return function() {
if (activityId) {
import_HeadModule.HeadModule === null || import_HeadModule.HeadModule === void 0 ? void 0 : import_HeadModule.HeadModule.suspendActivity(activityId);
}
};
}, [activityId]);
}
function deepObjectCompare(a, b) {
if (typeof a !== typeof b) {
return false;
}
if (typeof a === "object") {
if (Array.isArray(a) !== Array.isArray(b)) {
return false;
}
if (Array.isArray(a)) {
if (a.length !== b.length) {
return false;
}
return a.every(function(item, index) {
return deepObjectCompare(item, b[index]);
});
}
if (a === null || b === null) {
return a === b;
}
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
return aKeys.every(function(key) {
return deepObjectCompare(a[key], b[key]);
});
}
return a === b;
}
HeadNative.Provider = import_react.default.Fragment;
function HeadShim(props) {
return null;
}
HeadShim.Provider = import_react.default.Fragment;
var Head = import_HeadModule.HeadModule ? HeadNative : HeadShim;
Object.assign(Head, import_HeadModule.HeadModule);