Last active
December 2, 2021 07:29
-
-
Save sebastinez/6cf4b7ddd8063c629f218fb18a12f440 to your computer and use it in GitHub Desktop.
Improved anchors proposal
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
diff --git a/src/base/projects/Browser.svelte b/src/base/projects/Browser.svelte | |
index ccb9e46..70f3f5e 100644 | |
--- a/src/base/projects/Browser.svelte | |
+++ b/src/base/projects/Browser.svelte | |
@@ -4,12 +4,12 @@ | |
import type { Profile } from '@app/profile'; | |
import * as proj from '@app/project'; | |
import Loading from '@app/Loading.svelte'; | |
- import { Org } from '@app/base/orgs/Org'; | |
import * as utils from '@app/utils'; | |
import Tree from './Tree.svelte'; | |
import Blob from './Blob.svelte'; | |
import Readme from './Readme.svelte'; | |
+ import { ethers } from "ethers"; | |
enum Status { | |
Loading, | |
@@ -79,10 +79,49 @@ | |
return proj.getTree(urn, commit, path, config); | |
}; | |
+const GetAllAnchors = ` | |
+ query GetAllAnchors($project: Bytes!, $org: ID!) { | |
+ anchors(where: { objectId: $project, org: $org }) { | |
+ multihash | |
+ timestamp | |
+ } | |
+ } | |
+`; | |
+ | |
+ interface AnchorObject { | |
+ timestamp: number; | |
+ commit: string; | |
+ multihash: string; | |
+ } | |
+ | |
+ function getAnchorIndexByCommit(anchor: string[], commit: string): number { | |
+ return anchor.findIndex((a: string) => a === commit); | |
+ } | |
+ | |
+ async function getAllAnchors(anchors: string | null, urn: string): Promise<string[] | null> { | |
+ if (! anchors) { | |
+ return null; | |
+ } | |
+ const unpadded = utils.decodeRadicleId(urn); | |
+ const id = ethers.utils.hexZeroPad(unpadded, 32); | |
+ const allAnchors = await utils.querySubgraph(config.orgs.subgraph, GetAllAnchors, { project: id, org: anchors }); | |
+ return allAnchors.anchors | |
+ .sort(sortAnchorsByTimestamp) | |
+ .map((anchor: AnchorObject) => utils.formatProjectHash(ethers.utils.arrayify(anchor.multihash))); | |
+ } | |
+ | |
+ function sortAnchorsByTimestamp(a: AnchorObject, b: AnchorObject) { | |
+ if (a.timestamp > b.timestamp) { | |
+ return -1; | |
+ } else if (a.timestamp < b.timestamp) { | |
+ return 1; | |
+ } | |
+ } | |
+ | |
// This is reactive to respond to path changes that don't originate from this | |
// component, eg. when using the browser's "back" button. | |
$: getBlob = loadBlob(path); | |
- $: getAnchor = anchors ? Org.getAnchor(anchors, urn, config) : null; | |
+ $: getAnchor = anchors ? getAllAnchors(anchors, urn) : null; | |
$: loadingPath = state.status == Status.Loading ? state.path : null; | |
</script> | |
@@ -299,18 +338,18 @@ | |
{#await getAnchor} | |
<Loading small margins /> | |
{:then anchor} | |
- {#if anchor === commit} | |
+ {#if anchor?.includes(commit)} | |
{#if commit === project.head} | |
<span class="anchor-widget anchor-latest"> | |
<span class="anchor-label" title="{anchors}">anchored 🔒</span> | |
</span> | |
{:else} | |
<span class="anchor-widget" on:click={() => navigateBrowser(project.head)}> | |
- <span class="anchor-label" title="{anchors}">anchored 🔒</span> | |
+ <span class="anchor-label" title="{anchors}">{getAnchorIndexByCommit(anchor, commit)} anchor behind latest 🔒</span> | |
</span> | |
{/if} | |
{:else if anchor} | |
- <span class="anchor-widget not-anchored" on:click={() => navigateBrowser(anchor)}> | |
+ <span class="anchor-widget not-anchored" on:click={() => navigateBrowser(anchor[0])}> | |
<span class="anchor-label">not anchored 🔓</span> | |
</span> | |
{:else} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment