Skip to content

Commit

Permalink
Merge pull request #14 from NEARBuilders/temp-jiku
Browse files Browse the repository at this point in the history
Slow submit inputs fix
  • Loading branch information
elliotBraem authored Feb 28, 2024
2 parents 00df214 + afb171b commit e5a0b81
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 65 deletions.
1 change: 0 additions & 1 deletion apps/buildbox/widget/page/submit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const Root = styled.div`
display: flex;
flex-direction: column;
background-color: #0b0c14;
${"" /* background-color: #292320; */}
color: #fff;
gap: 5rem;
margin: 0 auto;
Expand Down
68 changes: 38 additions & 30 deletions apps/buildbox/widget/page/view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const { User, Button } = VM.require("buildhub.near/widget/components") || {
Button: () => <></>,
};

const { extractValidNearAddresses } = VM.require(
"buildbox.near/widget/utils.projects-sdk"
) || {
extractValidNearAddresses: () => {},
};

const data = Social.get(id + "/**", "final");

if (!id || !data) {
Expand Down Expand Up @@ -179,46 +185,48 @@ const {
learning,
} = JSON.parse(data[""]);

/* HELPER FUNCTION */
function isNearAddress(address) {
if (typeof address !== "string") {
return false;
}
// /* HELPER FUNCTION */
// function isNearAddress(address) {
// if (typeof address !== "string") {
// return false;
// }

// Allow both ".near" and ".testnet" endings
if (!address.endsWith(".near") && !address.endsWith(".testnet")) {
return false;
}
// // Allow both ".near" and ".testnet" endings
// if (!address.endsWith(".near") && !address.endsWith(".testnet")) {
// return false;
// }

const parts = address.split(".");
if (parts.length !== 2) {
return false;
}
// const parts = address.split(".");
// if (parts.length !== 2) {
// return false;
// }

if (parts[0].length < 2 || parts[0].length > 32) {
return false;
}
// if (parts[0].length < 2 || parts[0].length > 32) {
// return false;
// }

if (!/^[a-z0-9_-]+$/i.test(parts[0])) {
return false;
}
// if (!/^[a-z0-9_-]+$/i.test(parts[0])) {
// return false;
// }

return true;
}
// return true;
// }

function Team({ members }) {
if (members) {
// removing extra characters and splitting the string into an array
const arr = members.replace(/[\[\]\(\)@]/g, "").split(/[\s,]+/);
// const arr = members.replace(/[\[\]\(\)@]/g, "").split(/[\s,]+/);

// filtering out teammates that are not near addresses
const hexRegex = /^[0-9A-F\-_]+$/i;
const valid = arr.filter((teammate) => {
if (hexRegex.test(teammate)) {
return teammate;
}
return isNearAddress(teammate);
});
// // filtering out teammates that are not near addresses
// const hexRegex = /^[0-9A-F\-_]+$/i;
// const valid = arr.filter((teammate) => {
// if (hexRegex.test(teammate)) {
// return teammate;
// }
// return isNearAddress(teammate);
// });

const valid = extractValidNearAddresses(members);

const extractNearAddress = (id) => {
const parts = id.split("/");
Expand Down
46 changes: 46 additions & 0 deletions apps/buildbox/widget/utils/extractValidNearAddresses.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* HELPER FUNCTION */
function isNearAddress(address) {
if (typeof address !== "string") {
return false;
}

// Allow both ".near" and ".testnet" endings
if (!address.endsWith(".near") && !address.endsWith(".testnet")) {
return false;
}

const parts = address.split(".");
if (parts.length !== 2) {
return false;
}

if (parts[0].length < 2 || parts[0].length > 32) {
return false;
}

if (!/^[a-z0-9_-]+$/i.test(parts[0])) {
return false;
}

return true;
}

const extractValidNearAddresses = (string) => {
// removing extra characters and splitting the string into an array
const arr = string.replace(/[\[\]\(\)@]/g, "").split(/[\s,]+/);

// filtering out teammates that are not near addresses
const hexRegex = /^[0-9A-F\-_]+$/i;
const valid = arr.filter((teammate) => {
if (hexRegex.test(teammate)) {
return teammate;
}
return isNearAddress(teammate);
});

console.log("valid from validNearAddresses", valid);

return valid;
};

return { extractValidNearAddresses };
38 changes: 38 additions & 0 deletions apps/buildbox/widget/utils/fetchProjects.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const flattenObject = (obj, parentKey) => {
parentKey = parentKey ?? "";
let paths = [];

Object.keys(obj).forEach((key) => {
const currentPath = parentKey ? `${parentKey}/${key}` : key;

if (typeof obj[key] === "object") {
paths = paths.concat(flattenObject(obj[key], currentPath));
} else {
paths.push(currentPath);
}
});

console.log("from the helper function!");

return paths;
};

const fetchProjects = (app, type) => {
const keys = Social.keys(`*/${app}/${type}/*`, "final", {
return_type: "BlockHeight",
});

if (!keys) {
return "Loading...";
}

let flattenedKeys = flattenObject(keys);
flattenedKeys = flattenedKeys.filter(
(s) => !s.includes("/project/hackathon")
);

const projects = Social.get(flattenedKeys, "final");
return projects;
};

return { fetchProjects };
46 changes: 12 additions & 34 deletions apps/buildbox/widget/utils/projects-sdk.jsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
const flattenObject = (obj, parentKey) => {
parentKey = parentKey ?? "";
let paths = [];

Object.keys(obj).forEach((key) => {
const currentPath = parentKey ? `${parentKey}/${key}` : key;

if (typeof obj[key] === "object") {
paths = paths.concat(flattenObject(obj[key], currentPath));
} else {
paths.push(currentPath);
}
});

console.log("from the helper function!");

return paths;
const { extractValidNearAddresses } = VM.require(
"buildbox.near/widget/utils.extractValidNearAddresses"
) || {
validNearAddresses: () => {},
};

const fetchProjects = (app, type) => {
const keys = Social.keys(`*/${app}/${type}/*`, "final", {
return_type: "BlockHeight",
});

if (!keys) {
return "Loading...";
}

let flattenedKeys = flattenObject(keys);
flattenedKeys = flattenedKeys.filter(
(s) => !s.includes("/project/hackathon")
);

const projects = Social.get(flattenedKeys, "final");
return projects;
const { fetchProjects } = VM.require(
"buildbox.near/widget/utils.fetchProjects"
) || {
fetchProjects: () => {},
};

return { fetchProjects };
return {
fetchProjects,
extractValidNearAddresses,
};

0 comments on commit e5a0b81

Please sign in to comment.