Skip to content

Commit

Permalink
fix solutions description tagging (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Megha-Dev-19 authored Dec 7, 2023
1 parent 7ba0b71 commit bc6dcf8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
81 changes: 53 additions & 28 deletions src/devhub/entity/post/PostEditor.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
initState({
seekingFunding: props.seekingFunding ?? false,
author_id: context.accountId,
// Should be a list of objects with field "name".
labels,
// Should be a list of labels as strings.
// Both of the label structures should be modified together.
labelStrings,
postType,
name: props.name ?? "",
description:
(props.postType === "Solution"
? cleanDescription(props.description)
: props.description) ?? "",
amount: props.amount ?? "0",
token: props.token ?? "USDT",
supervisor: props.supervisor ?? "neardevdao.near",
githubLink: props.githubLink ?? "",
warning: "",
draftStateApplied: false,
mentionInput: "", // text next to @ tag
mentionsArray: [], // all the mentions in the description
});

/* INCLUDE: "core/lib/autocomplete" */
const autocompleteEnabled = true;

Expand All @@ -10,23 +34,46 @@ const AutoComplete = styled.div`
`;

function textareaInputHandler(value) {
const showAccountAutocomplete = /@[\w][^\s]*$/.test(value);
const words = value.split(/\s+/);
const allMentiones = words
.filter((word) => word.startsWith("@"))
.map((mention) => mention.slice(1));
const newMentiones = allMentiones.filter(
(item) => !state.mentionsArray.includes(item)
);

State.update((lastKnownState) => ({
...lastKnownState,
text: value,
showAccountAutocomplete,
showAccountAutocomplete: newMentiones?.length > 0,
mentionsArray: allMentiones,
mentionInput: newMentiones?.[0] ?? "",
}));
}

function autoCompleteAccountId(id) {
let description = state.description.replace(/[\s]{0,1}@[^\s]*$/, "");
description = `${description} @${id}`.trim() + " ";
// to make sure we update the @ at correct index
let currentIndex = 0;
const updatedDescription = state.description.replace(
/(?:^|\s)(@[^\s]*)/g,
(match) => {
if (currentIndex === state.mentionsArray.indexOf(state.mentionInput)) {
currentIndex++;
return ` @${id}`;
} else {
currentIndex++;
return match;
}
}
);
State.update((lastKnownState) => ({
...lastKnownState,
description,
handler: "autocompleteSelected",
description: updatedDescription,
showAccountAutocomplete: false,
}));
}

/* END_INCLUDE: "core/lib/autocomplete" */

const postType = props.postType ?? "Sponsorship";
Expand All @@ -49,28 +96,6 @@ const cleanDescription = (description) => {
);
};

initState({
seekingFunding: props.seekingFunding ?? false,
author_id: context.accountId,
// Should be a list of objects with field "name".
labels,
// Should be a list of labels as strings.
// Both of the label structures should be modified together.
labelStrings,
postType,
name: props.name ?? "",
description:
(props.postType === "Solution"
? cleanDescription(props.description)
: props.description) ?? "",
amount: props.amount ?? "0",
token: props.token ?? "USDT",
supervisor: props.supervisor ?? "neardevdao.near",
githubLink: props.githubLink ?? "",
warning: "",
draftStateApplied: false,
});

if (!state.draftStateApplied && props.draftState) {
State.update({ ...props.draftState, draftStateApplied: true });
}
Expand Down Expand Up @@ -388,7 +413,7 @@ const callDescriptionDiv = () => {
<Widget
src="${REPL_DEVHUB}/widget/devhub.components.molecule.AccountAutocomplete"
props={{
term: state.text.split("@").pop(),
term: state.mentionInput,
onSelect: autoCompleteAccountId,
onClose: () => State.update({ showAccountAutocomplete: false }),
}}
Expand Down
4 changes: 2 additions & 2 deletions src/devhub/page/create.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* INCLUDE: "core/lib/autocomplete" */

State.init({
seekingFunding: false,
author_id: context.accountId,
Expand All @@ -21,6 +19,8 @@ State.init({
mentionsArray: [], // all the mentions in the description
});

/* INCLUDE: "core/lib/autocomplete" */

const autocompleteEnabled = true;

const AutoComplete = styled.div`
Expand Down

0 comments on commit bc6dcf8

Please sign in to comment.