Skip to content

Commit 34c9b0d

Browse files
committed
refactor: wire up hints for copy actions
This wires up hints to show copied value + adds 30s cache for async lookup function used by this as a small optimization.
1 parent 568d24a commit 34c9b0d

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

add-on/src/lib/ipfs-companion.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ module.exports = async function init () {
242242
async function sendStatusUpdateToBrowserAction () {
243243
if (!browserActionPort) return
244244
const dropSlash = url => url.replace(/\/$/, '')
245+
const currentTab = await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
245246
const info = {
246247
active: state.active,
247248
ipfsNodeType: state.ipfsNodeType,
@@ -254,7 +255,7 @@ module.exports = async function init () {
254255
apiURLString: dropSlash(state.apiURLString),
255256
redirect: state.redirect,
256257
noIntegrationsHostnames: state.noIntegrationsHostnames,
257-
currentTab: await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
258+
currentTab
258259
}
259260
try {
260261
const v = await ipfs.version()
@@ -267,6 +268,11 @@ module.exports = async function init () {
267268
if (state.active && info.currentTab) {
268269
const url = info.currentTab.url
269270
info.isIpfsContext = ipfsPathValidator.isIpfsPageActionsContext(url)
271+
if (info.isIpfsContext) {
272+
info.currentTabPublicUrl = ipfsPathValidator.resolveToPublicUrl(url)
273+
info.currentTabContentPath = ipfsPathValidator.resolveToIpfsPath(url)
274+
info.currentTabCid = await ipfsPathValidator.resolveToCid(url)
275+
}
270276
info.currentDnslinkFqdn = dnslinkResolver.findDNSLinkHostname(url)
271277
info.currentFqdn = info.currentDnslinkFqdn || new URL(url).hostname
272278
info.currentTabIntegrationsOptOut = info.noIntegrationsHostnames && info.noIntegrationsHostnames.includes(info.currentFqdn)

add-on/src/lib/ipfs-path.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
'use strict'
22
/* eslint-env browser */
33

4+
const pMemoize = require('p-memoize')
45
const isIPFS = require('is-ipfs')
56
const isFQDN = require('is-fqdn')
67

8+
// For how long more expensive lookups (DAG traversal etc) should be cached
9+
const RESULT_TTL_MS = 30 * 1000
10+
711
// Turns URL or URIencoded path into a content path
812
function ipfsContentPath (urlOrPath, opts) {
913
opts = opts || {}
@@ -279,7 +283,7 @@ function createIpfsPathValidator (getState, getIpfs, dnslinkResolver) {
279283
// - Returns null if no valid path can be produced
280284
// The purpose of this resolver is to return immutable /ipfs/ address
281285
// even if /ipns/ is present in its input.
282-
async resolveToImmutableIpfsPath (urlOrPath) {
286+
resolveToImmutableIpfsPath: pMemoize(async function (urlOrPath) {
283287
const path = ipfsPathValidator.resolveToIpfsPath(urlOrPath)
284288
// Fail fast if no IPFS Path
285289
if (!path) return null
@@ -322,14 +326,14 @@ function createIpfsPathValidator (getState, getIpfs, dnslinkResolver) {
322326
}
323327
// Return /ipfs/ path
324328
return path
325-
},
329+
}, { maxAge: RESULT_TTL_MS }),
326330

327331
// Resolve URL or path to a raw CID:
328332
// - Result is the direct CID
329333
// - Ignores ?search and #hash from original URL
330334
// - Returns null if no CID can be produced
331335
// The purpose of this resolver is to return direct CID without anything else.
332-
async resolveToCid (urlOrPath) {
336+
resolveToCid: pMemoize(async function (urlOrPath) {
333337
const path = ipfsPathValidator.resolveToIpfsPath(urlOrPath)
334338
// Fail fast if no IPFS Path
335339
if (!path) return null
@@ -366,7 +370,7 @@ function createIpfsPathValidator (getState, getIpfs, dnslinkResolver) {
366370

367371
const directCid = isIPFS.ipfsPath(result) ? result.split('/')[2] : result
368372
return directCid
369-
}
373+
}, { maxAge: RESULT_TTL_MS })
370374
}
371375

372376
return ipfsPathValidator

add-on/src/popup/browser-action/context-actions.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ function contextActions ({
2424
currentFqdn,
2525
currentDnslinkFqdn,
2626
currentTabIntegrationsOptOut,
27+
currentTabContentPath,
28+
currentTabCid,
29+
currentTabPublicUrl,
2730
ipfsNodeType,
2831
isIpfsContext,
2932
isPinning,
@@ -54,17 +57,17 @@ function contextActions ({
5457
}) : null}
5558
${navItem({
5659
text: browser.i18n.getMessage(contextMenuCopyAddressAtPublicGw),
57-
helperText: 'PublicGw',
60+
helperText: currentTabPublicUrl,
5861
onClick: () => onCopy(contextMenuCopyAddressAtPublicGw)
5962
})}
6063
${navItem({
6164
text: browser.i18n.getMessage(contextMenuCopyCanonicalAddress),
62-
helperText: 'ipfsPath',
65+
helperText: currentTabContentPath,
6366
onClick: () => onCopy(contextMenuCopyCanonicalAddress)
6467
})}
6568
${navItem({
6669
text: browser.i18n.getMessage(contextMenuCopyRawCid),
67-
helperText: 'cid',
70+
helperText: currentTabCid,
6871
disabled: !activeCidResolver,
6972
onClick: () => onCopy(contextMenuCopyRawCid)
7073
})}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
"mime-types": "2.1.25",
150150
"multiaddr": "7.2.1",
151151
"multiaddr-to-uri": "5.0.0",
152-
"p-memoize": "3.1.0",
152+
"p-memoize": "4.0.0",
153153
"p-queue": "6.2.1",
154154
"path-browserify": "1.0.0",
155155
"piggybacker": "2.0.0",

yarn.lock

+20-7
Original file line numberDiff line numberDiff line change
@@ -10013,7 +10013,7 @@ mem@^1.1.0:
1001310013
dependencies:
1001410014
mimic-fn "^1.0.0"
1001510015

10016-
mem@^4.0.0, mem@^4.3.0:
10016+
mem@^4.0.0:
1001710017
version "4.3.0"
1001810018
resolved "https://registry.yarnpkg.com/mem/-/mem-4.3.0.tgz#461af497bc4ae09608cdb2e60eefb69bff744178"
1001910019
integrity sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==
@@ -10031,6 +10031,14 @@ mem@^5.0.0:
1003110031
mimic-fn "^2.1.0"
1003210032
p-is-promise "^2.1.0"
1003310033

10034+
mem@^6.0.1:
10035+
version "6.1.0"
10036+
resolved "https://registry.yarnpkg.com/mem/-/mem-6.1.0.tgz#846eca0bd4708a8f04b9c3f3cd769e194ae63c5c"
10037+
integrity sha512-RlbnLQgRHk5lwqTtpEkBTQ2ll/CG/iB+J4Hy2Wh97PjgZgXgWJWrFF+XXujh3UUVLvR4OOTgZzcWMMwnehlEUg==
10038+
dependencies:
10039+
map-age-cleaner "^0.1.3"
10040+
mimic-fn "^3.0.0"
10041+
1003410042
memdown@^1.0.0:
1003510043
version "1.4.1"
1003610044
resolved "https://registry.yarnpkg.com/memdown/-/memdown-1.4.1.tgz#b4e4e192174664ffbae41361aa500f3119efe215"
@@ -10245,6 +10253,11 @@ mimic-fn@^2.0.0, mimic-fn@^2.1.0:
1024510253
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
1024610254
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
1024710255

10256+
mimic-fn@^3.0.0:
10257+
version "3.0.0"
10258+
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-3.0.0.tgz#76044cfa8818bbf6999c5c9acadf2d3649b14b4b"
10259+
integrity sha512-PiVO95TKvhiwgSwg1IdLYlCTdul38yZxZMIcnDSFIBUm4BNZha2qpQ4GpJ++15bHoKDtrW2D69lMfFwdFYtNZQ==
10260+
1024810261
mimic-response@^1.0.0, mimic-response@^1.0.1:
1024910262
version "1.0.1"
1025010263
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
@@ -11662,13 +11675,13 @@ p-map@^3.0.0:
1166211675
dependencies:
1166311676
aggregate-error "^3.0.0"
1166411677

11665-
p-memoize@3.1.0:
11666-
version "3.1.0"
11667-
resolved "https://registry.yarnpkg.com/p-memoize/-/p-memoize-3.1.0.tgz#ac7587983c9e530139f969ca7b41ef40e93659aa"
11668-
integrity sha512-e5tIvrsr7ydUUnxb534iQWtXxWgk/86IsH+H+nV4FHouIggBt4coXboKBt26o4lTu7JbEnGSeXdEsYR8BhAHFA==
11678+
p-memoize@4.0.0:
11679+
version "4.0.0"
11680+
resolved "https://registry.yarnpkg.com/p-memoize/-/p-memoize-4.0.0.tgz#1f955b6c38aaa4b74d243e354eae51a7ecb48e94"
11681+
integrity sha512-oMxCJKVS75Bf2RWtXJNQNaX2K1G0FYpllOh2iTsPXZqnf9dWMcis3BL+pRdLeQY8lIdwwL01k/UV5LBdcVhZzg==
1166911682
dependencies:
11670-
mem "^4.3.0"
11671-
mimic-fn "^2.1.0"
11683+
mem "^6.0.1"
11684+
mimic-fn "^3.0.0"
1167211685

1167311686
1167411687
version "6.2.1"

0 commit comments

Comments
 (0)