Skip to content

Commit

Permalink
Added web of trust score slider
Browse files Browse the repository at this point in the history
  • Loading branch information
calvadev committed May 24, 2024
1 parent 7bfa3b1 commit e4920c7
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 27 deletions.
1 change: 0 additions & 1 deletion components/product-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ export default function NewForm({
imgCallbackOnUpload={(imgUrl) => {
setImages((prevValues) => {
const updatedImages = [...prevValues];
console.log("imgUrl", imgUrl);
if (imgUrl && imgUrl.length > 0) {
return [...updatedImages, imgUrl];
}
Expand Down
57 changes: 57 additions & 0 deletions components/utility-components/shopstr-slider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useState, useEffect } from "react";
import { Button } from "@nextui-org/react";
import { Slider } from "@nextui-org/react";
import { useTheme } from "next-themes";
import { getLocalStorageData } from "../../components/utility/nostr-helper-functions";
import { SHOPSTRBUTTONCLASSNAMES } from "../../components/utility/STATIC-VARIABLES";

const ShopstrSlider = () => {
const { theme, setTheme } = useTheme();

const [wot, setWot] = useState(getLocalStorageData().wot);
const [wotIsChanged, setWotIsChanged] = useState(false);

useEffect(() => {
localStorage.setItem("wot", String(wot));
}, [wot]);

const refreshPage = () => {
window.location.reload();
setWotIsChanged(false);
};

return (
<>
<div className="flex items-center p-2">
<Slider
size="sm"
step={1}
color={theme === "dark" ? "warning" : "secondary"}
label="Minimum Follower Count:"
showSteps={true}
maxValue={10}
minValue={1}
defaultValue={wot}
className="max-w-md text-light-text dark:text-dark-text"
onChangeEnd={(value) => {
if (Array.isArray(value)) {
setWot(value[0]);
} else {
setWot(value);
}
setWotIsChanged(true);
}}
/>
</div>
{wotIsChanged && (
<div className="flex h-fit flex-row justify-between bg-light-bg px-3 py-[15px] dark:bg-dark-bg">
<Button className={SHOPSTRBUTTONCLASSNAMES} onClick={refreshPage}>
Refresh to Apply
</Button>
</div>
)}
</>
);
};

export default ShopstrSlider;
12 changes: 12 additions & 0 deletions components/utility/nostr-helper-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ const LOCALSTORAGECONSTANTS = {
mints: "mints",
tokens: "tokens",
history: "history",
wot: "wot",
};

export const setLocalStorageDataOnSignIn = ({
Expand All @@ -307,12 +308,14 @@ export const setLocalStorageDataOnSignIn = ({
encryptedPrivateKey,
relays,
mints,
wot,
}: {
signInMethod: string;
pubkey: string;
encryptedPrivateKey?: string;
relays?: string[];
mints?: string[];
wot?: number;
}) => {
localStorage.setItem(LOCALSTORAGECONSTANTS.signInMethod, signInMethod);
localStorage.setItem(
Expand Down Expand Up @@ -345,6 +348,8 @@ export const setLocalStorageDataOnSignIn = ({
JSON.stringify(mints ? mints : ["https://mint.minibits.cash/Bitcoin"]),
);

localStorage.setItem(LOCALSTORAGECONSTANTS.wot, String(wot ? wot : 3));

window.dispatchEvent(new Event("storage"));
};

Expand All @@ -362,6 +367,7 @@ export interface LocalStorageInterface {
mints: string[];
tokens: [];
history: [];
wot: number;
encryptedPrivateKey?: string;
}

Expand All @@ -374,6 +380,7 @@ export const getLocalStorageData = (): LocalStorageInterface => {
let mints;
let tokens;
let history;
let wot;

if (typeof window !== "undefined") {
userNPub = localStorage.getItem(LOCALSTORAGECONSTANTS.userNPub);
Expand Down Expand Up @@ -437,6 +444,10 @@ export const getLocalStorageData = (): LocalStorageInterface => {
history = localStorage.getItem(LOCALSTORAGECONSTANTS.history)
? JSON.parse(localStorage.getItem("history") as string)
: localStorage.setItem(LOCALSTORAGECONSTANTS.history, JSON.stringify([]));

wot = localStorage.getItem(LOCALSTORAGECONSTANTS.wot)
? Number(localStorage.getItem(LOCALSTORAGECONSTANTS.wot))
: 3;
}
return {
signInMethod: signInMethod as string,
Expand All @@ -447,6 +458,7 @@ export const getLocalStorageData = (): LocalStorageInterface => {
mints,
tokens: tokens || [],
history: history || [],
wot: wot || 3,
};
};

Expand Down
1 change: 0 additions & 1 deletion components/wallet/send-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const SendButton = () => {
(p: Proof) => mintKeySetIds?.includes(p.id),
);
const tokenToSend = await wallet.send(numSats, filteredProofs);
console.log(tokenToSend);
const encodedSendToken = getEncodedToken({
token: [
{
Expand Down
5 changes: 3 additions & 2 deletions pages/api/nostr/fetch-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export const fetchAllFollows = async (
followList: string[];
}> => {
return new Promise(async function (resolve, reject) {
const wot = getLocalStorageData().wot;
try {
const relay = await Relay.connect("wss://purplepag.es");

Expand Down Expand Up @@ -325,7 +326,7 @@ export const fetchAllFollows = async (
});
secondDegreeFollowsArrayFromRelay =
secondDegreeFollowsArrayFromRelay.filter(
(pubkey) => (pubkeyCount.get(pubkey) || 0) >= 3,
(pubkey) => (pubkeyCount.get(pubkey) || 0) >= wot,
);
followsArrayFromRelay.push(...secondDegreeFollowsArrayFromRelay);
},
Expand Down Expand Up @@ -384,7 +385,7 @@ export const fetchAllFollows = async (
pubkeyCount.set(pubkey, (pubkeyCount.get(pubkey) || 0) + 1);
});
secondDegreeFollowsArray = secondDegreeFollowsArray.filter(
(pubkey) => (pubkeyCount.get(pubkey) || 0) >= 3,
(pubkey) => (pubkeyCount.get(pubkey) || 0) >= wot,
);
followsArray.push(...secondDegreeFollowsArray);
},
Expand Down
60 changes: 39 additions & 21 deletions pages/settings/preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SHOPSTRBUTTONCLASSNAMES } from "../../components/utility/STATIC-VARIABL
import { getLocalStorageData } from "../../components/utility/nostr-helper-functions";
import { useTheme } from "next-themes";
import { SettingsBreadCrumbs } from "@/components/settings/settings-bread-crumbs";
import ShopstrSlider from "../../components/utility-components/shopstr-slider";

const PreferencesPage = () => {
const [relays, setRelays] = useState(Array<string>(0));
Expand All @@ -32,14 +33,19 @@ const PreferencesPage = () => {
const [showMintModal, setShowMintModal] = useState(false);
const [copiedToClipboard, setCopiedToClipboard] = useState(false);

const [isLoaded, setIsLoaded] = useState(false);

useEffect(() => {
if (typeof window !== "undefined") {
setMints(getLocalStorageData().mints);
}
setIsLoaded(true);
}, []);

useEffect(() => {
localStorage.setItem("mints", JSON.stringify(mints));
if (mints.length != 0) {
localStorage.setItem("mints", JSON.stringify(mints));
}
}, [mints]);

const { theme, setTheme } = useTheme();
Expand Down Expand Up @@ -115,7 +121,9 @@ const PreferencesPage = () => {
}, []);

useEffect(() => {
localStorage.setItem("relays", JSON.stringify(relays));
if (relays.length != 0) {
localStorage.setItem("relays", JSON.stringify(relays));
}
}, [relays]);

const handleToggleRelayModal = () => {
Expand Down Expand Up @@ -271,32 +279,32 @@ const PreferencesPage = () => {
</span>

<div>
{mints.length === 0 && (
{mints.length === 0 ? (
<div className="mt-8 flex items-center justify-center">
<p className="break-words text-center text-xl dark:text-dark-text">
No mint added . . .
</p>
</div>
)}

<div className="overflow-y-scroll rounded-md bg-light-bg dark:bg-dark-bg">
<div className="mb-2 flex items-center justify-between rounded-md border-2 border-light-fg px-3 py-2 dark:border-dark-fg">
<div className="max-w-xsm break-all text-light-text dark:text-dark-text">
{mints[0]}
) : (
<div className="overflow-y-scroll rounded-md bg-light-bg dark:bg-dark-bg">
<div className="mb-2 flex items-center justify-between rounded-md border-2 border-light-fg px-3 py-2 dark:border-dark-fg">
<div className="max-w-xsm break-all text-light-text dark:text-dark-text">
{mints[0]}
</div>
<ClipboardIcon
onClick={handleCopyMint}
className={`ml-2 h-6 w-6 cursor-pointer text-light-text dark:text-dark-text ${
copiedToClipboard ? "hidden" : ""
}`}
/>
<CheckIcon
className={`ml-2 h-6 w-6 cursor-pointer text-light-text dark:text-dark-text ${
copiedToClipboard ? "" : "hidden"
}`}
/>
</div>
<ClipboardIcon
onClick={handleCopyMint}
className={`ml-2 h-6 w-6 cursor-pointer text-light-text dark:text-dark-text ${
copiedToClipboard ? "hidden" : ""
}`}
/>
<CheckIcon
className={`ml-2 h-6 w-6 cursor-pointer text-light-text dark:text-dark-text ${
copiedToClipboard ? "" : "hidden"
}`}
/>
</div>
</div>
)}
{mints.length > 0 && (
<div className="mx-4 my-4 flex items-center justify-center text-center">
<InformationCircleIcon className="h-6 w-6 text-light-text dark:text-dark-text" />
Expand Down Expand Up @@ -409,6 +417,16 @@ const PreferencesPage = () => {
</Modal>
</div>

<span className="my-4 flex text-2xl font-bold text-light-text dark:text-dark-text">
Web of Trust
</span>

{isLoaded && (
<>
<ShopstrSlider />
</>
)}

<span className="my-4 flex text-2xl font-bold text-light-text dark:text-dark-text">
Theme
</span>
Expand Down
5 changes: 3 additions & 2 deletions pages/sign-in/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const LoginPage = ({ router }: { router: NextRouter }) => {
JSON.stringify(["https://mint.minibits.cash/Bitcoin"]),
);

// alert("Signed in as " + npub + ".");
localStorage.setItem("wot", "3");

router.push("/");
}
} else {
Expand Down Expand Up @@ -78,7 +79,7 @@ const LoginPage = ({ router }: { router: NextRouter }) => {
"mints",
JSON.stringify(["https://mint.minibits.cash/Bitcoin"]),
);
// alert("Signed in as " + npub + ".");
localStorage.setItem("wot", "3");
router.push("/");
} catch (error) {
alert("Extension sign in failed!");
Expand Down

0 comments on commit e4920c7

Please sign in to comment.