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

View File

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