-
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
base: main
Are you sure you want to change the base?
Changes from all commits
b0b8715
013a901
f964926
d63b775
f8e1ff0
3831b9d
86360cc
3dd4e1e
843611e
e0d3cbb
6b9b15d
50c8cb4
d0fc2c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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() | ||
} |
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' | ||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||||
import { WebRTC } from '@multiformats/multiaddr-matcher' | ||||||||
import type { Multiaddr } from '@multiformats/multiaddr' | ||||||||
|
||||||||
export const getWebRTCAddrs = (addrs?: Multiaddr[]): Multiaddr[] => { | ||||||||
// TODO: Get only WebRTC addrs dialable from other browsers, e.g. WebRTC Direct, WebRTC Secure WebSockets, and WebRTC WebTransport (currently not included in Helia transports) | ||||||||
return addrs?.filter((addr: Multiaddr) => WebRTC.exactMatch(addr)) ?? [] | ||||||||
Comment on lines
+5
to
+6
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. are you talking about:
Suggested change
|
||||||||
} |
This file was deleted.
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.