-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to use custom public gateway (#1834)
* fix: local gateway and ipfs.io when cannot preview License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com> * custom public gateway * fix: opening non-unixfs DAG License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com> * fix: use custom public gateway on shareable links License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com> * fix: ux of submit/reset cosmetic fix that adds reset button to public gateway section in Settings and disables buttons when they are no-op. * style: public gateway description shareable links are better way to sonvey the practical meaning of this setting, other places are incidental Co-authored-by: Marcin Rataj <lidel@lidel.org>
- Loading branch information
Showing
14 changed files
with
218 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,54 @@ | ||
import { readSetting, writeSetting } from './local-storage' | ||
|
||
export const DEFAULT_GATEWAY = 'https://dweb.link' | ||
|
||
const readPublicGatewaySetting = () => { | ||
const setting = readSetting('ipfsPublicGateway') | ||
return setting || DEFAULT_GATEWAY | ||
} | ||
|
||
const init = () => ({ | ||
availableGateway: null, | ||
publicGateway: readPublicGatewaySetting() | ||
}) | ||
|
||
export const checkValidHttpUrl = (value) => { | ||
let url | ||
|
||
try { | ||
url = new URL(value) | ||
} catch (_) { | ||
return false | ||
} | ||
|
||
return url.protocol === 'http:' || url.protocol === 'https:' | ||
} | ||
|
||
const bundle = { | ||
name: 'gateway', | ||
|
||
reducer: (state = { availableGateway: null }, action) => { | ||
reducer: (state = init(), action) => { | ||
if (action.type === 'SET_AVAILABLE_GATEWAY') { | ||
return { ...state, availableGateway: action.payload } | ||
} | ||
|
||
if (action.type === 'SET_PUBLIC_GATEWAY') { | ||
return { ...state, publicGateway: action.payload } | ||
} | ||
|
||
return state | ||
}, | ||
|
||
doSetAvailableGateway: url => ({ dispatch }) => dispatch({ type: 'SET_AVAILABLE_GATEWAY', payload: url }), | ||
|
||
selectAvailableGateway: (state) => state?.gateway?.availableGateway | ||
doUpdatePublicGateway: (address) => async ({ dispatch }) => { | ||
await writeSetting('ipfsPublicGateway', address) | ||
dispatch({ type: 'SET_PUBLIC_GATEWAY', payload: address }) | ||
}, | ||
|
||
selectAvailableGateway: (state) => state?.gateway?.availableGateway, | ||
|
||
selectPublicGateway: (state) => state?.gateway?.publicGateway | ||
} | ||
|
||
export default bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Reads setting from the `localStorage` with a given `id` as JSON. If JSON | ||
* parse is failed setting is interpreted as a string value. | ||
* @param {string} id | ||
* @returns {string|object|null} | ||
*/ | ||
export const readSetting = (id) => { | ||
/** @type {string|null} */ | ||
let setting = null | ||
if (window.localStorage) { | ||
try { | ||
setting = window.localStorage.getItem(id) | ||
} catch (error) { | ||
console.error(`Error reading '${id}' value from localStorage`, error) | ||
} | ||
|
||
try { | ||
return JSON.parse(setting || '') | ||
} catch (_) { | ||
// res was probably a string, so pass it on. | ||
return setting | ||
} | ||
} | ||
|
||
return setting | ||
} | ||
|
||
/** | ||
* @param {string} id | ||
* @param {string|number|boolean|object} value | ||
*/ | ||
export const writeSetting = (id, value) => { | ||
try { | ||
window.localStorage.setItem(id, JSON.stringify(value)) | ||
} catch (error) { | ||
console.error(`Error writing '${id}' value to localStorage`, error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { connect } from 'redux-bundler-react' | ||
import { withTranslation } from 'react-i18next' | ||
import Button from '../button/Button' | ||
import { checkValidHttpUrl, DEFAULT_GATEWAY } from '../../bundles/gateway' | ||
|
||
const PublicGatewayForm = ({ t, doUpdatePublicGateway, publicGateway }) => { | ||
const [value, setValue] = useState(publicGateway) | ||
const initialIsValidGatewayUrl = !checkValidHttpUrl(value) | ||
const [showFailState, setShowFailState] = useState(initialIsValidGatewayUrl) | ||
const [isValidGatewayUrl, setIsValidGatewayUrl] = useState(initialIsValidGatewayUrl) | ||
|
||
// Updates the border of the input to indicate validity | ||
useEffect(() => { | ||
setShowFailState(!isValidGatewayUrl) | ||
}, [isValidGatewayUrl]) | ||
|
||
// Updates the border of the input to indicate validity | ||
useEffect(() => { | ||
const isValid = checkValidHttpUrl(value) | ||
setIsValidGatewayUrl(isValid) | ||
setShowFailState(!isValid) | ||
}, [value]) | ||
|
||
const onChange = (event) => setValue(event.target.value) | ||
|
||
const onSubmit = async (event) => { | ||
event.preventDefault() | ||
doUpdatePublicGateway(value) | ||
} | ||
|
||
const onReset = async (event) => { | ||
event.preventDefault() | ||
setValue(DEFAULT_GATEWAY) | ||
doUpdatePublicGateway(DEFAULT_GATEWAY) | ||
} | ||
|
||
const onKeyPress = (event) => { | ||
if (event.key === 'Enter') { | ||
onSubmit(event) | ||
} | ||
} | ||
|
||
return ( | ||
<form onSubmit={onSubmit}> | ||
<input | ||
id='public-gateway' | ||
aria-label={t('terms.publicGateway')} | ||
placeholder={t('publicGatewayForm.placeholder')} | ||
type='text' | ||
className={`w-100 lh-copy monospace f5 pl1 pv1 mb2 charcoal input-reset ba b--black-20 br1 ${showFailState ? 'focus-outline-red b--red-muted' : 'focus-outline-green b--green-muted'}`} | ||
onChange={onChange} | ||
onKeyPress={onKeyPress} | ||
value={value} | ||
/> | ||
<div className='tr'> | ||
<Button | ||
minWidth={100} | ||
height={40} | ||
bg='bg-charcoal' | ||
className='tc' | ||
disabled={value === DEFAULT_GATEWAY} | ||
onClick={onReset}> | ||
{t('app:actions.reset')} | ||
</Button> | ||
<Button | ||
minWidth={100} | ||
height={40} | ||
className='mt2 mt0-l ml2-l tc' | ||
disabled={!isValidGatewayUrl || value === publicGateway}> | ||
{t('actions.submit')} | ||
</Button> | ||
</div> | ||
</form> | ||
) | ||
} | ||
|
||
export default connect( | ||
'doUpdatePublicGateway', | ||
'selectPublicGateway', | ||
withTranslation('app')(PublicGatewayForm) | ||
) |
Oops, something went wrong.