From 4932ac7cf0014ecc0a8468a3dc8f9a3c397d58d8 Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Sun, 31 Mar 2024 09:34:58 +0200 Subject: [PATCH] allow changing index --- src/assets/main.css | 4 +++ src/components/FeedRow.vue | 6 +---- src/components/IndexNode.vue | 8 ++---- src/global.ts | 3 +-- src/views/FeedView.vue | 5 ++-- src/views/HomeView.vue | 2 ++ src/views/IndexView.vue | 51 +++++++++++++++++++++++++++++------- src/views/KuboView.vue | 2 +- 8 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/assets/main.css b/src/assets/main.css index 2591c56..c139e8a 100644 --- a/src/assets/main.css +++ b/src/assets/main.css @@ -7,6 +7,10 @@ font-weight: normal; } +.mono { + font-family: monospace; +} + a, .green { text-decoration: none; diff --git a/src/components/FeedRow.vue b/src/components/FeedRow.vue index a42ad7b..54ca04e 100644 --- a/src/components/FeedRow.vue +++ b/src/components/FeedRow.vue @@ -31,8 +31,4 @@ props.dag.then((r) => { </tr> </template> -<style scoped> -.mono { - font-family: monospace; -} -</style> +<style scoped></style> diff --git a/src/components/IndexNode.vue b/src/components/IndexNode.vue index bfb99cb..1566968 100644 --- a/src/components/IndexNode.vue +++ b/src/components/IndexNode.vue @@ -33,7 +33,7 @@ onload() <template v-if="inode"> <ul> <li v-for="c in inode.children.filter((e) => e != null)"> - <div>{{ c![0] }} <a :href="exploreUrl(c![1])" target="_blank">cid</a></div> + <div><span class="mono">{{ c![0] }}</span> <a :href="exploreUrl(c![1])" target="_blank">cid</a></div> <div><IndexNode :key="c![1].toString()" :cid="c![1]"></IndexNode></div> </li> </ul> @@ -41,8 +41,4 @@ onload() <template v-if="leaf"> leaf ({{ leaf.leaf.length }}) </template> </template> -<style scoped> -.mono { - font-family: monospace; -} -</style> +<style scoped></style> diff --git a/src/global.ts b/src/global.ts index 9610dbc..a138828 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,4 +1,3 @@ -import { ref } from 'vue' -import type { Ref } from 'vue' +import { ref, type Ref } from 'vue' export const feed: Ref<string[]> = ref([]) diff --git a/src/views/FeedView.vue b/src/views/FeedView.vue index 7a1efca..3c218c5 100644 --- a/src/views/FeedView.vue +++ b/src/views/FeedView.vue @@ -1,7 +1,6 @@ <script setup lang="ts"> import { feed } from '@/global' -import { ref } from 'vue' -import type { Ref } from 'vue' +import { ref, type Ref } from 'vue' import { TOPIC, pubsub } from '@/p2p' import type { PeerId } from '@libp2p/interface' import { CID } from 'multiformats' @@ -60,4 +59,4 @@ function get_dag(cid: CID): Promise<IndexRequest> { </div> </template> -<style></style> +<style scoped></style> diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index a7e9a3e..dc048d6 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -8,3 +8,5 @@ <p>This UI is optional. It aims to help understand how the indexer part work and interacts with the local IPFS node (kubo for instance).</p> </main> </template> + +<style scoped></style> \ No newline at end of file diff --git a/src/views/IndexView.vue b/src/views/IndexView.vue index e9fb50f..369a97e 100644 --- a/src/views/IndexView.vue +++ b/src/views/IndexView.vue @@ -2,14 +2,23 @@ import { kubo } from '@/kubo' import { emptyInode } from '../types' import { IPNS } from '../consts' -import { ref } from 'vue' import { CID } from 'multiformats' -import type { Ref } from 'vue' +import { ref, type Ref, computed } from 'vue' import IndexNode from '../components/IndexNode.vue' +const msg = ref('') const rootnodeipns = ref('') const ipnsTarget = ref('') -const ipnsTargetCid: Ref<CID | null> = ref(null) +const targetCid: Ref<CID | null> = ref(null) + +const isValid = computed(() => { + try { + CID.parse(msg.value) + return true + } catch { + return false + } +}) // inits indexing and publishes ipns entry for it async function initIndex() { @@ -18,27 +27,49 @@ async function initIndex() { const res = await kubo.name.publish(rootcid, { ttl: '1s' }) console.log(res) rootnodeipns.value = res.name + resolve() } async function resolve() { for await (const name of kubo.name.resolve(IPNS, { nocache: true })) { console.log(name) ipnsTarget.value = name - ipnsTargetCid.value = CID.parse(name.slice(6)) + targetCid.value = CID.parse(name.slice(6)) + } +} + +function setTargetCid() { + if (isValid.value) { + const cid = CID.parse(msg.value) + targetCid.value = cid + kubo.name.publish(cid, { ttl: '1s' }).then((_) => resolve()) + } else { + targetCid.value = null } } + +resolve() </script> <template> <div> <h1>Index</h1> <p>Info about the index</p> - <p>initialize index: <button @click="initIndex">initialize</button></p> - <p>root node ipns: /ipns/{{ rootnodeipns }}</p> - <p>IPNS {{ IPNS }}</p> - <p>pointing to <button @click="resolve">resolve</button> {{ ipnsTarget }}</p> - <p><IndexNode v-if="ipnsTargetCid" :key="ipnsTarget" :cid="ipnsTargetCid"></IndexNode></p> + <p>initialize empty index: <button @click="initIndex">reinitialize ⚠️</button></p> + <p>root node ipns: <span class="mono">/ipns/{{ rootnodeipns }}</span></p> + <p>You can use it to configure root node instead of hardcoded one:</p> + <p class="mono">{{ IPNS }}</p> + <p>which is pointing to <button @click="resolve">resolve</button></p> + <p class="mono">{{ ipnsTarget }}</p> + <p> + set IPNS target cid + <input v-model="msg" @keyup.enter="setTargetCid" /> + <button v-on:click="setTargetCid">Set ⚠️</button> + </p> + <h2>Tree preview</h2> + <p class="mono">{{ targetCid }}</p> + <p><IndexNode v-if="targetCid" :key="ipnsTarget" :cid="targetCid"></IndexNode></p> </div> </template> -<style></style> +<style scoped></style> diff --git a/src/views/KuboView.vue b/src/views/KuboView.vue index ddf9637..84dfd81 100644 --- a/src/views/KuboView.vue +++ b/src/views/KuboView.vue @@ -5,4 +5,4 @@ </div> </template> -<style></style> +<style scoped></style> -- GitLab