www-new/utils.ts

89 lines
2.1 KiB
TypeScript
Raw Normal View History

Warning Header (Closes #205) (#394) Update: In order to solve the issues discussed below, we decided to put the warnings in a json file, since they can be easily imported into a javascript file and webpack automatically bundles them with the client side app, so no static props is needed. __________ To get the warning data, since it is something that needs to be on all pages, it makes sense for the component to go in the app.js file (similar to the nav bar). However, next.js has a cumbersome issue that it does not support getStaticProps in the app.ts file (and getStaticProps only works in page files), thus we have no way of requesting the warning data easily in the \<WarningHeader\> Component. (https://github.com/vercel/next.js/discussions/10949) Here is my solution: - Request the warning data on the client side through the use of an api. - I made a warning api (/api/currentWarning) which sends a json of the current warning - Advantage: warnings will always be up to date since it is recalculated on every request - Advantage: It can potentially incorporate with other CSC services who might need to know about the warning (eg linktree?) - Disadvantage: can get expensive if we have a lot of warnings, to fix this I can cache the current warning and only re-read the files every 24h if this is a problem, but if we don’t have that many warnings I think it should be fine - Disadvantage: listed below: The current problem with what I implemented is that it doesn’t build in the production environment: - The way we build the website, when we call “next export” that disables any api endpoints. - According to this https://github.com/vercel/vercel/discussions/6551, if we want to allow api endpoints, we have to just do “next build”. This has implications that the site won’t be completely static anymore, but the other solutions also have similar problems ( though the GitHub post says that next will still optimize for static with only next build). Another advantage of allowing api’s is that maybe in the future we will implement other features where an api might be useful. If you want to try it out, my branch works fine locally (when just running npm run dev or npm run build”) Other possible solutions: - Use getInitialProps inside _app.js, but this has the downside that it will “disable Automatic Static Optimization in pages without Static Generation.” (https://nextjs.org/docs/advanced-features/custom-app), which I believe will slow the whole site down, but, we will be able to still use the old build command I think, I am not sure of the full effect of this. - However, implementing this would also be relatively simple. - Add the warning request to the getStaticProps of every single page, possibly through some wrapper component around every single page, this has the disadvantage that we need to do a lot of refactoring of all the pages of the site and cant use the intended “_app.ts” wrapper. - Add warning header only to the homepage (or maybe one or two other important pages). Co-authored-by: shahanneda <shahan.neda@gmail.com> Reviewed-on: https://git.csclub.uwaterloo.ca/www/www-new/pulls/394 Reviewed-by: Amy <a258wang@csclub.uwaterloo.ca>
2022-02-22 23:29:53 -05:00
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
export const TERMS = ["winter", "spring", "fall"] as const;
export type Term = typeof TERMS[number];
Warning Header (Closes #205) (#394) Update: In order to solve the issues discussed below, we decided to put the warnings in a json file, since they can be easily imported into a javascript file and webpack automatically bundles them with the client side app, so no static props is needed. __________ To get the warning data, since it is something that needs to be on all pages, it makes sense for the component to go in the app.js file (similar to the nav bar). However, next.js has a cumbersome issue that it does not support getStaticProps in the app.ts file (and getStaticProps only works in page files), thus we have no way of requesting the warning data easily in the \<WarningHeader\> Component. (https://github.com/vercel/next.js/discussions/10949) Here is my solution: - Request the warning data on the client side through the use of an api. - I made a warning api (/api/currentWarning) which sends a json of the current warning - Advantage: warnings will always be up to date since it is recalculated on every request - Advantage: It can potentially incorporate with other CSC services who might need to know about the warning (eg linktree?) - Disadvantage: can get expensive if we have a lot of warnings, to fix this I can cache the current warning and only re-read the files every 24h if this is a problem, but if we don’t have that many warnings I think it should be fine - Disadvantage: listed below: The current problem with what I implemented is that it doesn’t build in the production environment: - The way we build the website, when we call “next export” that disables any api endpoints. - According to this https://github.com/vercel/vercel/discussions/6551, if we want to allow api endpoints, we have to just do “next build”. This has implications that the site won’t be completely static anymore, but the other solutions also have similar problems ( though the GitHub post says that next will still optimize for static with only next build). Another advantage of allowing api’s is that maybe in the future we will implement other features where an api might be useful. If you want to try it out, my branch works fine locally (when just running npm run dev or npm run build”) Other possible solutions: - Use getInitialProps inside _app.js, but this has the downside that it will “disable Automatic Static Optimization in pages without Static Generation.” (https://nextjs.org/docs/advanced-features/custom-app), which I believe will slow the whole site down, but, we will be able to still use the old build command I think, I am not sure of the full effect of this. - However, implementing this would also be relatively simple. - Add the warning request to the getStaticProps of every single page, possibly through some wrapper component around every single page, this has the disadvantage that we need to do a lot of refactoring of all the pages of the site and cant use the intended “_app.ts” wrapper. - Add warning header only to the homepage (or maybe one or two other important pages). Co-authored-by: shahanneda <shahan.neda@gmail.com> Reviewed-on: https://git.csclub.uwaterloo.ca/www/www-new/pulls/394 Reviewed-by: Amy <a258wang@csclub.uwaterloo.ca>
2022-02-22 23:29:53 -05:00
export const DATE_FORMAT = "MMMM dd yyyy HH:mm";
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
export function isTerm(x: string): x is Term {
return TERMS.some((term) => x === term);
}
export function capitalize(str: string) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}
Warning Header (Closes #205) (#394) Update: In order to solve the issues discussed below, we decided to put the warnings in a json file, since they can be easily imported into a javascript file and webpack automatically bundles them with the client side app, so no static props is needed. __________ To get the warning data, since it is something that needs to be on all pages, it makes sense for the component to go in the app.js file (similar to the nav bar). However, next.js has a cumbersome issue that it does not support getStaticProps in the app.ts file (and getStaticProps only works in page files), thus we have no way of requesting the warning data easily in the \<WarningHeader\> Component. (https://github.com/vercel/next.js/discussions/10949) Here is my solution: - Request the warning data on the client side through the use of an api. - I made a warning api (/api/currentWarning) which sends a json of the current warning - Advantage: warnings will always be up to date since it is recalculated on every request - Advantage: It can potentially incorporate with other CSC services who might need to know about the warning (eg linktree?) - Disadvantage: can get expensive if we have a lot of warnings, to fix this I can cache the current warning and only re-read the files every 24h if this is a problem, but if we don’t have that many warnings I think it should be fine - Disadvantage: listed below: The current problem with what I implemented is that it doesn’t build in the production environment: - The way we build the website, when we call “next export” that disables any api endpoints. - According to this https://github.com/vercel/vercel/discussions/6551, if we want to allow api endpoints, we have to just do “next build”. This has implications that the site won’t be completely static anymore, but the other solutions also have similar problems ( though the GitHub post says that next will still optimize for static with only next build). Another advantage of allowing api’s is that maybe in the future we will implement other features where an api might be useful. If you want to try it out, my branch works fine locally (when just running npm run dev or npm run build”) Other possible solutions: - Use getInitialProps inside _app.js, but this has the downside that it will “disable Automatic Static Optimization in pages without Static Generation.” (https://nextjs.org/docs/advanced-features/custom-app), which I believe will slow the whole site down, but, we will be able to still use the old build command I think, I am not sure of the full effect of this. - However, implementing this would also be relatively simple. - Add the warning request to the getStaticProps of every single page, possibly through some wrapper component around every single page, this has the disadvantage that we need to do a lot of refactoring of all the pages of the site and cant use the intended “_app.ts” wrapper. - Add warning header only to the homepage (or maybe one or two other important pages). Co-authored-by: shahanneda <shahan.neda@gmail.com> Reviewed-on: https://git.csclub.uwaterloo.ca/www/www-new/pulls/394 Reviewed-by: Amy <a258wang@csclub.uwaterloo.ca>
2022-02-22 23:29:53 -05:00
// Converts a date to local time
export function getLocalDateFromEST(date: Date): Date {
return utcToZonedTime(
// The date parameter is in EST
zonedTimeToUtc(date, "America/Toronto"),
Intl.DateTimeFormat().resolvedOptions().timeZone
);
}
export interface TermYear {
term: Term;
year: number;
}
export interface GetTermYearOptions {
goBackwards?: boolean;
skipCurrent?: boolean;
}
export function* getTermYear(
start?: number | TermYear,
{ goBackwards = false, skipCurrent = false }: GetTermYearOptions = {}
) {
const allTerms = [...TERMS];
if (goBackwards) {
allTerms.reverse();
}
const today = new Date();
const todayYear = today.getFullYear();
const todayTerm = TERMS[Math.trunc(today.getMonth() / 4)];
start ??= { term: todayTerm, year: todayYear };
if (typeof start === "number") {
start = { term: allTerms[0], year: start };
}
let currentYear = start.year;
while (0 <= currentYear && currentYear <= Number.MAX_SAFE_INTEGER) {
for (const currentTerm of allTerms) {
if (
currentYear === start.year &&
allTerms.indexOf(currentTerm) < allTerms.indexOf(start.term)
) {
continue;
}
if (
skipCurrent &&
currentYear === start.year &&
currentTerm === start.term
) {
continue;
}
yield { term: currentTerm, year: currentYear };
}
currentYear = currentYear + (goBackwards ? -1 : 1);
}
}
export function getCurrentTermYear() {
const result = getTermYear().next();
if (result.done === true) {
throw new Error("Cannot get current term. Iterator is done.");
}
return result.value;
}