import React, { useEffect, useState } from "react"; import { getOrchestratorInfo, getEnsInfo, setCachedOrch, getOrchestratorInfoSilent } from "../actions/livepeer"; import { useDispatch, useSelector } from 'react-redux'; import { Text } from '@mantine/core'; const EventButtonAddress = (obj) => { const dispatch = useDispatch(); const livepeer = useSelector((state) => state.livepeerstate); const [hasRefreshed, setRefresh] = useState(false); const [orchInfo, setOrchInfo] = useState(null); const [thisAvatar, setAvatar] = useState(""); const now = new Date().getTime(); useEffect(() => { let thisInfo = null; let thisDomain = null; let hasENS = null; // Lookup domain in cache if (livepeer.ensDomainMapping && !hasRefreshed) { for (const thisAddr of livepeer.ensDomainMapping) { if (thisAddr.address === obj.address) { thisDomain = thisAddr; // Check timeout if (now - thisAddr.timestamp < 86400000) { break; } // Is outdated if (!hasRefreshed) { console.log("Refresh due to old ENS domain"); getEnsInfo(obj.address); setRefresh(true); } break; } } // If it was not cached at all if (thisDomain == null && !hasRefreshed) { console.log("Refresh due to non-existing ENS domain"); setRefresh(true); getEnsInfo(obj.address); } } // Lookup current info in cache only if this addr has a mapped ENS domain if (livepeer.ensInfoMapping && thisDomain && thisDomain.domain && !hasRefreshed) { for (const thisAddr of livepeer.ensInfoMapping) { if (thisAddr.domain === thisDomain.domain) { thisInfo = thisAddr; hasENS = true; // Check timeout if (now - thisAddr.timestamp < 3600000) { break; } // Is outdated if (!hasRefreshed) { console.log("Refresh due to old ENS info"); getEnsInfo(obj.address); setRefresh(true); } break; } } // If it was not cached at all if (thisInfo == null && !hasRefreshed) { console.log("Refresh due to non-existing ENS info"); getEnsInfo(obj.address); setRefresh(true); } } if (thisInfo && thisInfo != orchInfo) { console.log("Setting INFO obj"); setOrchInfo(thisInfo); } }, [livepeer.ensDomainMapping]); useEffect(() => { // Check if cached as an orchestrator let shouldUpdate = false; if (livepeer.orchInfo) { for (const thisOrch of livepeer.orchInfo) { if (thisOrch.id === obj.address) { return; } } shouldUpdate = true; } if (livepeer.delInfo) { for (const thisOrch of livepeer.delInfo) { if (thisOrch.id === obj.address) { return; } } shouldUpdate = true; } // Preload Orch info if (shouldUpdate) { console.log("Refresh due to non-existing orch in global state"); getOrchestratorInfoSilent(obj.address); } }, [livepeer.orchInfo]); let thisName; let thisIcon; if (orchInfo && orchInfo.domain) { thisName = {orchInfo.domain}; // Hardcoded Chad substitution if (thisAvatar == "" && orchInfo.avatar){ let thisUrl = orchInfo.avatar.url; if (thisUrl == "https://stronk.rocks/avatar.png"){ thisUrl = "https://stronk.rocks/avatar.png?" + performance.now(); } setAvatar(thisUrl); } if (thisAvatar != "") { thisIcon = } } else { thisName = {obj.address}; thisIcon = null; } return (
{thisIcon}
) } export default EventButtonAddress;