Optimization, convert empty strings/arrays to null

This commit is contained in:
MeowcaTheoRange 2023-10-02 14:30:33 -05:00
parent 779c7480a7
commit 2d2699242c
14 changed files with 84 additions and 29 deletions

Binary file not shown.

View file

@ -59,7 +59,8 @@
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
font-family: "Renogare", "Poppins"; font-family: "Renogare", "Lexend";
/* font-weight: 700; */
color: white; color: white;
font-size: 28px; font-size: 28px;
line-height: 35px; line-height: 35px;

View file

@ -24,9 +24,7 @@ export default function ClanCard({
theme: clan.color ? Color3.fromRGB(...clan.color) : undefined theme: clan.color ? Color3.fromRGB(...clan.color) : undefined
}} }}
> >
<Conditional <Conditional condition={link && clan.bgimage != null}>
condition={link && clan.bgimage != null && clan.bgimage != ""}
>
<div className={styles.headerImage}> <div className={styles.headerImage}>
<img <img
src={clan.bgimage as string} src={clan.bgimage as string}
@ -35,7 +33,7 @@ export default function ClanCard({
</div> </div>
</Conditional> </Conditional>
<div className={styles.horizontal}> <div className={styles.horizontal}>
<Conditional condition={clan.pfp != null && clan.pfp != ""}> <Conditional condition={clan.pfp != null}>
<div className={styles.horizontalLeft}> <div className={styles.horizontalLeft}>
<img <img
src={clan.pfp as string} src={clan.pfp as string}
@ -64,11 +62,7 @@ export default function ClanCard({
{clan.displayName ?? clan.name} {clan.displayName ?? clan.name}
</ConditionalParent> </ConditionalParent>
</p> </p>
<Conditional <Conditional condition={clan.flairs != null}>
condition={
clan.flairs != null && clan.flairs.length > 0
}
>
<div className={globals.horizontalListLeft}> <div className={globals.horizontalListLeft}>
{clan.flairs?.map(flair => ( {clan.flairs?.map(flair => (
<FlairCard flair={flair} /> <FlairCard flair={flair} />
@ -89,7 +83,7 @@ export default function ClanCard({
.join(", ")} .join(", ")}
</span> </span>
</p> </p>
<Conditional condition={clan.url != null && clan.url != ""}> <Conditional condition={clan.url != null}>
<p className={globals.iconText}> <p className={globals.iconText}>
<span className={globals.iconSmall}>link</span> <span className={globals.iconSmall}>link</span>
<span className={globals.text}> <span className={globals.text}>
@ -102,7 +96,7 @@ export default function ClanCard({
</span> </span>
</p> </p>
</Conditional> </Conditional>
<Conditional condition={clan.description.length > 0}> <Conditional condition={clan.description != null}>
<p className={globals.iconText}> <p className={globals.iconText}>
<span className={globals.iconSmall}> <span className={globals.iconSmall}>
description description

View file

@ -32,7 +32,7 @@
drop-shadow(-1px 1px 0 currentcolor); drop-shadow(-1px 1px 0 currentcolor);
} }
.SignCard.Skeleton .topImagePlaceholder { .SignCard.Skeleton .topImagePlaceholder {
font-family: "Renogare", "Poppins", "Space Grotesk", "Fira Code", font-family: "Renogare", "Lexend", "Space Grotesk", "Fira Code",
"Courier New", monospace; "Courier New", monospace;
font-size: 52px; font-size: 52px;
width: 52px; width: 52px;

View file

@ -63,6 +63,15 @@ export default function Credits() {
</Link>{" "} </Link>{" "}
font by Indian Type Foundry. font by Indian Type Foundry.
</span> </span>
<span className={globals.blockText}>
<Link
className={globals.link}
href="https://fonts.google.com/specimen/Lexend+Deca"
>
Lexend (Deca)
</Link>{" "}
font by Superunion.
</span>
<span className={globals.blockText}> <span className={globals.blockText}>
<Link <Link
className={globals.link} className={globals.link}

View file

@ -3,7 +3,7 @@ import { getSingleClan } from "../clan";
import { ServerClanToClientClan } from "../convert/clan"; import { ServerClanToClientClan } from "../convert/clan";
import { ServerFlairToClientFlair } from "../convert/flair"; import { ServerFlairToClientFlair } from "../convert/flair";
import { getManyFlairs } from "../flair"; import { getManyFlairs } from "../flair";
import { cutArray } from "../utility/merge"; import { cutArray, cutObjectBlank } from "../utility/merge";
export async function ClanGET( export async function ClanGET(
query?: Partial<{ query?: Partial<{
@ -24,5 +24,5 @@ export async function ClanGET(
ServerFlairToClientFlair ServerFlairToClientFlair
) )
); );
return serverClan as ClientClan; return cutObjectBlank(serverClan) as ClientClan;
} }

View file

@ -3,6 +3,7 @@ import { ClientMessage } from "@/types/message";
import { getSingleClan } from "../clan"; import { getSingleClan } from "../clan";
import { ServerMessageToClientMessage } from "../convert/message"; import { ServerMessageToClientMessage } from "../convert/message";
import { getSingleMessage } from "../message"; import { getSingleMessage } from "../message";
import { cutObjectBlank } from "../utility/merge";
import { ClanGET } from "./clan"; import { ClanGET } from "./clan";
export async function MessageGET( export async function MessageGET(
@ -33,5 +34,5 @@ export async function MessageGET(
from: fullClan from: fullClan
}; };
// we know this is not null, as we passed in our own clan // we know this is not null, as we passed in our own clan
return endMessage; return cutObjectBlank(endMessage);
} }

View file

@ -5,7 +5,7 @@ import { ServerFlairToClientFlair } from "../convert/flair";
import { ServerTrollToClientTroll } from "../convert/troll"; import { ServerTrollToClientTroll } from "../convert/troll";
import { getManyFlairs } from "../flair"; import { getManyFlairs } from "../flair";
import { getSingleTroll } from "../troll"; import { getSingleTroll } from "../troll";
import { cutArray } from "../utility/merge"; import { cutArray, cutObjectBlank } from "../utility/merge";
import { ClanGET } from "./clan"; import { ClanGET } from "./clan";
export async function TrollGET( export async function TrollGET(
@ -39,5 +39,5 @@ export async function TrollGET(
); );
// we know this is not null, as we passed in our own clan // we know this is not null, as we passed in our own clan
serverTroll.owner = (await ClanGET(null, clan)) as ClientClan; serverTroll.owner = (await ClanGET(null, clan)) as ClientClan;
return serverTroll as ClientTroll; return cutObjectBlank(serverTroll) as ClientTroll;
} }

View file

@ -25,6 +25,40 @@ export function cutObject<T extends { [key: string]: any }>(object: T) {
return cut as T; return cut as T;
} }
export function cutArrayBlank<T>(array: T[]) {
const cut: any[] = [];
array.forEach((value, i) => {
if (value == null) return;
else if (
(typeof value == "string" || Array.isArray(value)) &&
value.length <= 0
)
cut[i] = undefined;
else if (Array.isArray(value)) cut[i] = cutArrayBlank(value);
else if (typeof value == "object") cut[i] = cutObjectBlank(value);
else cut[i] = value;
});
return cut as T[];
}
export function cutObjectBlank<T extends { [key: string]: any }>(object: T) {
const keys = Object.keys(object);
let cut: { [key: string]: any } = {};
keys.forEach(key => {
var val = object[key];
if (val == null) return;
else if (
(typeof val == "string" || Array.isArray(val)) &&
val.length <= 0
)
cut[key] = undefined;
else if (Array.isArray(val)) cut[key] = cutArrayBlank(val);
else if (typeof val == "object") cut[key] = cutObjectBlank(val);
else cut[key] = val;
});
return cut as T;
}
export function sanitize<Type extends WithId<{}>>( export function sanitize<Type extends WithId<{}>>(
serverType: Type serverType: Type
): Omit<Type, "_id"> { ): Omit<Type, "_id"> {

View file

@ -1,6 +1,7 @@
import { ClanGET } from "@/lib/trollcall/api/clan"; import { ClanGET } from "@/lib/trollcall/api/clan";
import { getManyPagedClans } from "@/lib/trollcall/clan"; import { getManyPagedClans } from "@/lib/trollcall/clan";
import { ServerClanToClientClan } from "@/lib/trollcall/convert/clan"; import { ServerClanToClientClan } from "@/lib/trollcall/convert/clan";
import { cutObjectBlank } from "@/lib/trollcall/utility/merge";
import { ClientClan } from "@/types/clan"; import { ClientClan } from "@/types/clan";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
@ -22,7 +23,7 @@ export default async function handler(
// ServerFlairToClientFlair // ServerFlairToClientFlair
// ) // )
// ); // );
return thisClan; return cutObjectBlank(thisClan);
}, },
5, 5,
page page

View file

@ -2,6 +2,7 @@ import { ClanGET } from "@/lib/trollcall/api/clan";
import { getSingleClan } from "@/lib/trollcall/clan"; import { getSingleClan } from "@/lib/trollcall/clan";
import { ServerMessageToClientMessage } from "@/lib/trollcall/convert/message"; import { ServerMessageToClientMessage } from "@/lib/trollcall/convert/message";
import { getManyPagedMessages } from "@/lib/trollcall/message"; import { getManyPagedMessages } from "@/lib/trollcall/message";
import { cutObjectBlank } from "@/lib/trollcall/utility/merge";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
export default async function handler( export default async function handler(
@ -40,7 +41,7 @@ export default async function handler(
} }
thisMessage.from = clientFrom; thisMessage.from = clientFrom;
return thisMessage; return cutObjectBlank(thisMessage);
}, },
5, 5,
page page

View file

@ -3,7 +3,7 @@ import { ServerFlairToClientFlair } from "@/lib/trollcall/convert/flair";
import { ServerTrollToClientTroll } from "@/lib/trollcall/convert/troll"; import { ServerTrollToClientTroll } from "@/lib/trollcall/convert/troll";
import { getManyFlairs } from "@/lib/trollcall/flair"; import { getManyFlairs } from "@/lib/trollcall/flair";
import { getManyPagedTrolls } from "@/lib/trollcall/troll"; import { getManyPagedTrolls } from "@/lib/trollcall/troll";
import { cutArray } from "@/lib/trollcall/utility/merge"; import { cutArray, cutObjectBlank } from "@/lib/trollcall/utility/merge";
import { ClientClan } from "@/types/clan"; import { ClientClan } from "@/types/clan";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
@ -14,13 +14,23 @@ export default async function handler(
const { method, query } = req; const { method, query } = req;
const page = query.page ? query.page[0] : 0; const page = query.page ? query.page[0] : 0;
if (method === "GET") { if (method === "GET") {
const owners: { [key: string]: any } = {};
const trolls = await getManyPagedTrolls( const trolls = await getManyPagedTrolls(
{}, {},
async (troll: any) => { async (troll: any) => {
const thisTroll = await ServerTrollToClientTroll(troll); const thisTroll = await ServerTrollToClientTroll(troll);
thisTroll.owner = (await ClanGET({ let clientOwner;
_id: troll.owner if (owners[troll.owner.toString()] != null)
})) as ClientClan; clientOwner = owners[troll.owner.toString()];
else {
clientOwner = (await ClanGET({
_id: troll.owner
})) as ClientClan;
if (clientOwner == null) return res.status(404).end();
owners[troll.owner.toString()] = clientOwner;
}
thisTroll.owner = clientOwner;
if (troll.flairs != null) if (troll.flairs != null)
thisTroll.flairs = cutArray( thisTroll.flairs = cutArray(
await getManyFlairs( await getManyFlairs(
@ -28,8 +38,7 @@ export default async function handler(
ServerFlairToClientFlair ServerFlairToClientFlair
) )
); );
return cutObjectBlank(thisTroll);
return thisTroll;
}, },
5, 5,
page page

View file

@ -4,7 +4,7 @@ import { ServerFlairToClientFlair } from "@/lib/trollcall/convert/flair";
import { ServerTrollToClientTroll } from "@/lib/trollcall/convert/troll"; import { ServerTrollToClientTroll } from "@/lib/trollcall/convert/troll";
import { getManyFlairs } from "@/lib/trollcall/flair"; import { getManyFlairs } from "@/lib/trollcall/flair";
import { getManyPagedTrolls } from "@/lib/trollcall/troll"; import { getManyPagedTrolls } from "@/lib/trollcall/troll";
import { cutArray } from "@/lib/trollcall/utility/merge"; import { cutArray, cutObjectBlank } from "@/lib/trollcall/utility/merge";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
export default async function handler( export default async function handler(
@ -35,7 +35,7 @@ export default async function handler(
) )
); );
return thisTroll; return cutObjectBlank(thisTroll);
}, },
5, 5,
page page

View file

@ -8,7 +8,7 @@
format("opentype"); format("opentype");
} }
/* ANY usage of the Renogare font should also specify a fallback! Try the Poppins font. */ /* ANY usage of the Renogare font should also specify a fallback! Try the Lexend font. */
/* @font-face { /* @font-face {
font-family: "Renogare"; font-family: "Renogare";
src: url("../../public/fonts/Renogare/Renogare.woff") format("woff"), src: url("../../public/fonts/Renogare/Renogare.woff") format("woff"),
@ -21,6 +21,11 @@
src: url("../../public/fonts/Poppins/Poppins.ttf") format("truetype"); src: url("../../public/fonts/Poppins/Poppins.ttf") format("truetype");
} }
@font-face {
font-family: "Lexend";
src: url("../../public/fonts/Lexend/Lexend.ttf") format("truetype");
}
@font-face { @font-face {
font-family: "Flow Circular"; font-family: "Flow Circular";
src: url("../../public/fonts/FlowCircular/FlowCircular.ttf") src: url("../../public/fonts/FlowCircular/FlowCircular.ttf")