Skip to content

Commit

Permalink
Merge pull request #17 from sekaiking/feature/mockSocialGet
Browse files Browse the repository at this point in the history
Implement mocking SocialGet
  • Loading branch information
elliotBraem authored Aug 14, 2023
2 parents 4b14f17 + 21ec262 commit 8092bed
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useInitNear,
useNear,
utils,
useCache,
} from "near-social-vm";
import React, { useCallback, useEffect, useState } from "react";
import "react-bootstrap-typeahead/css/Typeahead.bs5.css";
Expand All @@ -35,6 +36,8 @@ import { NetworkId, Widgets } from "./data/widgets";
import { useBosLoaderInitializer } from "./hooks/useBosLoaderInitializer";
import Flags from "./pages/Flags";
import ViewPage from "./pages/ViewPage";
import { useBosLoaderStore } from "./stores/bos-loader";
import { recursiveGet } from "./utils/data";

export const refreshAllowanceObj = {};
const documentationHref = "https://social.near-docs.io/";
Expand All @@ -46,6 +49,7 @@ function App(props) {
const [availableStorage, setAvailableStorage] = useState(null);
const [walletModal, setWalletModal] = useState(null);
const [widgetSrc, setWidgetSrc] = useState(null);
const bosLoaderStore = useBosLoaderStore();

const ethersProviderContext = useEthersProviderContext();
useBosLoaderInitializer();
Expand All @@ -57,6 +61,54 @@ function App(props) {

const location = window.location;

const cc = useCache();
cc.socialGet = (near, keys, recursive, blockId, options, invalidate) => {
if (!near) {
return null;
}
keys = Array.isArray(keys) ? keys : [keys];
keys = keys.map((key) => (recursive ? `${key}/**` : `${key}`));

let data = null;

if (keys.length === 1 && bosLoaderStore.dataMap) {
data = recursiveGet(bosLoaderStore.dataMap, keys[0]);
} else {
console.warn("Currently only support mocking one key of SocialGet");
}

if (data === undefined || data === null) {
const args = {
keys,
options,
};
data = cc.cachedViewCall(
near,
near.config.contractName,
"get",
args,
blockId,
invalidate
);

if (keys.length === 1) {
const parts = keys[0].split("/");
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part === "*" || part === "**") {
break;
}
data = data?.[part];
}
}
}

if (data === null) {
return null;
}
return data;
};

useEffect(() => {
initNear &&
initNear({
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useBosLoaderInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function useBosLoaderInitializer() {
setStore({
hasResolved: true,
redirectMap: data.components,
dataMap: data.data,
});
} catch (e) {
console.error(e);
Expand Down
1 change: 1 addition & 0 deletions src/stores/bos-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const useBosLoaderStore = create((set) => ({
hasResolved: false,
loaderUrl: '',
redirectMap: {},
dataMap: {},
set: (state) => set((previousState) => ({ ...previousState, ...state })),
}));
78 changes: 78 additions & 0 deletions src/utils/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// TODO: Need testing to check if it works exactly the same as the original function in SocialDB
// A recursive function to retrieve values from a given node based on keys and options
function recursiveGet(obj, key) {
if (!obj) {
return obj;
}

const RECURSIVE = "**"; // Recursive pattern
const WILD = "*"; // Wildcard pattern
const DELIMITER = "/"; // Delimiter

const parts = key.split(DELIMITER);
const firstPart = parts[0];

if (key === RECURSIVE && parts.length > 1) {
throw new Error("Recursive pattern can only be the last part of the key");
}

// if the first part of the key is not in the object
if (
firstPart !== WILD &&
firstPart !== RECURSIVE &&
Object.keys(obj).length > 0 &&
(obj[firstPart] === undefined || obj[firstPart] === null)
) {
return undefined;
}

// if it's the last part of the key

if (parts.length === 1) {
if (firstPart === RECURSIVE) {
return obj;
}
if (obj[firstPart]?.[""]) {
return obj[firstPart][""];
}
if (
typeof obj[firstPart] === "boolean" ||
typeof obj[firstPart] === "string" ||
typeof obj[firstPart] === "number"
) {
return obj[firstPart];
}
if (typeof obj === "object" && firstPart === WILD) {
const res = {};
Object.keys(obj).forEach((key) => {
if (typeof obj[key] !== "object") {
res[key] = obj[key];
}
});
return res;
}
return undefined;
}

if (firstPart === WILD) {
const res = {};
Object.keys(obj).forEach((key) => {
const subKey = parts.slice(1).join(DELIMITER);
const subtree = recursiveGet(obj[key], subKey);
if (subtree !== undefined) {
if (subKey.split(DELIMITER)[0] === WILD || subKey === RECURSIVE) {
res[key] = subtree;
} else {
res[key] = { [subKey.split(DELIMITER)[0]]: subtree };
}
}
});
return res;
}

return recursiveGet(obj[firstPart], parts.slice(1).join(DELIMITER));
}

module.exports = {
recursiveGet,
};

1 comment on commit 8092bed

@vercel
Copy link

@vercel vercel bot commented on 8092bed Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.