Skip to content

Commit

Permalink
feat(components): show dependencies and dependents in component details
Browse files Browse the repository at this point in the history
related #358
  • Loading branch information
antfu committed Aug 10, 2023
1 parent 143b7fb commit 7dbc3d1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 15 deletions.
33 changes: 30 additions & 3 deletions packages/devtools/client/components/ComponentDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { pascalCase } from 'scule'
const props = defineProps<{
component: Component
dependencies?: string[]
dependents?: string[]
}>()
// @ts-expect-error types
Expand All @@ -15,7 +17,7 @@ const copy = useCopy()

<template>
<div flex="~ col gap1" items-start of-hidden>
<div flex="~ gap2">
<div flex="~ gap2" px3>
<ComponentName :component="component" />
<NIconButton title="Copy name" flex-none icon="carbon-copy" @click="copy(`<${name}></${name}>`)" />
<Badge
Expand All @@ -25,13 +27,38 @@ const copy = useCopy()
v-text="'runtime'"
/>
</div>
<div>
<div px3 pb2>
<FilepathItem
v-if="filePath"
:filepath="filePath"
w-full text-sm op25 group-hover:op75
w-full text-sm op40 group-hover:op75
/>
</div>

<div v-if="dependents" border="t base" max-h-60 w-full of-auto px3 py3>
<div text-sm>
<strong text-primary>{{ dependents.length }}</strong><span op50> references</span>
</div>
<div v-if="dependents.length" flex="~ col gap-2" items-start pt3 text-sm op75>
<FilepathItem
v-for="id of dependents" :key="id"
:filepath="id"
/>
</div>
</div>

<div v-if="dependencies" border="t base" max-h-60 w-full of-auto px3 py3>
<div text-sm>
<strong text-primary>{{ dependencies.length }}</strong><span op50> dependencies</span>
</div>
<div v-if="dependencies.length" flex="~ col gap-2" items-start pt3 text-sm op75>
<FilepathItem
v-for="id of dependencies" :key="id"
:filepath="id"
/>
</div>
</div>

<slot />
</div>
</template>
23 changes: 21 additions & 2 deletions packages/devtools/client/components/ComponentItem.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<script setup lang="ts">
import type { Component } from 'nuxt/schema'
import type { ComponentRelationship } from '../../types'
const props = defineProps<{
component: Component
relationships?: ComponentRelationship[] | null
}>()
const dependencies = computed(() => {
const deps = props.relationships?.find(i => i.id === props.component.filePath)?.deps
return deps?.map(i => props.relationships?.find(j => j.id === i)?.id).filter(Boolean) as string[]
})
const dependents = computed(() => {
const deps = props.relationships?.filter(i => i.deps.includes(props.component.filePath))
return deps?.map(i => props.relationships?.find(j => j.id === i.id)?.id).filter(Boolean) as string[]
})
// @ts-expect-error types
const filePath = computed(() => props.component.filePath || props.component.file || props.component.__file || '')
</script>

<template>
<div

hover="bg-active"
class="group"
flex="~ gap2"
Expand All @@ -22,9 +33,17 @@ const filePath = computed(() => props.component.filePath || props.component.file
<ComponentName :component="component" />
</button>
<template #popper>
<ComponentDetails :component="component" p4 />
<ComponentDetails
:component="component"
:dependencies="dependencies"
:dependents="dependents"
w-100 pt4
/>
</template>
</VDropdown>
<sup v-if="dependents?.length" ml--1 op50>
{{ dependents?.length }}
</sup>
<Badge
v-if="component.global"
bg-green-400:10 text-green-400
Expand Down
29 changes: 23 additions & 6 deletions packages/devtools/client/components/ComponentsGraph.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ComponentRelationship } from '~/../src/types'
const props = defineProps<{
components: Component[]
relationships?: ComponentRelationship[] | null
}>()
const container = ref<HTMLElement>()
Expand All @@ -23,7 +24,6 @@ const selected = ref<{
const pages = useServerPages()
const config = useServerConfig()
const layouts = useLayouts()
const relationships = useComponentsRelationships()
const {
componentsGraphShowNodeModules: showNodeModules,
Expand All @@ -36,10 +36,9 @@ const {
const selectedFilter = ref<ComponentRelationship>()
const entries = computed(() => {
const relations = (relationships.value || [])
const relations = (props.relationships || [])
if (selectedFilter.value) {
const set = new Set<ComponentRelationship>()
relations.find(i => i.id === selected.value?.id)
function addToSet(rel?: ComponentRelationship) {
if (!rel || set.has(rel))
return
Expand Down Expand Up @@ -128,6 +127,20 @@ const data = computed<Data>(() => {
}
})
const selectedDependencies = computed(() => {
if (!selected.value?.component)
return []
const deps = props.relationships?.find(i => i.id === selected.value?.component?.filePath)?.deps
return deps?.map(i => props.relationships?.find(j => j.id === i)?.id).filter(Boolean) as string[]
})
const selectedDependents = computed(() => {
if (!selected.value?.component)
return []
const deps = props.relationships?.filter(i => i.deps.includes(selected.value!.component!.filePath!))
return deps?.map(i => props.relationships?.find(j => j.id === i.id)?.id).filter(Boolean) as string[]
})
onMounted(() => {
const options: Options = {
nodes: {
Expand Down Expand Up @@ -252,9 +265,13 @@ function setFilter() {
w-80
@close="selected = undefined"
>
<div v-if="selected && selected.component" p4 pr10 pt4 flex="~ col gap4">
<ComponentDetails :component="selected.component" />
<div>
<div v-if="selected && selected.component" py4 pt4 flex="~ col">
<ComponentDetails
:component="selected.component"
:dependencies="selectedDependencies"
:dependents="selectedDependents"
/>
<div border="t base" p4>
<NButton n="primary solid" @click="setFilter()">
Filter to this component
</NButton>
Expand Down
29 changes: 25 additions & 4 deletions packages/devtools/client/components/ComponentsList.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import type { Component } from 'nuxt/schema'
import Fuse from 'fuse.js'
import type { ComponentRelationship } from '../../types'
const props = defineProps<{
components: Component[]
relationships?: ComponentRelationship[] | null
}>()
const search = ref('')
Expand Down Expand Up @@ -84,7 +86,12 @@ const filtered = computed(() => {
:open="filtered.user.length <= DETAILS_MAX_ITEMS"
:description="`Total components: ${filtered.count.user}`"
>
<ComponentItem v-for="c of filtered.user" :key="c.filePath" :component="c" />
<ComponentItem
v-for="c of filtered.user"
:key="c.filePath"
:component="c"
:relationships="relationships"
/>
</NSectionBlock>
<NSectionBlock
v-if="filtered.runtime.length"
Expand All @@ -93,15 +100,24 @@ const filtered = computed(() => {
text="Runtime components"
:description="`Total components: ${filtered.count.runtime}`"
>
<ComponentItem v-for="c of filtered.runtime" :key="c.filePath" :component="c" />
<ComponentItem
v-for="c of filtered.runtime" :key="c.filePath"
:component="c"
:relationships="relationships"
/>
</NSectionBlock>
<NSectionBlock
v-if="filtered.builtin.length"
icon="simple-icons-nuxtdotjs"
text="Built-in components"
:description="`Total components: ${filtered.count.builtin}`"
>
<ComponentItem v-for="c of filtered.builtin" :key="c.filePath" :component="c" />
<ComponentItem
v-for="c of filtered.builtin"
:key="c.filePath"
:component="c"
:relationships="relationships"
/>
</NSectionBlock>
<NSectionBlock
v-if="filtered.lib.size"
Expand All @@ -113,7 +129,12 @@ const filtered = computed(() => {
<div v-for="[key, value] of filtered.lib.entries()" :key="key" ml-2>
<NIconTitle :text="`${key} (${value.length})`" py1 op50 />
<div pl4>
<ComponentItem v-for="c of value" :key="c.filePath" :component="c" />
<ComponentItem
v-for="c of value"
:key="c.filePath"
:component="c"
:relationships="relationships"
/>
</div>
</div>
</NSectionBlock>
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/client/pages/modules/components.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ definePageMeta({
const client = useClient()
const components = useComponents()
const relationships = useComponentsRelationships()
const { componentsView: view } = useDevToolsUIOptions()
Expand All @@ -29,6 +30,7 @@ function toggleView() {
<component
:is="view === 'list' ? ComponentsList : ComponentsGraph"
:components="components"
:relationships="relationships"
>
<div flex-none flex="~ gap3">
<NIconButton
Expand Down

0 comments on commit 7dbc3d1

Please sign in to comment.