Skip to content

Commit

Permalink
feat: useIpfsHashCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeday committed Dec 14, 2023
1 parent b30dce4 commit 617deaf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
6 changes: 3 additions & 3 deletions features/ipfs/ipfs-base-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let ipfsBaseScript = '';
// #!if IPFS_MODE === "true"
ipfsBaseScript = `
(function () {
const base = document.createElement('base')
base.href = window.location.pathname
document.head.append(base)
const base = document.createElement('base');
base.href = window.location.pathname;
document.head.append(base);
})();
`;
// #!endif
Expand Down
70 changes: 70 additions & 0 deletions features/ipfs/use-ipfs-hash-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useLidoSWR } from '@lido-sdk/react';
import { dynamics } from 'config';
import { useEffect, useState } from 'react';
import { useMainnetStaticRpcProvider } from 'shared/hooks/use-mainnet-static-rpc-provider';
import { STRATEGY_LAZY } from 'utils/swrStrategies';

type EnsHashCheckReturn = {
cid: string | null;
};

const ENS_NAME = 'ethereum.staked.eth';

export const useIpfsHashCheck = () => {
const provider = useMainnetStaticRpcProvider();

const [currentCID, setCurrentCID] = useState<null | string>(() => {
// try to extract CID from url
if (typeof window !== 'undefined') {
const cidRegExp = new RegExp(
'(Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})',
);
return cidRegExp.exec(window.location.href)?.[0] ?? null;
}
return null;
});
const swr = useLidoSWR<EnsHashCheckReturn>(
['swr:ipfs-hash-check', ENS_NAME, currentCID],
async ([_, ensName]: [string, string, string | null]) => {
const resolver = await provider.getResolver(ensName);
if (resolver) {
const contentHash = await resolver.getContentHash();
if (contentHash) {
return {
cid: contentHash,
};
}
}
return {
cid: null,
};
},
{ ...STRATEGY_LAZY, isPaused: () => !dynamics.ipfsMode },
);

useEffect(() => {
if (swr.data?.cid && !currentCID) {
setCurrentCID(swr.data.cid);
}
}, [swr.data?.cid, currentCID]);

const isUpdateAvailable = Boolean(
swr.data?.cid && currentCID && swr.data.cid !== currentCID,
);

return {
isUpdateAvailable,
get data() {
return swr.data;
},
get initialLoading() {
return swr.initialLoading;
},
get loading() {
return swr.loading;
},
get error() {
return swr.error;
},
};
};

0 comments on commit 617deaf

Please sign in to comment.