-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: embed provider webrtc maddr into share link #206
Open
2color
wants to merge
14
commits into
main
Choose a base branch
from
embed-maddr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,382
−8,560
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0b8715
refactor: remove unused pending prop
2color 013a901
deps: bump downstream deps like libp2p
2color f964926
feat: embed provider webrtc maddr into share link
2color d63b775
fix: add missing dep
2color f8e1ff0
fix: lint error
2color 3831b9d
fix: linting errors
2color 86360cc
fix: build error
2color 3dd4e1e
fix: refine dialability in node info box
2color 843611e
fix: peer id overflow on mobile
2color e0d3cbb
refactor: side effects in files-provider
2color 6b9b15d
chore: remove unnecessary style
2color 50c8cb4
fix: small fixes
2color d0fc2c8
feat: make dht providing optional
2color 01cf103
fix: only get browser dialable webrtc multiaddrs
2color File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 +1,17 @@ | ||
import { type CID } from 'multiformats/cid' | ||
export function getShareLink (cid: string | CID, name?: string): string { | ||
if (name === undefined) { | ||
return new URL(`/#/${cid}`, window.location.href).toString() | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
||
export function getShareLink ({ cid, name, webrtcMaddrs }: { cid: string | CID, name?: string, webrtcMaddrs?: Multiaddr[] }): string { | ||
const url = new URL(`/#/${cid}?`, window.location.href) | ||
|
||
if (name !== undefined) { | ||
url.hash += `filename=${encodeURIComponent(name)}` | ||
} | ||
|
||
if (webrtcMaddrs !== undefined) { | ||
const encodedMaddrs = webrtcMaddrs.map(addr => addr.toString()).join(',') | ||
url.hash += `&maddrs=${encodeURIComponent(encodedMaddrs)}` | ||
} | ||
|
||
return new URL(`/#/${cid}?filename=${encodeURI(name)}`, window.location.href).toString() | ||
return url.toString() | ||
} |
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,7 +1,8 @@ | ||||||||||||||||||||||||
import { multiaddr } from '@multiformats/multiaddr' | ||||||||||||||||||||||||
import { cid } from 'is-ipfs' | ||||||||||||||||||||||||
import { useEffect } from 'react' | ||||||||||||||||||||||||
import { useHashLocation } from 'wouter/use-hash-location' | ||||||||||||||||||||||||
import { useDownloadInfo } from '../providers/download-provider' | ||||||||||||||||||||||||
import { useFilesDispatch } from './use-files' | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* * `(?<=\/)` — Positive lookbehind to ensure that the match is preceded by / without including it in the result. | ||||||||||||||||||||||||
|
@@ -12,17 +13,27 @@ const cidRegex = /(?<=\/)[^/?]+(?=\?|$)/ | |||||||||||||||||||||||
|
||||||||||||||||||||||||
const filenameRegex = /(?<=filename=)[^&]+/ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const maddrsRegex = /(?<=maddrs=)[^&]+/ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export type CurrentPage = 'add' | 'download' | ||||||||||||||||||||||||
export const useCurrentPage = (): CurrentPage => { | ||||||||||||||||||||||||
const [location] = useHashLocation() | ||||||||||||||||||||||||
const { setDownloadInfo } = useDownloadInfo() | ||||||||||||||||||||||||
const dispatch = useFilesDispatch() | ||||||||||||||||||||||||
const maybeCid = location.match(cidRegex)?.[0] ?? null | ||||||||||||||||||||||||
const filename = location.match(filenameRegex)?.[0] ?? null | ||||||||||||||||||||||||
const maddrs = location.match(maddrsRegex)?.[0] ?? null | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Dispatch the fetch_start action if the URL contains a cid | ||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||
if (maybeCid == null) return | ||||||||||||||||||||||||
setDownloadInfo(maybeCid, filename) | ||||||||||||||||||||||||
}, [maybeCid, filename]) | ||||||||||||||||||||||||
dispatch({ type: 'reset_files' }) | ||||||||||||||||||||||||
const decodedFilename = decodeURIComponent(filename ?? '') | ||||||||||||||||||||||||
// Decode the provider's maddrs from the URL | ||||||||||||||||||||||||
const decodedMaddrs = maddrs != null ? decodeURIComponent(maddrs).split(',') : null | ||||||||||||||||||||||||
const multiaddrs = decodedMaddrs != null ? decodedMaddrs.map(maddr => multiaddr(maddr)) : null | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
dispatch({ type: 'fetch_start', cid: maybeCid, filename: decodedFilename, providerMaddrs: multiaddrs }) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to cancel the fetch on unmount. maybe create an abort controller and pass signal to
Suggested change
|
||||||||||||||||||||||||
}, [maybeCid, filename, maddrs]) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if (location.startsWith('/add') || !cid(maybeCid ?? '')) { | ||||||||||||||||||||||||
return 'add' | ||||||||||||||||||||||||
|
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { | ||
WebRTC, | ||
WebRTCDirect, | ||
WebSocketsSecure, | ||
WebTransport | ||
} from '@multiformats/multiaddr-matcher' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
||
export const getWebRTCAddrs = (addrs?: Multiaddr[]): Multiaddr[] => { | ||
return ( | ||
addrs?.filter( | ||
(addr: Multiaddr) => | ||
WebRTC.exactMatch(addr) && | ||
(WebRTCDirect.matches(addr) || | ||
WebSocketsSecure.matches(addr) || | ||
WebTransport.matches(addr)) | ||
) ?? [] | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I feel like a separate
useMemo
on amaddrs
dep with the fully resolvedmultiaddrs
would be a little better.. but there is likely little difference.. sometimes react can get wonky if you have different effects too separated.