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

website(bugfix): support static rendering, avoid cookies server side #617

Merged
merged 3 commits into from
Nov 9, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { CURRENCY_COOKIE, DefaultParams } from '@/app/[lang]/[region]';
import { DefaultParams } from '@/app/[lang]/[region]';
import { I18nDialog } from '@/components/i18n-dialog';
import { SILogo } from '@/components/logos/si-logo';
import { GlobeEuropeAfricaIcon, LanguageIcon } from '@heroicons/react/24/solid';
import { Translator } from '@socialincome/shared/src/utils/i18n';
import { BaseContainer, Button } from '@socialincome/ui';
import { cookies } from 'next/headers';
import { PropsWithChildren } from 'react';

export interface SurveyPageProps {
Expand All @@ -14,7 +13,7 @@ export interface SurveyPageProps {
} & DefaultParams;
}

export default async function Layout({ children, params: { lang, region } }: PropsWithChildren<SurveyPageProps>) {
export default async function Layout({ children, params: { lang } }: PropsWithChildren<SurveyPageProps>) {
const translator = await Translator.getInstance({
language: lang,
namespaces: ['common', 'website-common', 'website-me'],
Expand All @@ -34,11 +33,8 @@ export default async function Layout({ children, params: { lang, region } }: Pro
currencies={[]}
translations={{
language: translator.t('language'),
currentLanguage: translator.t(`languages.${lang}`),
region: translator.t('region'),
currentRegion: translator.t(`regions.${region}`),
currency: translator.t('currency'),
currentCurrency: translator.t(`currencies.${cookies().get(CURRENCY_COOKIE)}`),
}}
>
<Button variant="ghost" className="flex max-w-md space-x-2">
Expand Down
1 change: 0 additions & 1 deletion website/src/app/[lang]/[region]/(website)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default async function Page(props: DefaultPageProps) {
requiredField: translator.t('login.required-field'),
invalidEmail: translator.t('login.invalid-email'),
submitButton: translator.t('login.submit-button'),

unknownUser: translator.t('login.unknown-user'),
wrongPassword: translator.t('login.wrong-password'),
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useI18n } from '@/app/providers/i18n-provider-client';
import { useI18n } from '@/app/context-providers';
import { WebsiteCurrency } from '@/i18n';
import { redirect } from 'next/navigation';
import { useEffect } from 'react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useI18n } from '@/app/providers/i18n-provider-client';
import { useI18n } from '@/app/context-providers';
import { redirect } from 'next/navigation';
import { useEffect } from 'react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use client';

import { CURRENCY_COOKIE, LANGUAGE_COOKIE, REGION_COOKIE } from '@/app/[lang]/[region]';
import { useCookieState } from '@/hooks/useCookieState';
import { WebsiteCurrency, WebsiteLanguage, WebsiteRegion } from '@/i18n';
import { DEFAULT_REGION } from '@socialincome/shared/src/firebase';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Analytics, getAnalytics, isSupported as isAnalyticsSupported } from 'firebase/analytics';
import { connectAuthEmulator, getAuth } from 'firebase/auth';
import { connectFirestoreEmulator, getFirestore } from 'firebase/firestore';
import { connectFunctionsEmulator, getFunctions } from 'firebase/functions';
import { connectStorageEmulator, getStorage } from 'firebase/storage';
import { usePathname } from 'next/navigation';
import { PropsWithChildren, useEffect, useState } from 'react';
import { usePathname, useRouter } from 'next/navigation';
import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react';
import { Toaster } from 'react-hot-toast';
import {
AnalyticsProvider,
Expand Down Expand Up @@ -95,7 +98,7 @@ function FirebaseSDKProviders({ children }: PropsWithChildren) {
);
}

export function ThemeProvider({ children }: PropsWithChildren) {
function ThemeProvider({ children }: PropsWithChildren) {
const pathname = usePathname();
const baseSegment = pathname?.split('/')[3];

Expand All @@ -111,7 +114,58 @@ export function ThemeProvider({ children }: PropsWithChildren) {
return <body className={theme}>{children}</body>;
}

export function ProvidersClient({ children }: PropsWithChildren) {
type I18nContextType = {
language: WebsiteLanguage | undefined;
setLanguage: (language: WebsiteLanguage) => void;
region: WebsiteRegion | undefined;
setRegion: (country: WebsiteRegion) => void;
currency: WebsiteCurrency | undefined;
setCurrency: (currency: WebsiteCurrency) => void;
};

const I18nContext = createContext<I18nContextType>(undefined!);
export const useI18n = () => useContext(I18nContext);

function I18nProvider({ children }: PropsWithChildren) {
const router = useRouter();

const { value: language, setCookie: setLanguage } = useCookieState<WebsiteLanguage>(LANGUAGE_COOKIE);
const { value: region, setCookie: setRegion } = useCookieState<WebsiteRegion>(REGION_COOKIE);
const { value: currency, setCookie: setCurrency } = useCookieState<WebsiteCurrency>(CURRENCY_COOKIE);

useEffect(() => {
const pathSegments = window.location.pathname.split('/');
if (language && pathSegments[1] !== language) {
pathSegments[1] = language;
router.push(pathSegments.join('/'));
}
}, [language, router]);

useEffect(() => {
const pathSegments = window.location.pathname.split('/');
if (region && pathSegments[2] !== region) {
pathSegments[2] = region;
router.push(pathSegments.join('/'));
}
}, [region, router]);

return (
<I18nContext.Provider
value={{
language: language,
setLanguage: (language) => setLanguage(language, { expires: 365 }),
region: region,
setRegion: (country) => setRegion(country, { expires: 365 }),
currency: currency,
setCurrency: (currency) => setCurrency(currency, { expires: 365 }),
}}
>
{children}
</I18nContext.Provider>
);
}

export function ContextProviders({ children }: PropsWithChildren) {
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
Expand All @@ -128,8 +182,10 @@ export function ProvidersClient({ children }: PropsWithChildren) {
<FirebaseSDKProviders>
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<Toaster />
{children}
<I18nProvider>
<Toaster />
{children}
</I18nProvider>
</ThemeProvider>
</QueryClientProvider>
</FirebaseSDKProviders>
Expand Down
7 changes: 2 additions & 5 deletions website/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { I18nProvider } from '@/app/providers/i18n-provider';
import { ProvidersClient } from '@/app/providers/providers-client';
import { ContextProviders } from '@/app/context-providers';
import { PropsWithChildren } from 'react';
import './globals.css';

Expand All @@ -11,9 +10,7 @@ export const metadata = {
export default function RootLayout({ children }: PropsWithChildren) {
return (
<html suppressHydrationWarning={true}>
<I18nProvider>
<ProvidersClient>{children}</ProvidersClient>
</I18nProvider>
<ContextProviders>{children}</ContextProviders>
</html>
);
}
68 changes: 0 additions & 68 deletions website/src/app/providers/i18n-provider-client.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions website/src/app/providers/i18n-provider.tsx

This file was deleted.

5 changes: 2 additions & 3 deletions website/src/components/country-flags.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { WebsiteCurrency } from '@/i18n';
import { CH, EU } from 'country-flag-icons/react/1x1';
import { SL, US } from 'country-flag-icons/react/3x2';
import { Component } from 'react';

export function getFlagComponentByCurrency(currency: WebsiteCurrency | undefined) {
switch (currency) {
Expand All @@ -13,7 +12,7 @@ export function getFlagComponentByCurrency(currency: WebsiteCurrency | undefined
return EU;
case 'SLE':
return SL;
case undefined:
return Component;
default:
return undefined;
}
}
2 changes: 1 addition & 1 deletion website/src/components/footer/footer-client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { DefaultParams } from '@/app/[lang]/[region]';
import { useI18n } from '@/app/providers/i18n-provider-client';
import { useI18n } from '@/app/context-providers';
import { WebsiteLanguage, WebsiteRegion } from '@/i18n';
import { GlobeAltIcon, LanguageIcon } from '@heroicons/react/24/solid';
import {
Expand Down
11 changes: 4 additions & 7 deletions website/src/components/i18n-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useI18n } from '@/app/providers/i18n-provider-client';
import { useI18n } from '@/app/context-providers';
import { WebsiteCurrency, WebsiteLanguage, WebsiteRegion } from '@/i18n';
import { CurrencyDollarIcon } from '@heroicons/react/24/outline';
import { GlobeEuropeAfricaIcon, LanguageIcon } from '@heroicons/react/24/solid';
Expand Down Expand Up @@ -36,11 +36,8 @@ type I18nDialogProps = {
}[];
translations: {
language: string;
currentLanguage: string;
region: string;
currentRegion: string;
currency: string;
currentCurrency: string;
};
};

Expand All @@ -58,7 +55,7 @@ export function I18nDialog(
<Select value={language} onValueChange={(l: WebsiteLanguage) => setLanguage(l)}>
<SelectTrigger className="space-x-2">
<LanguageIcon className="h-4 w-4" />
<SelectValue placeholder={translations.currentLanguage} />
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
Expand All @@ -77,7 +74,7 @@ export function I18nDialog(
<Select value={region} onValueChange={(c: WebsiteRegion) => setRegion(c)}>
<SelectTrigger className="space-x-2">
<GlobeEuropeAfricaIcon className="h-4 w-4" />
<SelectValue placeholder={translations.currentRegion} />
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
Expand All @@ -102,7 +99,7 @@ export function I18nDialog(
>
<SelectTrigger className="space-x-2">
<CurrencyDollarIcon className="h-4 w-4" />
<SelectValue placeholder={translations.currentCurrency} />
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
Expand Down
8 changes: 1 addition & 7 deletions website/src/components/navbar/navbar-client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import { DefaultParams } from '@/app/[lang]/[region]';
import { I18nDialog } from '@/components/i18n-dialog';
import { SILogo } from '@/components/logos/si-logo';
import { WebsiteCurrency } from '@/i18n';
Expand All @@ -23,7 +24,6 @@ import {
import _ from 'lodash';
import Link from 'next/link';
import { useState } from 'react';
import { DefaultParams } from '../../app/[lang]/[region]';

type NavbarSection = {
title: string;
Expand All @@ -40,11 +40,8 @@ type NavbarProps = {
sections: NavbarSection[];
translations: {
language: string;
currentLanguage: string;
region: string;
currentRegion: string;
currency: string;
currentCurrency: string;
myProfile: string;
contactDetails: string;
payments: string;
Expand Down Expand Up @@ -76,11 +73,8 @@ export function NavbarClient(
currencies={currencies}
translations={{
language: translations.language,
currentLanguage: translations.currentLanguage,
region: translations.region,
currentRegion: translations.currentRegion,
currency: translations.currency,
currentCurrency: translations.currentCurrency,
}}
>
<Button variant="ghost" className="flex max-w-md space-x-2">
Expand Down
Loading
Loading