Skip to content

Commit

Permalink
website(bugfix): fix client-side rendering issue (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkue authored Mar 29, 2024
1 parent 09f8ac8 commit 552df17
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default onCall<CreateDonationCertificatesFunctionProps, Promise<string>>(
await withFile(async ({ path }) => {
await writer.writeDonationCertificatePDF(path);

const { downloadUrl } = await storageAdmin.uploadAndGetDownloadURL({
const { downloadUrl } = await storageAdmin.uploadAndGetDownloadUrl({
sourceFilePath: path,
destinationFilePath: `users/${userId}/donation-certificates/${writer.year}_${writer.user.language}.pdf`,
});
Expand Down
2 changes: 1 addition & 1 deletion shared/src/firebase/admin/StorageAdmin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('useStorageAdmin', () => {
test('upload private file', async () => {
await fs.writeFile(tmpFile, 'test');

const { downloadUrl } = await storageAdmin.uploadAndGetDownloadURL({
const { downloadUrl } = await storageAdmin.uploadAndGetDownloadUrl({
sourceFilePath: tmpFile,
destinationFilePath: tmpFile,
});
Expand Down
2 changes: 1 addition & 1 deletion shared/src/firebase/admin/StorageAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class StorageAdmin {
await destinationBucket.upload(sourceFilePath, { destination: destinationFilePath });
};

uploadAndGetDownloadURL = async ({ bucket, sourceFilePath, destinationFilePath }: UploadProps) => {
uploadAndGetDownloadUrl = async ({ bucket, sourceFilePath, destinationFilePath }: UploadProps) => {
const destinationBucket = bucket || this.storage.bucket();
const token = randomBytes(32).toString('hex');
const [file, metadata] = await destinationBucket.upload(sourceFilePath, {
Expand Down
1 change: 1 addition & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ yarn-error.log*

# local env files
.env*.local
.env.production

# vercel
.vercel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const NewsletterPopupClient = ({ delay, lang, translations }: NewsletterP
return () => {
clearTimeout(timeout);
};
}, []);
}, [delay, lang, translations]);

return null;
};
23 changes: 17 additions & 6 deletions website/src/components/providers/context-providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { connectFunctionsEmulator, getFunctions } from 'firebase/functions';
import { connectStorageEmulator, getStorage } from 'firebase/storage';
import _ from 'lodash';
import { useRouter, useSearchParams } from 'next/navigation';
import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react';
import { PropsWithChildren, Suspense, createContext, useContext, useEffect, useState } from 'react';
import {
AnalyticsProvider,
AuthProvider,
Expand Down Expand Up @@ -150,14 +150,14 @@ type I18nContextType = {
const I18nContext = createContext<I18nContextType>(undefined!);
export const useI18n = () => useContext(I18nContext);

function I18nProvider({ children }: PropsWithChildren) {
function I18nUrlUpdater() {
// This component is used to watch the URL and update the language and region in the context if the URL changes.
// It's a separate component because it uses the useSearchParams hook, and needs to be wrapped in a Suspense
// boundary (https://nextjs.org/docs/messages/deopted-into-client-rendering).
const router = useRouter();
const searchParams = useSearchParams();
const searchParamsString = searchParams.toString();

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);
const { language, setLanguage, region, setRegion } = useI18n();

useEffect(() => {
const urlSegments = window.location.pathname.split('/');
Expand All @@ -181,6 +181,14 @@ function I18nProvider({ children }: PropsWithChildren) {
}
}, [region, router, setRegion]);

return null;
}

function I18nProvider({ children }: PropsWithChildren) {
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);

return (
<I18nContext.Provider
value={{
Expand All @@ -192,6 +200,9 @@ function I18nProvider({ children }: PropsWithChildren) {
setCurrency: (currency) => setCurrency(currency, { expires: 365 }),
}}
>
<Suspense fallback={null}>
<I18nUrlUpdater />
</Suspense>
{children}
</I18nContext.Provider>
);
Expand Down

0 comments on commit 552df17

Please sign in to comment.