re-compute stack

This commit is contained in:
Seth Trowbridge 2023-05-13 23:21:22 -04:00
parent 260c156b8f
commit b51e469632
2 changed files with 56 additions and 36 deletions

View File

@ -20,7 +20,7 @@ type MetaKeys = keyof Meta;
export const Meta =
{
Stack:[] as Array<MetasStackItem>,
Stack:[] as MetasStackItem[],
Meta: {
title:"",
description:"",
@ -28,6 +28,47 @@ export const Meta =
image:"",
canonical:""
} as Meta,
ComputeFinal(inStack:MetasStackItem[], inStart=0)
{
const seed = {
title:"",
description:"",
keywords:"",
image:"",
canonical:""
};
if(inStack.length>0)
{
let final = {...seed, ...inStack[0]};
for(let i=inStart+1; i<inStack.length; i++)
{
const curr = inStack[i];
Object.keys(seed).forEach(key=>
{
const lookup = key as MetaKeys
const valPrev = final[lookup];
const valCurr = curr[lookup];
if(valPrev && valCurr && curr.concatListed)
{
final[lookup] = valPrev + curr.concatListed + valCurr;
}
else if(!valCurr && curr.dropUnlisted)
{
final[lookup] = "";
}
else
{
final[lookup] = valCurr||"";
}
});
}
return final;
}
else
{
return seed;
}
},
Context: React.createContext([[], ()=>{}] as [Get:MetasStackItem[], Set:React.StateUpdater<MetasStackItem[]>]),
Provider({children}:{children:Children})
{
@ -47,24 +88,19 @@ export const Meta =
};
}, []);
React.useEffect(()=>{
const stack = binding[0];
const last = stack[stack.length-1];
console.log("updating page title", stack);
if(last && refElements.current)
React.useEffect(()=>
{
refElements.current.url.forEach(e=>e.setAttribute("content", last.canonical||""));
if(refElements.current)
{
const final = Meta.ComputeFinal(binding[0]);
document.title = last.title||"";
refElements.current.title.forEach(e=>e.setAttribute("content", last.title||""));
refElements.current.image.forEach(e=>e.setAttribute("content", last.image||""));
refElements.current.description.forEach(e=>e.setAttribute("content", last.description||""));
refElements.current.url.forEach(e=>e.setAttribute("content", final.canonical||""));
document.title = final.title;
refElements.current.title.forEach(e=>e.setAttribute("content", final.title||""));
refElements.current.image.forEach(e=>e.setAttribute("content", final.image||""));
refElements.current.description.forEach(e=>e.setAttribute("content", final.description||""));
}
})
});
return <Meta.Context.Provider value={binding}>{children}</Meta.Context.Provider>;
},
Metas({concatListed=undefined, dropUnlisted=false, ...props}:MetasModeArgs&MetasInputs):null
@ -85,24 +121,6 @@ export const Meta =
break;
}
}
const previous = clone[i];
if(previous)
{
Object.keys(Meta.Meta).forEach(key=>
{
const lookup = key as MetaKeys
const valPrev = previous[lookup];
const valCurr = props[lookup];
if(valPrev && valCurr && concatListed)
{
props[lookup] = valPrev + concatListed + valCurr;
}
if(!valCurr)
{
props[lookup] = dropUnlisted ? "" : valPrev;
}
});
}
clone.splice(i+1, 0, {id, depth, concatListed, dropUnlisted, ...props});
return clone;
});
@ -137,7 +155,7 @@ export const Meta =
if(!window.innerWidth && props.title)
{
Meta.Meta.title = props.title;
Meta.Stack.push({id, depth, concatListed, dropUnlisted, ...props});
}
return null;

View File

@ -393,6 +393,8 @@ else if(App && TwindInst)
});
Iso.Fetch.ServerTouched = false;
Iso.Meta.Meta = Iso.Meta.ComputeFinal(Iso.Meta.Stack);
const results = Twind.extract(bake, TwindInst);
type = `text/html`;