Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discussions tab to community page #645

Merged
merged 28 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b950911
initial commit
Tguntenaar Jan 23, 2024
8de82ab
added createDiscussion and dynamic feed action
Tguntenaar Jan 26, 2024
3a7dd60
remove old test
Tguntenaar Jan 26, 2024
b6f3db7
call grant_write_permission on socialdb contract to repost
Tguntenaar Jan 26, 2024
d9846b5
removed comment
Tguntenaar Jan 26, 2024
51a1a1e
add import for getDepositAmountForWriteAccess
Tguntenaar Jan 26, 2024
6040946
get storage_balance_of user
Tguntenaar Jan 26, 2024
ae0034e
grant permission for discussion to write post
Tguntenaar Jan 31, 2024
61ca49d
follow the discussions account in the feed
Tguntenaar Feb 2, 2024
787fbb4
removed grant_write_permission
Tguntenaar Feb 5, 2024
c8ac8eb
need a better way to get the blockheight
Tguntenaar Feb 6, 2024
815c8db
added near view get with_block_height
Tguntenaar Feb 6, 2024
d4ebeda
change title
Tguntenaar Feb 7, 2024
63d75bb
updated preview with latest changes
Tguntenaar Feb 7, 2024
b98b0a4
Merge branch 'main' of github.com:near/neardevhub-widgets into featur…
Tguntenaar Feb 7, 2024
75962f1
Recreate what announcements does: TODO listen to index/repost instead…
Tguntenaar Feb 8, 2024
6af9aa1
wip
Tguntenaar Feb 8, 2024
e171f4e
wip props.transactionHashes not recognized ?
Tguntenaar Feb 10, 2024
a4520e1
pass transactionHashes on from the page
Tguntenaar Feb 10, 2024
f95363a
Ready for review @frol
Tguntenaar Feb 12, 2024
b33eb09
added force option and getBlockHeightAndRepost onCommit
Tguntenaar Feb 16, 2024
b430920
Change the profile preview where discussions is posted from
Tguntenaar Feb 17, 2024
54d6c4b
Update src/devhub/entity/community/Discussions.jsx
Tguntenaar Feb 17, 2024
1b92280
added REPL_RPC_URL
Tguntenaar Feb 17, 2024
9e98b74
Merge branch 'feature/584-discussions-front-end' of github.com:Tgunte…
Tguntenaar Feb 17, 2024
ce7be7c
removed double feed
Tguntenaar Feb 17, 2024
5156218
added space to replacements
Tguntenaar Feb 17, 2024
5bae5f0
notify mention in discussions
Tguntenaar Feb 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion replacements.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet",
"REPL_DEVS": "nearbuilders.testnet",
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet"
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet",
"REPL_RPC_URL": "https://rpc.testnet.near.org"
}
3 changes: 2 additions & 1 deletion replacements.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"REPL_MOB": "mob.near",
"REPL_EFIZ": "efiz.near",
"REPL_DEVS": "devs.near",
"REPL_SOCIAL_CONTRACT": "social.near"
"REPL_SOCIAL_CONTRACT": "social.near",
"REPL_RPC_URL": "https://rpc.mainnet.near.org"
}
3 changes: 2 additions & 1 deletion replacements.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet",
"REPL_DEVS": "nearbuilders.testnet",
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet"
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet",
"REPL_RPC_URL": "https://rpc.testnet.near.org"
}
2 changes: 1 addition & 1 deletion src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Page() {
// ?page=community
case "community": {
return (
// Considering to consolsidate this into a single widget,
// Considering to consolidate this into a single widget,
// where each level handles its own routing.
// Modularizing a page just like we do with addons
<Widget
Expand Down
13 changes: 13 additions & 0 deletions src/core/adapter/devhub-contract.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function getAllCommunitiesMetadata() {
);
}

function getSocialWithBlockHeight(data) {
return Near.view("${REPL_SOCIAL_CONTRACT}", "get", data) ?? null;
}

function getAllLabels() {
return Near.view("${REPL_DEVHUB_LEGACY}", "get_all_labels") ?? null;
}
Expand Down Expand Up @@ -131,6 +135,15 @@ function setCommunitySocialDB({ handle, data }) {
);
}

function createDiscussion({ handle, data }) {
return (
Near.call("${REPL_DEVHUB_CONTRACT}", "create_discussion", {
handle,
data,
}) ?? null
);
}

function useQuery(name, params) {
const initialState = { data: null, error: null, isLoading: true };

Expand Down
2 changes: 1 addition & 1 deletion src/devhub/components/organism/Feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ return (
<Feed
index={[
{
action: "post",
action: props.action ? props.action : "post",
key: "main",
options: {
limit: 10,
Expand Down
2 changes: 1 addition & 1 deletion src/devhub/entity/community/Announcements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ return (
src={"${REPL_DEVHUB}/widget/devhub.entity.community.Compose"}
props={{
onSubmit: (v) => setCommunitySocialDB({ handle, data: v }),
communityAccountId: `${handle}.community.${REPL_DEVHUB_CONTRACT}`,
profileAccountId: `${handle}.community.${REPL_DEVHUB_CONTRACT}`,
}}
/>
</div>
Expand Down
13 changes: 7 additions & 6 deletions src/devhub/entity/community/Compose.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const communityAccountId = props.communityAccountId;
if (!communityAccountId) {
const profileAccountId = props.profileAccountId;

if (!profileAccountId) {
return <></>;
}

Expand All @@ -11,7 +12,7 @@ State.init({
mentionsArray: [], // all the mentions in the description
});

const profile = Social.getr(`${communityAccountId}/profile`);
const profile = Social.getr(`${profileAccountId}/profile`);
const autocompleteEnabled = true;

const content = {
Expand Down Expand Up @@ -40,7 +41,7 @@ function extractMentions(text) {

function extractTagNotifications(text, item) {
return extractMentions(text || "")
.filter((accountId) => accountId !== communityAccountId)
.filter((accountId) => accountId !== profileAccountId)
.map((accountId) => ({
key: accountId,
value: {
Expand All @@ -67,7 +68,7 @@ function composeData() {

const notifications = extractTagNotifications(state.text, {
type: "social",
path: `${communityAccountId}/post/main`,
path: `${profileAccountId}/post/main`,
});

if (notifications.length) {
Expand Down Expand Up @@ -363,7 +364,7 @@ return (
src="${REPL_NEAR}/widget/v1.Posts.Post"
loading={<div className="w-100" style={{ height: "200px" }} />}
props={{
accountId: communityAccountId,
accountId: profileAccountId,
blockHeight: "now",
content,
}}
Expand Down
226 changes: 226 additions & 0 deletions src/devhub/entity/community/Discussions.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
const { handle } = props;
const { getCommunity, setCommunitySocialDB } = VM.require(
"${REPL_DEVHUB}/widget/core.adapter.devhub-contract"
);

getCommunity = getCommunity || (() => <></>);
setCommunitySocialDB = setCommunitySocialDB || (() => <></>);

const communityData = getCommunity({ handle });

const MainContent = styled.div`
padding-left: 2rem;
flex: 3;
@media screen and (max-width: 960px) {
padding-left: 0rem;
}
.post:hover {
background-color: inherit !important;
}
`;

const SidebarContainer = styled.div`
flex: 1;
`;

const Heading = styled.div`
font-size: 19px;
font-weight: 600;
`;

const SubHeading = styled.div`
font-size: 15px;
font-weight: 600;
`;

const Container = styled.div`
flex-wrap: no-wrap;
max-width: 100%;

.max-width-100 {
max-width: 100%;
}
@media screen and (max-width: 960px) {
flex-wrap: wrap;
}

.card {
border-radius: 1rem !important;
}

.display-none {
display: none;
}
`;

const Tag = styled.div`
border-top-right-radius: 50px;
border-bottom-right-radius: 50px;
border-top-left-radius: 50px;
border-bottom-left-radius: 50px;
padding-inline: 0.8rem;
padding-block: 0.3rem;
display: flex;
gap: 0.5rem;
border-width: 1px;
border-style: solid;
font-size: 14px;
color: rgba(0, 236, 151, 1);
font-weight: 800;
`;

const [sort, setSort] = useState("timedesc");

function repostOnDiscussions(blockHeight) {
Near.call([
{
contractName: "${REPL_DEVHUB_CONTRACT}",
methodName: "create_discussion",
args: {
handle,
block_height: blockHeight,
},
gas: Big(10).pow(14),
},
]);
}

async function checkHashes() {
if (props.transactionHashes) {
asyncFetch("${REPL_RPC_URL}", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "dontcare",
method: "tx",
params: [props.transactionHashes, context.accountId],
}),
}).then((transaction) => {
if (transaction !== null) {
const transaction_method_name =
transaction?.body?.result?.transaction?.actions[0].FunctionCall
.method_name;

if (transaction_method_name === "set") {
getBlockHeightAndRepost();
}

// show the latest created post to user
if (transaction_method_name === "create_discussion") {
console.log("Discussions created in the last call, show it to user.");
}
}
});
}
}

function getBlockHeightAndRepost() {
Near.asyncView("${REPL_SOCIAL_CONTRACT}", "get", {
keys: [`${context.accountId}/post/**`],
options: {
with_block_height: true,
},
})
.then((response) => {
let blockHeight = response[context.accountId][":block"];
repostOnDiscussions(blockHeight);
})
.catch((error) => {
console.log(
"DevHub Error [Discussions]: getBlockHeightAndRepost failed",
error
);
});
}

checkHashes();

return (
<div className="w-100" style={{ maxWidth: "100%" }}>
<Container className="d-flex gap-3 m-3 pl-2">
<MainContent className="max-width-100">
<div className="d-flex flex-column gap-4">
{context.accountId && (
<div className="card p-4">
<Widget
src={"${REPL_DEVHUB}/widget/devhub.entity.community.Compose"}
props={{
onSubmit: (v) => {
Social.set(v, {
force: true,
onCommit: () => {
getBlockHeightAndRepost();
},
});
},
profileAccountId: context.accountId,
}}
/>
</div>
)}
<div className="d-flex flex-wrap justify-content-between">
<Heading>Discussions</Heading>
<div className={"d-flex align-items-center gap-2"}>
<select
name="sort"
id="sort"
class="form-select"
value={sort}
onChange={(e) => {
setSort(e.target.value);
}}
>
<option selected value="timedesc">
Latest
</option>
<option value="recentcommentdesc">Last Commented</option>
</select>
</div>
</div>

<div className={"card p-4"}>
{/* TODO: the current feed is from https://near.org/near/widget/ComponentDetailsPage?src=mob.near/widget/ProfileTabs
We will replace this with our custom feed as soon as it can support reposts */}
<Widget
key="feed"
src="mob.near/widget/MainPage.N.Feed"
props={{
accounts: [
`discussions.${handle}.community.${REPL_DEVHUB_CONTRACT}`,
],
}}
/>
</div>
</div>
</MainContent>
<SidebarContainer>
<div className="d-flex flex-column gap-3">
<div className="card p-4">
<div className="mb-2">{communityData?.description}</div>
<div className="d-flex gap-2 flex-wrap">
<Tag>{communityData?.tag} </Tag>
</div>
</div>
<div className="card p-4 d-flex flex-column gap-2">
<SubHeading>Community Admins</SubHeading>
{(communityData?.admins ?? []).map((accountId) => (
<div
key={accountId}
className="d-flex"
style={{ fontWeight: 500 }}
>
<Widget
src="${REPL_DEVHUB}/widget/devhub.components.molecule.ProfileCard"
props={{ accountId }}
/>
</div>
))}
</div>
</div>
</SidebarContainer>
</Container>
</div>
);
2 changes: 1 addition & 1 deletion src/devhub/entity/post/PostEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const AutoComplete = styled.div`
if (props.transactionHashes) {
const transaction = useCache(
() =>
asyncFetch("https://rpc.mainnet.near.org", {
asyncFetch("${REPL_RPC_URL}", {
method: "POST",
headers: {
"content-type": "application/json",
Expand Down
8 changes: 8 additions & 0 deletions src/devhub/page/community/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const tabs = [
handle: community.handle,
},
},
{
title: "Discussions",
view: "${REPL_DEVHUB}/widget/devhub.entity.community.Discussions",
params: {
handle: community.handle,
transactionHashes: props.transactionHashes,
},
},
{
title: "Activity",
view: "${REPL_DEVHUB}/widget/devhub.entity.community.Activity",
Expand Down
Loading