Skip to content

Commit

Permalink
configure embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Feb 3, 2024
1 parent 00747f1 commit ee7d75d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 26 deletions.
29 changes: 15 additions & 14 deletions apps/buildbox/widget/page/feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ return (
<Feed
index={[
{
action: "hashtag",
key: "abstraction",
options: {
limit: 10,
order: "desc",
},
cacheOptions: {
ignoreCache: true,
},
required: true
},
{
action: "hashtag",
key: "hack",
action: "post",
key: "main",
options: {
limit: 10,
order: "desc",
accountId: ["surgecode.near"]
},
cacheOptions: {
ignoreCache: true,
},
required: true
},
// {
// action: "hashtag",
// key: "hack",
// options: {
// limit: 10,
// order: "desc",
// },
// cacheOptions: {
// ignoreCache: true,
// },
// required: true
// },
]}
Item={(p) => (
<Widget
Expand Down
125 changes: 113 additions & 12 deletions apps/buildbox/widget/post/embed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,39 @@ const EmbedMap = new Map([
"mob.near/widget/MainPage.N.Post.Embed",
"mob.near/widget/MainPage.N.Post.Embed",
],
[
"**/buildbox/project/**",
"buildbox.near/widget/project.embed",
]
]);


const href = props.href;

const parseUrl = (url) => {
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}`;
}
Expand All @@ -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 <a href={href}>{props.children}</a>;
}
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;
Expand All @@ -55,6 +123,39 @@ const Wrapper = styled.div`
margin-top: 12px;
`;

if (!parsed || !EmbedMap.has(parsed.widgetSrc)) {
return (
<Wrapper>
<div
className="d-flex justify-content-center align-items-center"
style={{ height: "200px" }}
>
<div className="text-center">
<p>You do not have a plugin installed to render this embedding.</p>
<Link
to={`/embeds.near/widget/Plugin.Index?type=embed&widgetSrc=${parsed.widgetSrc}`}
className="btn btn-primary mb-3"
>
<i className="bi bi-plug" /> Install one from the marketplace
&#8594;
</Link>
<div>
<span>
{`or `}
<a href={href} target="_blank" rel="noopener noreferrer">
click here
</a>
{` to view`}
</span>
</div>
</div>
</div>
</Wrapper>
);
}

const widgetSrc = EmbedMap.get(parsed.widgetSrc);

return (
<Wrapper>
<Widget loading="" src={widgetSrc} props={parsed.props} />
Expand Down
4 changes: 4 additions & 0 deletions apps/buildbox/widget/project/embed.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { href } = props;

const data = Social.get(href + "/**", "final");
return <p>{JSON.stringify(data)}</p>

0 comments on commit ee7d75d

Please sign in to comment.