Skip to content

Commit

Permalink
fix: Fixes Broken Image thumbnail loading issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
towfiqi committed Mar 5, 2023
1 parent 5fc1779 commit 5dd366b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 5 additions & 4 deletions components/domains/DomainItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -21,10 +22,10 @@ const DomainItem = ({ domain, selected, isConsoleIntegrated = false }: DomainIte
<a className='flex flex-col lg:flex-row'>
<div className={`flex-1 p-6 flex ${!isConsoleIntegrated ? 'basis-1/3' : ''}`}>
<div className="domain_thumb w-20 h-20 mr-6 bg-slate-100 rounded border border-gray-200 overflow-hidden">
<img src={`https://image.thum.io/get/maxAge/96/width/200/https://${domain.domain}`} alt={domain.domain} />
{thumb && <img src={thumb} alt={domain.domain} />}
</div>
<div className="domain_details flex-1">
<h3 className='font-semibold text-base mb-2 capitalize'>{domain.domain}</h3>
<h3 className='font-semibold text-base mb-2'>{domain.domain}</h3>
{keywordsUpdated && (
<span className=' text-gray-600 text-xs'>
Updated <TimeAgo title={dayjs(keywordsUpdated).format('DD-MMM-YYYY, hh:mm:ss A')} date={keywordsUpdated} />
Expand Down
30 changes: 29 additions & 1 deletion pages/domains/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<thumbImages>({});
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(() => {
Expand Down Expand Up @@ -62,6 +89,7 @@ const SingleDomain: NextPage = () => {
domain={domain}
selected={false}
isConsoleIntegrated={!!(appSettings && appSettings?.settings?.search_console_integrated) }
thumb={domainThumbs[domain.domain]}
// isConsoleIntegrated={false}
/>;
})}
Expand Down

0 comments on commit 5dd366b

Please sign in to comment.