Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache cms #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts


.cache/*
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

A file `translations.json` is downloaded from the CMS every time you run a command. This allows Typescript to use it's type data for autocompleting and build-time verifying translation keys.

## CMS cache

On the first run, the project fetches data from the CMS. This data is stored in `.cache`, which is used for subsequent fetches. The cache TTL is 1 hour.
When the `Translation` collection is fetched, a type file is also generated in the cache directory, which holds the type of the translation keys. If the type file does not exist, the translation key type is inferred as `string`. For example

```
const t = useTranslate();
const string = t("doesnt:exist"); // does not fail if there is no type file in the cache
```

## License

This project contains both code and content. The code of the website is licensed under GPLv3. By content we mean text, image, logos or designs specific to the Satakunta Nation. The content is proprietary. In other words, you can freely use the technical implementation (code, config files) of this website for your purposes, but make your own content.
2 changes: 1 addition & 1 deletion components/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable react-hooks/exhaustive-deps */
import useTranslate from "@/hooks/useTranslate";
import { EmblaCarouselType, EmblaOptionsType } from "embla-carousel";
import useEmblaCarousel from "embla-carousel-react";
import Image from "next/image";
import Link from "next/link";
import React, { useEffect } from "react";
import { useTranslate } from "@/hooks/TranslationContext";
import { NextButton, PrevButton, usePrevNextButtons } from "./CarouselArrows";

type PropType = {
Expand Down
2 changes: 1 addition & 1 deletion components/ContactTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Contact } from "@/lib/cmsClient";
import useTranslate from "@/hooks/useTranslate";
import {
TableContainer,
Table,
Expand All @@ -12,6 +11,7 @@ import {
} from "@mui/material";
import styles from "@/styles/contactTable.module.css";
import { useMemo, useState } from "react";
import { useTranslate } from "@/hooks/TranslationContext";

export type ContactTableProps = {
contactData: Contact[];
Expand Down
2 changes: 1 addition & 1 deletion components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import { useState } from "react";
import useTranslate from "@/hooks/useTranslate";
import { NavigationLink } from "@/lib/cmsClient";
import { useTranslate } from "@/hooks/TranslationContext";
import close from "../public/close.svg";
import menu from "../public/menu.svg";

Expand Down
34 changes: 0 additions & 34 deletions fetchTranslations.ts

This file was deleted.

47 changes: 47 additions & 0 deletions hooks/TranslationContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Translation, TranslationKey } from "@/lib/cmsClient";
import { createContext, useContext } from "react";
import { Language, useLanguage } from "../lib/LanguageContext";

export const TranslationContext = createContext<Translation[] | null>(null);

type TranslationProviderProps = {
translations: Translation[];
children: React.ReactNode;
};

export const TranslationProvider = ({
translations,
children,
}: TranslationProviderProps) => (
<TranslationContext.Provider value={translations}>
{children}
</TranslationContext.Provider>
);

const translate = (
translations: Translation[],
key: TranslationKey,
language: Language,
): string => {
const translation = translations.find((t) => t.key === key);

if (translation === undefined) {
throw new Error(
`Could not find translation ${key} (see that it exists in the CMS)`,
);
}

return translation[language];
};

export const useTranslate = () => {
const { language } = useLanguage();
const translations = useContext(TranslationContext);
if (translations === null) {
throw new Error(
"useTranslate failed, make sure TranslationProvider is set",
);
}
return (key: TranslationKey, languageOverride?: Language) =>
translate(translations, key, languageOverride ?? language);
};
Loading
Loading