-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderToolsIndex.js
More file actions
189 lines (175 loc) · 6.43 KB
/
renderToolsIndex.js
File metadata and controls
189 lines (175 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { getToolRegistry } from "./toolRegistry.js";
import { escapeHtml } from "../src/shared/string/stringUtil.js";
const SAMPLES_INDEX_PATH = "/samples/index.html";
const SAMPLES_METADATA_PATH = "/samples/metadata/samples.index.metadata.json";
function toStandaloneHref(entryPoint) {
const normalized = String(entryPoint || "").replace(/^\.?\/*/, "");
return normalized ? `/tools/${normalized}` : "#";
}
function buildDocumentationLinks(tool) {
const folder = String(tool.folderName || tool.path || "").trim();
if (!folder) {
return [];
}
return [
{ label: "How To Use", path: `${folder}/how_to_use.html` },
{ label: "Read Me", path: `${folder}/README.md` }
];
}
function buildCardLinks(tool, sampleCount) {
const docs = buildDocumentationLinks(tool);
const links = [...docs];
if (Number.isInteger(sampleCount) && sampleCount > 0) {
links.push({
label: `Samples (${sampleCount})`,
path: `${SAMPLES_INDEX_PATH}?tool=${encodeURIComponent(tool.id)}`
});
}
const seen = new Set();
return links.filter((entry) => {
const label = String(entry?.label || "").trim();
const path = String(entry?.path || "").trim();
if (!label || !path) {
return false;
}
const key = `${label.toLowerCase()}|${path.toLowerCase()}`;
if (seen.has(key)) {
return false;
}
seen.add(key);
return true;
});
}
function renderToolCard(tool, sampleCountByToolId) {
const standaloneHref = toStandaloneHref(tool.entryPoint);
const sampleCount = Number(sampleCountByToolId.get(tool.id) || 0);
const cardLinks = buildCardLinks(tool, sampleCount);
const sampleLinks = cardLinks.length > 0
? `
<div class="meta">
${cardLinks.map((entry) => `
<a class="tools-platform-card__action tools-platform-card__action--secondary" href="${escapeHtml(entry.path)}">${escapeHtml(entry.label)}</a>
`).join("")}
</div>
`
: "";
return `
<div class="card tools-platform-card">
<div class="meta">
<span class="pill live">${escapeHtml(tool.showcaseTag || "Active Tool")}</span>
<span class="pill planned">${escapeHtml(tool.showcaseStatus || "Engine Theme")}</span>
</div>
<h3><a href="${escapeHtml(standaloneHref)}">${escapeHtml(tool.displayName)}</a></h3>
<p>${escapeHtml(tool.description)}</p>
${sampleLinks}
</div>
`;
}
function classifyToolGroup(toolId) {
const workflowToolIds = new Set([
"workspace-manager-v2"
]);
const viewerToolIds = new Set([
"3d-asset-viewer",
"replay-visualizer",
"performance-profiler"
]);
const utilityToolIds = new Set([
"asset-browser",
"asset-manager-v2",
"asset-pipeline",
"tile-model-converter",
"physics-sandbox",
"3d-json-payload"
]);
if (workflowToolIds.has(toolId)) {
return "workflow";
}
if (viewerToolIds.has(toolId)) {
return "viewers";
}
if (utilityToolIds.has(toolId)) {
return "utilities";
}
return "editors";
}
async function loadSampleCountByToolId() {
const counts = new Map();
try {
const response = await fetch(SAMPLES_METADATA_PATH, { cache: "no-store" });
if (!response.ok) {
return counts;
}
const metadata = await response.json();
const samples = Array.isArray(metadata?.samples) ? metadata.samples : [];
for (const sample of samples) {
if (String(sample?.phase || "").trim() === "20") {
continue;
}
const hintedToolIds = new Set(
(Array.isArray(sample?.toolHints) ? sample.toolHints : [])
.map((toolId) => String(toolId || "").trim().toLowerCase())
.filter(Boolean)
);
const presets = Array.isArray(sample?.roundtripToolPresets) ? sample.roundtripToolPresets : [];
const countedToolIds = new Set();
for (const preset of presets) {
const toolId = String(preset?.toolId || "").trim().toLowerCase();
const presetPath = String(preset?.presetPath || "").trim();
if (!toolId) {
continue;
}
if (!presetPath || !hintedToolIds.has(toolId) || countedToolIds.has(toolId)) {
continue;
}
countedToolIds.add(toolId);
counts.set(toolId, Number(counts.get(toolId) || 0) + 1);
}
}
return counts;
} catch {
return counts;
}
}
function renderActiveToolsList(sampleCountByToolId) {
const workflowGrid = document.querySelector("[data-active-tools-workflow-grid]");
const editorsGrid = document.querySelector("[data-active-tools-editors-grid]");
const utilitiesGrid = document.querySelector("[data-active-tools-utilities-grid]");
const viewersGrid = document.querySelector("[data-active-tools-viewers-grid]");
if (!workflowGrid || !editorsGrid || !utilitiesGrid || !viewersGrid) {
return;
}
const tools = getToolRegistry()
.filter((entry) => entry.active === true)
.filter((entry) => entry.visibleInToolsList === true)
.filter((entry) => entry.id !== "state-inspector")
.sort((left, right) => String(left.displayName || "").localeCompare(String(right.displayName || "")));
const workflow = tools.filter((tool) => classifyToolGroup(tool.id) === "workflow").map((tool) => renderToolCard(tool, sampleCountByToolId));
const editors = tools.filter((tool) => classifyToolGroup(tool.id) === "editors").map((tool) => renderToolCard(tool, sampleCountByToolId));
const utilities = tools.filter((tool) => classifyToolGroup(tool.id) === "utilities").map((tool) => renderToolCard(tool, sampleCountByToolId));
const viewers = tools.filter((tool) => classifyToolGroup(tool.id) === "viewers").map((tool) => renderToolCard(tool, sampleCountByToolId));
workflowGrid.innerHTML = workflow.join("\n");
editorsGrid.innerHTML = editors.join("\n");
utilitiesGrid.innerHTML = utilities.join("\n");
viewersGrid.innerHTML = viewers.join("\n");
}
function sortPlannedCardsAlphabetically() {
const grid = document.querySelector("[data-planned-tools-grid]");
if (!grid) {
return;
}
const cards = Array.from(grid.querySelectorAll(".card"));
cards
.sort((left, right) => {
const leftName = left.querySelector("h3")?.textContent?.trim() || "";
const rightName = right.querySelector("h3")?.textContent?.trim() || "";
return leftName.localeCompare(rightName);
})
.forEach((card) => grid.appendChild(card));
}
async function initToolsIndex() {
const sampleCountByToolId = await loadSampleCountByToolId();
renderActiveToolsList(sampleCountByToolId);
sortPlannedCardsAlphabetically();
}
void initToolsIndex();