From ee7d75dec70a3cb6a71a4b09a49faffb042ba567 Mon Sep 17 00:00:00 2001 From: Elliot Braem <16282460+elliotBraem@users.noreply.github.com> Date: Sat, 3 Feb 2024 10:07:08 -0700 Subject: [PATCH] configure embedding --- apps/buildbox/widget/page/feed.jsx | 29 +++--- apps/buildbox/widget/post/embed.jsx | 125 ++++++++++++++++++++++--- apps/buildbox/widget/project/embed.jsx | 4 + 3 files changed, 132 insertions(+), 26 deletions(-) create mode 100644 apps/buildbox/widget/project/embed.jsx diff --git a/apps/buildbox/widget/page/feed.jsx b/apps/buildbox/widget/page/feed.jsx index ce6a613..157592a 100644 --- a/apps/buildbox/widget/page/feed.jsx +++ b/apps/buildbox/widget/page/feed.jsx @@ -6,29 +6,30 @@ return ( ( { - if (typeof url !== "string") { +const assertString = (s) => { + if (typeof s !== "string") { return null; } +}; + +// checks for use of "**" in widgetSrc string +const containsGlob = (path) => /\*\*/.test(path); + +const findWithKey = (key, href) => { + let fragments = key.split("**").filter((f) => f != ""); + const hasFragment = (str, fragment) => str.search(fragment) != -1; + while (fragments.length > 0) { + if (hasFragment(href, fragments[0])) { + fragments.shift(); + } else { + return null; + } + } + return true; +}; + +const parseUrl = (url) => { + assertString(url); if (url.startsWith("/")) { url = `https://near.social${url}`; } @@ -29,22 +54,65 @@ const parseUrl = (url) => { } }; +const parseGlob = (path) => { + assertString(path); + const keysWithGlobs = [...EmbedMap.keys()].filter((key) => containsGlob(key)); + console.log("keysWithGlobs", keysWithGlobs) + const keysThatMatch = keysWithGlobs.filter((key) => findWithKey(key, href)); + if (keysThatMatch.length >= 1) { + try { + return keysThatMatch[0]; + } catch { + return null; + } + } + return null; +}; + const parsed = useMemo(() => { + // try parsing embed link to URL const url = parseUrl(href); - if (!url) { - return null; + if (!!url) { + return { + widgetSrc: url.pathname.substring(1), + props: Object.fromEntries([...url.searchParams.entries()]), + }; } - return { - widgetSrc: url.pathname.substring(1), - props: Object.fromEntries([...url.searchParams.entries()]), - }; + + // try parsing embed link to glob if url failed + const widgetSrc = parseGlob(href); + console.log("parsed", widgetSrc) + if (!!widgetSrc) { + return { + widgetSrc, + props: { + href, + }, + }; + } + + // neither valid url nor valid glob + return null; }, [href]); -if (!parsed || !EmbedMap.has(parsed.widgetSrc)) { - return {props.children}; -} +function filterByWidgetSrc(obj, widgetSrcValue) { + let result = []; -const widgetSrc = EmbedMap.get(parsed.widgetSrc); + function recurse(currentObj) { + if (typeof currentObj === "object" && currentObj !== null) { + if ( + currentObj.metadata && + currentObj.metadata.widgetSrc === widgetSrcValue + ) { + result.push(currentObj); + } + Object.values(currentObj).forEach((value) => recurse(value)); + } + } + + recurse(obj); + return result; +} const Wrapper = styled.div` border-radius: 1rem; @@ -55,6 +123,39 @@ const Wrapper = styled.div` margin-top: 12px; `; +if (!parsed || !EmbedMap.has(parsed.widgetSrc)) { + return ( + +
+
+

You do not have a plugin installed to render this embedding.

+ + Install one from the marketplace + → + +
+ + {`or `} + + click here + + {` to view`} + +
+
+
+
+ ); +} + +const widgetSrc = EmbedMap.get(parsed.widgetSrc); + return ( diff --git a/apps/buildbox/widget/project/embed.jsx b/apps/buildbox/widget/project/embed.jsx new file mode 100644 index 0000000..644475f --- /dev/null +++ b/apps/buildbox/widget/project/embed.jsx @@ -0,0 +1,4 @@ +const { href } = props; + +const data = Social.get(href + "/**", "final"); +return

{JSON.stringify(data)}

\ No newline at end of file