|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { OpenrpcDocument } from '@open-rpc/meta-schema' |
| 3 | +import type { NimiqRpcMethods } from '../../utils/rpc' |
| 4 | +import { useAsyncData, useRoute } from '#imports' |
| 5 | +import { computed, ref, watch } from 'vue' |
| 6 | +import openRpcDocument from '../../../data/openrpc-document.json' |
| 7 | +import { loadMethods } from '../../utils/rpc' |
| 8 | +
|
| 9 | +const route = useRoute() |
| 10 | +
|
| 11 | +const { data: groups } = await useAsyncData('rpc-sidebar-method-groups', () => loadMethods(openRpcDocument as OpenrpcDocument)) |
| 12 | +
|
| 13 | +const normalizedPath = computed(() => normalizePath(route.path)) |
| 14 | +const activeGroup = computed(() => groups.value?.find(group => |
| 15 | + group.methods.some(method => normalizePath(method.link) === normalizedPath.value), |
| 16 | +)) |
| 17 | +const expandedGroup = ref<string | null>(null) |
| 18 | +
|
| 19 | +watch(activeGroup, (group) => { |
| 20 | + expandedGroup.value = group?.text ?? null |
| 21 | +}, { immediate: true }) |
| 22 | +
|
| 23 | +function isExpanded(group: NimiqRpcMethods) { |
| 24 | + return expandedGroup.value === group.text |
| 25 | +} |
| 26 | +
|
| 27 | +function toggleGroup(group: NimiqRpcMethods) { |
| 28 | + expandedGroup.value = isExpanded(group) ? null : group.text |
| 29 | +} |
| 30 | +
|
| 31 | +function isActive(link: string) { |
| 32 | + return normalizePath(link) === normalizedPath.value |
| 33 | +} |
| 34 | +
|
| 35 | +function normalizePath(path: string) { |
| 36 | + return path.replace(/\/+$/, '') || '/' |
| 37 | +} |
| 38 | +</script> |
| 39 | + |
| 40 | +<template> |
| 41 | + <div class="space-y-5"> |
| 42 | + <div class="space-y-1 px-2"> |
| 43 | + <p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted"> |
| 44 | + Methods |
| 45 | + </p> |
| 46 | + <p class="text-sm leading-6 text-toned"> |
| 47 | + Browse RPC methods by category. |
| 48 | + </p> |
| 49 | + </div> |
| 50 | + |
| 51 | + <nav class="space-y-1" aria-label="RPC methods"> |
| 52 | + <NuxtLink |
| 53 | + to="/rpc/methods/" |
| 54 | + class="flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors" |
| 55 | + :class="isActive('/rpc/methods/') |
| 56 | + ? 'bg-elevated text-highlighted' |
| 57 | + : 'text-toned hover:bg-elevated/60 hover:text-highlighted'" |
| 58 | + > |
| 59 | + <UIcon name="i-tabler:grid-dots" class="size-4 shrink-0" /> |
| 60 | + <span>All</span> |
| 61 | + </NuxtLink> |
| 62 | + |
| 63 | + <div |
| 64 | + v-for="group in (groups || [])" |
| 65 | + :key="group.text" |
| 66 | + class="space-y-1" |
| 67 | + > |
| 68 | + <button |
| 69 | + type="button" |
| 70 | + class="flex w-full items-center justify-between gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors" |
| 71 | + :class="isExpanded(group) || activeGroup?.text === group.text |
| 72 | + ? 'bg-elevated text-highlighted' |
| 73 | + : 'text-toned hover:bg-elevated/60 hover:text-highlighted'" |
| 74 | + @click="toggleGroup(group)" |
| 75 | + > |
| 76 | + <span class="flex min-w-0 items-center gap-3"> |
| 77 | + <UIcon :name="group.icon" class="size-4 shrink-0" /> |
| 78 | + <span class="truncate">{{ group.text }}</span> |
| 79 | + </span> |
| 80 | + <UIcon |
| 81 | + :name="isExpanded(group) ? 'i-lucide-chevron-down' : 'i-lucide-chevron-right'" |
| 82 | + class="size-4 shrink-0 text-muted" |
| 83 | + /> |
| 84 | + </button> |
| 85 | + |
| 86 | + <div |
| 87 | + v-if="isExpanded(group)" |
| 88 | + class="ml-5 border-l border-default pl-3" |
| 89 | + > |
| 90 | + <NuxtLink |
| 91 | + v-for="method in group.methods" |
| 92 | + :key="method.name" |
| 93 | + :to="method.link" |
| 94 | + class="block rounded-md px-3 py-2 font-mono text-sm transition-colors" |
| 95 | + :class="isActive(method.link) |
| 96 | + ? 'bg-elevated text-highlighted' |
| 97 | + : 'text-toned hover:bg-elevated/60 hover:text-highlighted'" |
| 98 | + > |
| 99 | + <span class="block truncate">{{ method.name }}</span> |
| 100 | + </NuxtLink> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + </nav> |
| 104 | + </div> |
| 105 | +</template> |
0 commit comments