Skip to content

Commit 8e577a4

Browse files
cgsvcgsv
cgsv
authored and
cgsv
committed
add sentry and fix a hydration bug
1 parent ef4b87e commit 8e577a4

12 files changed

+658
-5
lines changed

.example.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ SAVESUBS_X_AUTH_TOKEN=
44
UPSTASH_REDIS_REST_URL=
55
UPSTASH_REDIS_REST_TOKEN=
66
UPSTASH_RATE_REDIS_REST_URL=
7-
UPSTASH_RATE_REDIS_REST_TOKEN=
7+
UPSTASH_RATE_REDIS_REST_TOKEN=
8+
NEXT_PUBLIC_SENTRY_DSN=

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ yarn-error.log*
3434
# typescript
3535
*.tsbuildinfo
3636
next-env.d.ts
37+
38+
# Sentry
39+
.sentryclirc
40+
41+
# Sentry
42+
next.config.original.js

components/Header.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ import { checkOpenaiApiKey } from "@/lib/openai/openai";
55
import { toast } from "react-hot-toast";
66
import { useTranslation } from 'next-i18next';
77
import { useRouter } from "next/router";
8+
import { useEffect, useState } from "react";
89

910
export default function Header() {
1011
const [userKey, setUserKey] = useLocalStorage<string>("user-openai-apikey-trans");
11-
const [translateEngine, setTranslateEngine] = useLocalStorage<string>("translate-engine");
12+
13+
// note: Hydration error when use SSR, do not use localStorage for display now
14+
const [translateEngine, setTranslateEngine] = useState("");
15+
const [translateEngineStore, setTranslateEngineStore] = useLocalStorage<string>("translate-engine");
16+
1217
const { t } = useTranslation("common");
1318
const { i18n } = useTranslation();
1419
const router = useRouter();
1520
const tooltip = "Current using " + (translateEngine === "google" ? "google translate" : "GPT") + ", Click to change";
1621

22+
useEffect(() => {
23+
setTranslateEngine(translateEngineStore || "");
24+
}, []);
25+
1726
const changeLang = () => {
1827
const newLang = i18n.language === "en" ? "zh-CN" : "en";
1928

@@ -26,6 +35,7 @@ export default function Header() {
2635
const changeEngine = () => {
2736
const newEngine = translateEngine === "google" ? "openai" : "google";
2837
setTranslateEngine(newEngine);
38+
setTranslateEngineStore(newEngine);
2939
}
3040

3141
const setOpenAIKey = () => {

next.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// This file sets a custom webpack configuration to use your Next.js app
2+
// with Sentry.
3+
// https://nextjs.org/docs/api-reference/next.config.js/introduction
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
5+
const { withSentryConfig } = require('@sentry/nextjs');
6+
17
/** @type {import('next').NextConfig} */
28
const {i18n} = require('./next-i18next.config');
39

@@ -7,3 +13,9 @@ const nextConfig = {
713
}
814

915
module.exports = nextConfig
16+
17+
module.exports = withSentryConfig(
18+
module.exports,
19+
{ silent: true },
20+
{ hideSourcemaps: true },
21+
);

0 commit comments

Comments
 (0)