Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/quiet-explorer-console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quartz-community/explorer": patch
---

Remove leftover debug `console.log` calls that fired on every navigation, and coalesce the `nav`/`render` event handlers so the file tree is rebuilt once per navigation instead of twice.
46 changes: 30 additions & 16 deletions src/components/scripts/explorer.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ function processTrie(trie, sortFn, filterFn, mapFn) {
// Build trie from content index data
async function buildFileTrie(dataFns) {
try {
console.log("[Explorer] Fetching content index...");
const data = await fetchData;
console.log("[Explorer] Fetched data keys:", Object.keys(data).slice(0, 5));

if (!data) {
console.error("[Explorer] No data received");
Expand All @@ -122,15 +120,12 @@ async function buildFileTrie(dataFns) {
const contentData = data.content || data;
const entries = Object.entries(contentData);

console.log("[Explorer] Entry count:", entries.length);

if (entries.length === 0) {
console.warn("[Explorer] No content entries found");
return null;
}

const trie = FileTrieNode.fromEntries(entries);
console.log("[Explorer] Trie root children:", trie.children.length);

// Parse data functions from string if provided
let sortFn = defaultSortFn;
Expand Down Expand Up @@ -237,10 +232,8 @@ function renderTree(node, container, currentSlug, folderBehavior, savedState, pa
async function handleNavOrRender(e) {
const thisGeneration = ++currentRenderGeneration;
try {
console.log("[Explorer] Nav event received, generation:", thisGeneration);
const currentSlug = (e.detail?.url || "").replace(/^\/+/, "");
const allExplorers = document.querySelectorAll("div.explorer");
console.log("[Explorer] Found", allExplorers.length, "explorers");

const savedState = {};
try {
Expand All @@ -267,22 +260,17 @@ async function handleNavOrRender(e) {
const folderBehavior = explorer.dataset.behavior || "collapse";

// Build and render the tree
console.log("[Explorer] Starting tree build...");
const trie = await buildFileTrie(dataFns);

// Check if another nav event started while we were fetching
if (thisGeneration === currentRenderGeneration) {
console.log("[Explorer] Render generation is current, rendering tree");
console.log("[Explorer] Trie result:", trie ? "success" : "null");
if (trie && trie.children && trie.children.length > 0) {
// Clear again before rendering to ensure clean state
explorerUl.innerHTML = '<li class="overflow-end"></li>';

console.log("[Explorer] Rendering", trie.children.length, "children");
for (const child of trie.children) {
renderTree(child, explorerUl, currentSlug, folderBehavior, savedState, "");
}
console.log("[Explorer] Render complete, final list length:", explorerUl.children.length);
} else {
console.warn("[Explorer] No trie or empty children");
}
Expand All @@ -297,8 +285,6 @@ async function handleNavOrRender(e) {
activeElement.scrollIntoView({ behavior: "smooth" });
}
}
} else {
console.log("[Explorer] Stale render generation, skipping tree render");
}

// Always set up event listeners, regardless of render generation
Expand Down Expand Up @@ -409,8 +395,36 @@ async function handleNavOrRender(e) {
}
}

document.addEventListener("nav", handleNavOrRender);
document.addEventListener("render", handleNavOrRender);
// "nav" and "render" can both fire for the same navigation (e.g. a plugin
// that dispatches "render" right after "nav" to signal a content-index
// update). Coalesce same-tick dispatches into a single tree rebuild instead
// of rebuilding once per event, while still keeping the most recent url.
let pendingDetail = null;
let isRenderScheduled = false;

function scheduleNavOrRender(e) {
const previousUrl = pendingDetail?.url;
pendingDetail = { ...(pendingDetail || {}), ...(e?.detail || {}) };
// Only fall back to the previous url if this event didn't carry one at
// all (e.g. "render" with no detail) — an explicit "" url (root page) is
// a real value and must not be discarded.
if (pendingDetail.url == null && previousUrl != null) {
pendingDetail.url = previousUrl;
}

if (!isRenderScheduled) {
isRenderScheduled = true;
queueMicrotask(() => {
const detail = pendingDetail;
isRenderScheduled = false;
pendingDetail = null;
handleNavOrRender({ detail });
});
}
}

document.addEventListener("nav", scheduleNavOrRender);
document.addEventListener("render", scheduleNavOrRender);

document.addEventListener("prenav", () => {
const explorer = document.querySelector(".explorer-ul");
Expand Down