Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions packages/ui/client/components/FileDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const draft = ref(false)
const hasGraphBeenDisplayed = ref(false)
const loadingModuleGraph = ref(false)
const currentFilepath = ref<string | undefined>(undefined)
const hideNodeModules = ref(true)

const graphData = computed(() => {
const c = current.value
Expand Down Expand Up @@ -61,10 +62,12 @@ function onDraft(value: boolean) {
draft.value = value
}

async function loadModuleGraph() {
const nodeModuleRegex = /[/\\]node_modules[/\\]/

async function loadModuleGraph(force = false) {
if (
loadingModuleGraph.value
|| graphData.value?.filepath === currentFilepath.value
|| (graphData.value?.filepath === currentFilepath.value && !force)
) {
return
}
Expand All @@ -76,20 +79,32 @@ async function loadModuleGraph() {
try {
const gd = graphData.value
if (!gd) {
loadingModuleGraph.value = false
return
}

if (
!currentFilepath.value
force
|| !currentFilepath.value
|| gd.filepath !== currentFilepath.value
|| (!graph.value.nodes.length && !graph.value.links.length)
) {
const moduleGraph = await client.rpc.getModuleGraph(
gd.projectName,
gd.filepath,
!!browserState,
)
// remove node_modules from the graph when enabled
if (hideNodeModules.value) {
moduleGraph.inlined = moduleGraph.inlined.filter(
n => !nodeModuleRegex.test(n),
)
moduleGraph.externalized = moduleGraph.externalized.filter(
n => !nodeModuleRegex.test(n),
)
}
graph.value = getModuleGraph(
await client.rpc.getModuleGraph(
gd.projectName,
gd.filepath,
!!browserState,
),
moduleGraph,
gd.filepath,
)
currentFilepath.value = gd.filepath
Expand All @@ -103,10 +118,11 @@ async function loadModuleGraph() {
}

debouncedWatch(
() => [graphData.value, viewMode.value] as const,
([, vm]) => {
() => [graphData.value, viewMode.value, hideNodeModules.value] as const,
([, vm, hide], old) => {
if (vm === 'graph') {
loadModuleGraph()
// only force reload when hide is changed
loadModuleGraph(old && hide !== old[2])
}
},
{ debounce: 100, immediate: true },
Expand Down Expand Up @@ -221,6 +237,7 @@ const projectNameTextColor = computed(() => {
<div v-if="hasGraphBeenDisplayed" :flex-1="viewMode === 'graph' && ''">
<ViewModuleGraph
v-show="viewMode === 'graph' && !loadingModuleGraph"
v-model="hideNodeModules"
:graph="graph"
data-testid="graph"
:project-name="current.file.projectName || ''"
Expand Down
39 changes: 36 additions & 3 deletions packages/ui/client/components/views/ViewModuleGraph.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const props = defineProps<{
projectName: string
}>()

const modelValue = defineModel<boolean>({ default: true })

const { graph } = toRefs(props)

const el = ref<HTMLDivElement>()
Expand All @@ -46,7 +48,7 @@ onUnmounted(() => {
controller.value?.shutdown()
})

watch(graph, resetGraphController)
watch(graph, () => resetGraphController())

function setFilter(name: ModuleType, value: boolean) {
controller.value?.filterNodesByType(value, name)
Expand All @@ -57,8 +59,16 @@ function setSelectedModule(id: string) {
modalShow.value = true
}

function resetGraphController() {
function resetGraphController(reset = false) {
controller.value?.shutdown()

// Force reload the module graph only when node_modules are shown.
// The module graph doesn't contain node_modules entries.
if (reset && !modelValue.value) {
modelValue.value = true
return
}

if (!graph.value || !el.value) {
return
}
Expand Down Expand Up @@ -154,6 +164,28 @@ function bindOnClick(
<div h-full min-h-75 flex-1 overflow="hidden">
<div>
<div flex items-center gap-4 px-3 py-2>
<div
flex="~ gap-1"
items-center
select-none
>
<input
id="hide-node_modules"
v-model="modelValue"
type="checkbox"
>
<label
font-light
text-sm
ws-nowrap
overflow-hidden
select-none
truncate
for="hide-node_modules"
border-b-2
border="$cm-namespace"
>Hide node_modules</label>
</div>
<div
v-for="node of controller?.nodeTypes.sort()"
:key="node"
Expand All @@ -173,6 +205,7 @@ function bindOnClick(
ws-nowrap
overflow-hidden
capitalize
select-none
truncate
:for="`type-${node}`"
border-b-2
Expand All @@ -184,7 +217,7 @@ function bindOnClick(
<IconButton
v-tooltip.bottom="'Reset'"
icon="i-carbon-reset"
@click="resetGraphController"
@click="resetGraphController(true)"
/>
</div>
</div>
Expand Down
Loading