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;
align-items: center;
justify-content: center;
font-family: "Renogare", "Poppins";
font-family: "Renogare", "Lexend";
/* font-weight: 700; */
color: white;
font-size: 28px;
line-height: 35px;

View file

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

View file

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

View file

@ -63,6 +63,15 @@ export default function Credits() {
</Link>{" "}
font by Indian Type Foundry.
</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}>
<Link
className={globals.link}

View file

@ -3,7 +3,7 @@ import { getSingleClan } from "../clan";
import { ServerClanToClientClan } from "../convert/clan";
import { ServerFlairToClientFlair } from "../convert/flair";
import { getManyFlairs } from "../flair";
import { cutArray } from "../utility/merge";
import { cutArray, cutObjectBlank } from "../utility/merge";
export async function ClanGET(
query?: Partial<{
@ -24,5 +24,5 @@ export async function ClanGET(
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 { ServerMessageToClientMessage } from "../convert/message";
import { getSingleMessage } from "../message";
import { cutObjectBlank } from "../utility/merge";
import { ClanGET } from "./clan";
export async function MessageGET(
@ -33,5 +34,5 @@ export async function MessageGET(
from: fullClan
};
// 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 { getManyFlairs } from "../flair";
import { getSingleTroll } from "../troll";
import { cutArray } from "../utility/merge";
import { cutArray, cutObjectBlank } from "../utility/merge";
import { ClanGET } from "./clan";
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
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;
}
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<{}>>(
serverType: Type
): Omit<Type, "_id"> {

View file

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

View file

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

View file

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

View file

@ -4,7 +4,7 @@ import { ServerFlairToClientFlair } from "@/lib/trollcall/convert/flair";
import { ServerTrollToClientTroll } from "@/lib/trollcall/convert/troll";
import { getManyFlairs } from "@/lib/trollcall/flair";
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";
export default async function handler(
@ -35,7 +35,7 @@ export default async function handler(
)
);
return thisTroll;
return cutObjectBlank(thisTroll);
},
5,
page

View file

@ -8,7 +8,7 @@
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-family: "Renogare";
src: url("../../public/fonts/Renogare/Renogare.woff") format("woff"),
@ -21,6 +21,11 @@
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-family: "Flow Circular";
src: url("../../public/fonts/FlowCircular/FlowCircular.ttf")