Skip to content

Commit 795633d

Browse files
authored
fix: various fixes for ipfs-webui consumption (#458)
* fix: setExplorePath doesnt throw for encoded paths * fix: kuboGateway defaults are handled gracefully * chore: move localStorage info log * fix: do not init helia on mount * fix: react style prop semicolon warning * chore: lint fix
1 parent ff3e359 commit 795633d

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/components/object-info/links-table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface RowProps {
4646

4747
const Row: React.FC<RowProps> = ({ onLinkClick, startIndex, index, rowHeight, link }) => {
4848
const key = startIndex + index
49-
const backgroundColor = key % 2 === 0 ? '#fff' : 'rgb(251, 251, 251);'
49+
const backgroundColor = key % 2 === 0 ? '#fff' : 'rgb(251, 251, 251)'
5050

5151
return (
5252
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions

src/lib/init-helia.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import { trustlessGateway } from '@helia/block-brokers'
22
import { createHeliaHTTP } from '@helia/http'
3-
import { type Helia } from '@helia/interface'
3+
import { type Routing, type Helia } from '@helia/interface'
44
import { delegatedHTTPRouting, httpGatewayRouting } from '@helia/routers'
55
import { addDagNodeToHelia } from '../lib/helpers.js'
66
import { getHashersForCodes } from './hash-importer.js'
77
import type { KuboGatewayOptions } from '../types.d.js'
88

9+
const localStorageKey = 'explore.ipld.gatewayEnabled'
10+
console.info(
11+
`🎛️ Customise whether ipld-explorer-components fetches content from gateways by setting an '${localStorageKey}' value to true/false in localStorage. e.g. localStorage.setItem('${localStorageKey}', false) -- NOTE: defaults to true`
12+
)
13+
914
/**
1015
* Whether to enable remote gateways for fetching content. We default to true if the setting is not present.
1116
*/
1217
function areRemoteGatewaysEnabled (): boolean {
13-
const localStorageKey = 'explore.ipld.gatewayEnabled'
14-
console.info(
15-
`🎛️ Customise whether ipld-explorer-components fetches content from gateways by setting an '${localStorageKey}' value to true/false in localStorage. e.g. localStorage.setItem('explore.ipld.gatewayEnabled', false) -- NOTE: defaults to true`
16-
)
1718
const gatewayEnabledSetting = localStorage.getItem(localStorageKey)
1819

1920
return gatewayEnabledSetting != null ? JSON.parse(gatewayEnabledSetting) : true
2021
}
2122

2223
export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions): Promise<Helia> {
23-
const routers = [
24-
// Always add the Kubo gateway
25-
httpGatewayRouting({ gateways: [`${kuboGatewayOptions.protocol ?? 'http'}://${kuboGatewayOptions.host}:${kuboGatewayOptions.port}`] })
26-
]
24+
const routers: Array<Partial<Routing>> = []
25+
const kuboGatewayUrlString = `${kuboGatewayOptions.protocol ?? 'http'}://${kuboGatewayOptions.host}:${kuboGatewayOptions.port}`
26+
try {
27+
const kuboGatewayUrl = new URL(kuboGatewayUrlString)
28+
// Always try to add the Kubo gateway if we have a valid URL
29+
routers.push(httpGatewayRouting({ gateways: [kuboGatewayUrl.href] }))
30+
} catch (error) {
31+
// eslint-disable-next-line no-console
32+
console.error('Invalid kuboGateway url string: %s', kuboGatewayUrlString, error)
33+
}
2734

2835
if (areRemoteGatewaysEnabled()) {
2936
// eslint-disable-next-line no-console

src/providers/explore.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export const ExploreProvider = ({ children, state, explorePathPrefix = '#/explor
153153

154154
const setExplorePath = (path: string | null): void => {
155155
const newPath = processPath(path, explorePathPrefix)
156-
if (newPath != null && !window.location.href.includes(newPath)) {
156+
if (newPath != null && !window.location.href.includes(encodeURI(newPath))) {
157157
throw new Error('setExplorePath should only be used to update the state, not the URL. If you are using a routing library that doesn\'t allow you to listen to hashchange events, ensure the URL is updated prior to calling setExplorePath.')
158158
}
159159
setExploreState((exploreState) => ({

src/providers/helia.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Helia } from '@helia/interface'
2-
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react'
2+
import React, { createContext, useContext, useState, useCallback } from 'react'
33
import packageJson from '../../package.json'
44
import initHelia from '../lib/init-helia.js'
55
import type { KuboGatewayOptions } from '../types.js'
@@ -29,7 +29,8 @@ const getDefaultKuboGatewayOptions = (): KuboGatewayOptions => {
2929
const localStorageKuboGatewayOptions = localStorage.getItem('kuboGateway')
3030
if (localStorageKuboGatewayOptions != null) {
3131
try {
32-
return JSON.parse(localStorageKuboGatewayOptions) as KuboGatewayOptions
32+
const kuboGatewaySettings = JSON.parse(localStorageKuboGatewayOptions) as KuboGatewayOptions
33+
return { ...defaultKuboGatewayOptions, ...kuboGatewaySettings }
3334
} catch (e) {
3435
console.error('getDefaultKuboGatewayOptions error', e)
3536
}
@@ -67,11 +68,6 @@ export const HeliaProvider = ({ children }: React.ComponentProps<any>): any => {
6768
}
6869
}, [kuboGatewayOptions, setHelia])
6970

70-
useEffect(() => {
71-
void doInitHelia()
72-
// eslint-disable-next-line react-hooks/exhaustive-deps
73-
}, [])
74-
7571
return (
7672
<HeliaContext.Provider value={{ helia, error, kuboGatewayOptions, selectHeliaReady, selectHeliaIdentity, doInitHelia, setKuboGatewayOptions: setKuboGatewayOptionsPublic }}>
7773
{children}

0 commit comments

Comments
 (0)