@crossbell/ipfs-react
Advanced tools
Comparing version 0.0.11 to 0.0.12
import React from 'react'; | ||
export declare function useHandleLinkClick(): (event: React.MouseEvent<HTMLAnchorElement>) => void; | ||
export declare function useHandleLinkClick(onClick?: React.MouseEventHandler<HTMLAnchorElement>): (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void; | ||
//# sourceMappingURL=use-handle-link-click.d.ts.map |
@@ -7,31 +7,15 @@ "use strict"; | ||
exports.useHandleLinkClick = void 0; | ||
const ipfs_gateway_1 = require("@crossbell/ipfs-gateway"); | ||
const react_1 = __importDefault(require("react")); | ||
const use_ipfs_gateway_1 = require("./use-ipfs-gateway"); | ||
function useHandleLinkClick() { | ||
function useHandleLinkClick(onClick) { | ||
const ipfsGateway = (0, use_ipfs_gateway_1.useIpfsGateway)(); | ||
const onClickRef = react_1.default.useRef(onClick); | ||
onClickRef.current = onClick; | ||
return react_1.default.useCallback((event) => { | ||
const elm = event.currentTarget; | ||
const link = elm.href; | ||
const gatewayPrefix = ipfsGateway.config.serviceWorker?.gatewayPrefix; | ||
const ipfsUrl = (() => { | ||
if ((0, ipfs_gateway_1.isIpfsUrl)(link)) { | ||
return link; | ||
} | ||
else if (gatewayPrefix) { | ||
return (0, ipfs_gateway_1.swGatewayUrlToIpfsUrl)(gatewayPrefix, link); | ||
} | ||
else { | ||
return null; | ||
} | ||
})(); | ||
if (ipfsUrl) { | ||
const newWindow = window.open(); | ||
if (newWindow) { | ||
onClickRef.current?.(event); | ||
if (!event.defaultPrevented) { | ||
const ipfsUrl = ipfsGateway.convertToIpfsUrl(event.currentTarget.href); | ||
if (ipfsUrl) { | ||
event.preventDefault(); | ||
ipfsGateway | ||
.getFastestWeb2Url(ipfsUrl) | ||
.catch(() => null) | ||
.then((url) => url ?? ipfsGateway.getFallbackWeb2Url(ipfsUrl)) | ||
.then((url) => (newWindow.location.href = url)); | ||
ipfsGateway.openIpfsUrl(ipfsUrl); | ||
} | ||
@@ -38,0 +22,0 @@ } |
export * from './context'; | ||
export * from './hooks'; | ||
export * from './components'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./hooks"), exports); | ||
__exportStar(require("./components"), exports); |
import React from 'react'; | ||
export declare function useHandleLinkClick(): (event: React.MouseEvent<HTMLAnchorElement>) => void; | ||
export declare function useHandleLinkClick(onClick?: React.MouseEventHandler<HTMLAnchorElement>): (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void; | ||
//# sourceMappingURL=use-handle-link-click.d.ts.map |
@@ -1,30 +0,14 @@ | ||
import { isIpfsUrl, swGatewayUrlToIpfsUrl } from '@crossbell/ipfs-gateway'; | ||
import React from 'react'; | ||
import { useIpfsGateway } from './use-ipfs-gateway'; | ||
export function useHandleLinkClick() { | ||
export function useHandleLinkClick(onClick) { | ||
const ipfsGateway = useIpfsGateway(); | ||
const onClickRef = React.useRef(onClick); | ||
onClickRef.current = onClick; | ||
return React.useCallback((event) => { | ||
const elm = event.currentTarget; | ||
const link = elm.href; | ||
const gatewayPrefix = ipfsGateway.config.serviceWorker?.gatewayPrefix; | ||
const ipfsUrl = (() => { | ||
if (isIpfsUrl(link)) { | ||
return link; | ||
} | ||
else if (gatewayPrefix) { | ||
return swGatewayUrlToIpfsUrl(gatewayPrefix, link); | ||
} | ||
else { | ||
return null; | ||
} | ||
})(); | ||
if (ipfsUrl) { | ||
const newWindow = window.open(); | ||
if (newWindow) { | ||
onClickRef.current?.(event); | ||
if (!event.defaultPrevented) { | ||
const ipfsUrl = ipfsGateway.convertToIpfsUrl(event.currentTarget.href); | ||
if (ipfsUrl) { | ||
event.preventDefault(); | ||
ipfsGateway | ||
.getFastestWeb2Url(ipfsUrl) | ||
.catch(() => null) | ||
.then((url) => url ?? ipfsGateway.getFallbackWeb2Url(ipfsUrl)) | ||
.then((url) => (newWindow.location.href = url)); | ||
ipfsGateway.openIpfsUrl(ipfsUrl); | ||
} | ||
@@ -31,0 +15,0 @@ } |
export * from './context'; | ||
export * from './hooks'; | ||
export * from './components'; | ||
//# sourceMappingURL=index.d.ts.map |
export * from './context'; | ||
export * from './hooks'; | ||
export * from './components'; |
{ | ||
"name": "@crossbell/ipfs-react", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"license": "MIT", | ||
@@ -19,7 +19,7 @@ "repository": "github:Crossbell-Box/crossbell-monorepo", | ||
"peerDependencies": { | ||
"@crossbell/ipfs-gateway": "0.0.11", | ||
"@crossbell/ipfs-gateway": "0.0.12", | ||
"react": "^17 || ^18" | ||
}, | ||
"devDependencies": { | ||
"@crossbell/ipfs-gateway": "0.0.11", | ||
"@crossbell/ipfs-gateway": "0.0.12", | ||
"@types/react": "18.0.18", | ||
@@ -26,0 +26,0 @@ "node-fetch": "^3.2.10", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19081
51
267