letprompt=`I have a React component structure where a Suspense boundary in "${suspenseComponent}" wraps dynamic API calls that are nested deep in the component tree. This causes more content than necessary to show loading states.\n\n`
prompt+='Current structure:\n'
for(constrofdeepReasons){
prompt+=`- ${r.expression}() is called ${r.layersBetween} layers deep: ${r.componentsBetween.join(' > ')}\n`
}
prompt+=
'\nPlease help me move the Suspense boundary closer to the dynamic API calls to minimize the loading state area. Show me the refactored code.'
function matchDynamicToBoundary(boundaries, dynamicAccesses) {
var boundaryReasons = {};
dynamicAccesses.forEach(function(api) {
if (!api.frames.length) return;
// Frames are leaf-to-root, reverse to get root-to-leaf order
var apiFrames = api.frames.slice().reverse();
var apiNames = apiFrames.map(function(f) { return f.componentName; });
// Find the boundary whose path is a prefix of the api path (nearest ancestor)
var bestMatch = null;
var bestMatchLen = 0;
var bestBoundaryLen = 0;
boundaries.forEach(function(b) {
if (!b.frames.length) return;
var boundaryFrames = b.frames.slice().reverse();
var boundaryNames = boundaryFrames.map(function(f) { return f.componentName; });
// Check if boundary components are a prefix of api components
if (boundaryNames.length > apiNames.length) return;
var isPrefix = true;
for (var i = 0; i < boundaryNames.length; i++) {
if (boundaryNames[i] !== apiNames[i]) {
isPrefix = false;
break;
}
}
if (isPrefix && boundaryNames.length > bestMatchLen) {
bestMatch = b.id;
bestMatchLen = boundaryNames.length;
bestBoundaryLen = boundaryNames.length;
}
});
if (bestMatch) {
// Calculate layers between
var layersBetween = apiNames.length - bestBoundaryLen;
var componentsBetween = apiNames.slice(bestBoundaryLen);
if (!boundaryReasons[bestMatch]) boundaryReasons[bestMatch] = [];
// Check if this expression is already tracked
var exists = false;
for (var i = 0; i < boundaryReasons[bestMatch].length; i++) {
if (boundaryReasons[bestMatch][i].expression === api.expression) {
exists = true;
break;
}
}
if (!exists) {
boundaryReasons[bestMatch].push({
expression: api.expression,
layersBetween: layersBetween,
componentsBetween: componentsBetween
});
}
}
});
return boundaryReasons;
}
// Generate optimization prompt for a suspense boundary with deep dynamic calls
function generatePrompt(suspenseComponent, reasons) {
var deepReasons = [];
for (var i = 0; i < reasons.length; i++) {
if (reasons[i].layersBetween > 0) deepReasons.push(reasons[i]);
}
if (deepReasons.length === 0) return null;
var nl = String.fromCharCode(10);
var prompt = 'I have a React component structure where a Suspense boundary in "' + suspenseComponent + '" wraps dynamic API calls that are nested deep in the component tree. This causes more content than necessary to show loading states.' + nl + nl;
prompt += nl + 'Please help me move the Suspense boundary closer to the dynamic API calls to minimize the loading state area. Show me the refactored code.';
var index = window.__sp.prompts.length;
window.__sp.prompts.push(prompt);
return index;
}
// Build tree from boundaries and dynamic accesses
function buildTree(boundaries, dynamicAccesses, boundaryReasons) {