Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Jan 23, 2024
1 parent e5d20f7 commit a15a670
Show file tree
Hide file tree
Showing 14 changed files with 1,256 additions and 8 deletions.
3 changes: 3 additions & 0 deletions apps/bos-blocks/bos.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appAccount": "devs.near"
}
99 changes: 99 additions & 0 deletions apps/bos-blocks/widget/Compose.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
if (!context.accountId) {
return "";
}

const index = props.index || {
post: JSON.stringify({
key: "main",
value: {
type: "md",
},
}),
};

const composeData = () => {
if (props.appendHashtags) {
state.content.text = props.appendHashtags(state.content.text);
}
const data = {
post: {
main: JSON.stringify(state.content),
},
index,
};

const item = {
type: "social",
path: `${context.accountId}/post/main`,
};

const notifications = state.extractMentionNotifications(
state.content.text,
item
);

if (notifications.length) {
data.index.notify = JSON.stringify(
notifications.length > 1 ? notifications : notifications[0]
);
}

const hashtags = state.extractHashtags(state.content.text);

if (hashtags.length) {
data.index.hashtag = JSON.stringify(
hashtags.map((hashtag) => ({
key: hashtag,
value: item,
}))
);
}

return data;
};

State.init({
onChange: ({ content }) => {
State.update({ content });
},
});

return (
<>
<div style={{ margin: "0 -12px" }}>
<Widget
src="mob.near/widget/MainPage.N.Common.Compose"
props={{
placeholder: "What's happening?",
onChange: state.onChange,
onHelper: ({ extractMentionNotifications, extractHashtags }) => {
State.update({ extractMentionNotifications, extractHashtags });
},
composeButton: (onCompose) => (
<CommitButton
disabled={!state.content}
force
className="btn btn-primary rounded-5"
data={composeData}
onCommit={() => {
onCompose();
}}
>
Post
</CommitButton>
),
}}
/>
</div>
{state.content && (
<Widget
src="mob.near/widget/MainPage.N.Post"
props={{
accountId: context.accountId,
content: state.content,
blockHeight: "now",
}}
/>
)}
</>
);
89 changes: 89 additions & 0 deletions apps/bos-blocks/widget/Feed.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const Feed = ({ index, typeWhitelist, Item, Layout, showCompose }) => {
Item = Item || ((props) => <div>{JSON.stringify(props)}</div>);
Layout = Layout || (({ children }) => children);

const renderItem = (a, i) => {
if (typeWhitelist && !typeWhitelist.includes(a.value.type)) {
return false;
}
return (
<div key={JSON.stringify(a)}>
<Item
accountId={a.accountId}
path={a.value.path}
blockHeight={a.blockHeight}
type={a.value.type}
/>
</div>
);
};

const composeIndex = () => {
const arr = Array.isArray(index) ? index : [index];

const grouped = arr.reduce((acc, i) => {
if (i.action !== "repost") {
if (!acc[i.action]) {
acc[i.action] = [];
}
acc[i.action].push({ key: i.key, value: { type: "md" } });
}
return acc;
}, {});

Object.keys(grouped).forEach((action) => {
if (grouped[action].length === 1) {
grouped[action] = grouped[action][0];
}
grouped[action] = JSON.stringify(grouped[action]);
});

return grouped;
};

const appendHashtags = (v) => {
const arr = Array.isArray(index) ? index : [index];
const hashtags = arr
.filter((i) => i.action === "hashtag")
.map((i) => i.key);

hashtags.forEach((hashtag) => {
if (v.toLowerCase().includes(`#${hashtag.toLowerCase()}`)) return;
else v += ` #${hashtag}`;
});

return v;
};

return (
<>
{showCompose && (
<Widget
src="devs.near/widget/Compose"
props={{ index: composeIndex(), appendHashtags }}
/>
)}
{Array.isArray(index) ? (
<Widget
src="/*__@appAccount__*//widget/PR.MergedIndexFeed"
props={{
index,
renderItem,
Layout: ({ children }) => <Layout>{children}</Layout>,
}}
/>
) : (
<Widget
src="/*__@appAccount__*//widget/PR.FilteredIndexFeed"
props={{
index,
renderItem,
Layout: ({ children }) => <Layout>{children}</Layout>,
}}
/>
)}
</>
);
};

return { Feed };
Loading

0 comments on commit a15a670

Please sign in to comment.