From 882b463c8dea5388623bc2fc5187549838936b6d Mon Sep 17 00:00:00 2001 From: Jikugodwill Date: Wed, 28 Feb 2024 13:43:20 +0100 Subject: [PATCH 1/2] Updated sdk structure and a new module for extraction of near addresses from strings --- apps/buildbox/widget/page/view.jsx | 68 +++++++++++-------- .../utils/extractValidNearAddresses.jsx | 46 +++++++++++++ apps/buildbox/widget/utils/fetchProjects.jsx | 38 +++++++++++ apps/buildbox/widget/utils/projects-sdk.jsx | 46 ++++--------- 4 files changed, 134 insertions(+), 64 deletions(-) create mode 100644 apps/buildbox/widget/utils/extractValidNearAddresses.jsx create mode 100644 apps/buildbox/widget/utils/fetchProjects.jsx diff --git a/apps/buildbox/widget/page/view.jsx b/apps/buildbox/widget/page/view.jsx index 708aa7c..9cf23b3 100644 --- a/apps/buildbox/widget/page/view.jsx +++ b/apps/buildbox/widget/page/view.jsx @@ -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) { @@ -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("/"); diff --git a/apps/buildbox/widget/utils/extractValidNearAddresses.jsx b/apps/buildbox/widget/utils/extractValidNearAddresses.jsx new file mode 100644 index 0000000..775baa6 --- /dev/null +++ b/apps/buildbox/widget/utils/extractValidNearAddresses.jsx @@ -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 }; diff --git a/apps/buildbox/widget/utils/fetchProjects.jsx b/apps/buildbox/widget/utils/fetchProjects.jsx new file mode 100644 index 0000000..d903ddc --- /dev/null +++ b/apps/buildbox/widget/utils/fetchProjects.jsx @@ -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 }; diff --git a/apps/buildbox/widget/utils/projects-sdk.jsx b/apps/buildbox/widget/utils/projects-sdk.jsx index d903ddc..c4c4d16 100644 --- a/apps/buildbox/widget/utils/projects-sdk.jsx +++ b/apps/buildbox/widget/utils/projects-sdk.jsx @@ -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, +}; From afb171bca1ecb985ddfc4597a713647e439d7061 Mon Sep 17 00:00:00 2001 From: Jikugodwill Date: Wed, 28 Feb 2024 16:20:44 +0100 Subject: [PATCH 2/2] Somehow resolved inputs in submit page - Submit page is slow #12 --- apps/buildbox/widget/page/submit.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/buildbox/widget/page/submit.jsx b/apps/buildbox/widget/page/submit.jsx index 6839aa0..9d9f9f3 100644 --- a/apps/buildbox/widget/page/submit.jsx +++ b/apps/buildbox/widget/page/submit.jsx @@ -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;