From 5dd366b91e2a94e658bf5250a8a0fa64c09e1c11 Mon Sep 17 00:00:00 2001 From: towfiqi Date: Sun, 5 Mar 2023 12:14:08 +0600 Subject: [PATCH] fix: Fixes Broken Image thumbnail loading issue. --- components/domains/DomainItem.tsx | 9 +++++---- pages/domains/index.tsx | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/components/domains/DomainItem.tsx b/components/domains/DomainItem.tsx index e4db635..83a47c7 100644 --- a/components/domains/DomainItem.tsx +++ b/components/domains/DomainItem.tsx @@ -9,10 +9,11 @@ import Icon from '../common/Icon'; type DomainItemProps = { domain: DomainType, selected: boolean, - isConsoleIntegrated: boolean + isConsoleIntegrated: boolean, + thumb: string, } -const DomainItem = ({ domain, selected, isConsoleIntegrated = false }: DomainItemProps) => { +const DomainItem = ({ domain, selected, isConsoleIntegrated = false, thumb }: DomainItemProps) => { const { keywordsUpdated, slug, keywordCount = 0, avgPosition = 0, scVisits = 0, scImpressions = 0, scPosition = 0 } = domain; // const router = useRouter(); return ( @@ -21,10 +22,10 @@ const DomainItem = ({ domain, selected, isConsoleIntegrated = false }: DomainIte
- {domain.domain} + {thumb && {domain.domain}}
-

{domain.domain}

+

{domain.domain}

{keywordsUpdated && ( Updated diff --git a/pages/domains/index.tsx b/pages/domains/index.tsx index 335d09c..4ee70c2 100644 --- a/pages/domains/index.tsx +++ b/pages/domains/index.tsx @@ -11,16 +11,43 @@ import { useFetchDomains } from '../../services/domains'; import DomainItem from '../../components/domains/DomainItem'; import Icon from '../../components/common/Icon'; +type thumbImages = { [domain:string] : string } + const SingleDomain: NextPage = () => { const router = useRouter(); const [noScrapprtError, setNoScrapprtError] = useState(false); const [showSettings, setShowSettings] = useState(false); const [showAddDomain, setShowAddDomain] = useState(false); + const [domainThumbs, setDomainThumbs] = useState({}); const { data: appSettings } = useFetchSettings(); const { data: domainsData, isLoading } = useFetchDomains(router, true); useEffect(() => { - console.log('Domains Data: ', domainsData); + // console.log('Domains Data: ', domainsData); + if (domainsData?.domains && domainsData.domains.length > 0) { + const domainThumbsRaw = localStorage.getItem('domainThumbs'); + const domThumbs = domainThumbsRaw ? JSON.parse(domainThumbsRaw) : {}; + domainsData.domains.forEach(async (domain:DomainType) => { + if (domain.domain) { + if (!domThumbs[domain.domain]) { + const domainImageBlob = await fetch(`https://image.thum.io/get/auth/66909-serpbear/maxAge/96/width/200/https://${domain.domain}`).then((res) => res.blob()); + if (domainImageBlob) { + const reader = new FileReader(); + await new Promise((resolve, reject) => { + reader.onload = resolve; + reader.onerror = reject; + reader.readAsDataURL(domainImageBlob); + }); + const imageBase: string = reader.result && typeof reader.result === 'string' ? reader.result : ''; + localStorage.setItem('domainThumbs', JSON.stringify({ ...domThumbs, [domain.domain]: imageBase })); + setDomainThumbs((currentThumbs) => ({ ...currentThumbs, [domain.domain]: imageBase })); + } + } else { + setDomainThumbs((currentThumbs) => ({ ...currentThumbs, [domain.domain]: domThumbs[domain.domain] })); + } + } + }); + } }, [domainsData]); useEffect(() => { @@ -62,6 +89,7 @@ const SingleDomain: NextPage = () => { domain={domain} selected={false} isConsoleIntegrated={!!(appSettings && appSettings?.settings?.search_console_integrated) } + thumb={domainThumbs[domain.domain]} // isConsoleIntegrated={false} />; })}