Skip to content

Commit

Permalink
add sentry and fix a hydration bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsv authored and cgsv committed Mar 10, 2023
1 parent ef4b87e commit 8e577a4
Show file tree
Hide file tree
Showing 12 changed files with 658 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ SAVESUBS_X_AUTH_TOKEN=
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
UPSTASH_RATE_REDIS_REST_URL=
UPSTASH_RATE_REDIS_REST_TOKEN=
UPSTASH_RATE_REDIS_REST_TOKEN=
NEXT_PUBLIC_SENTRY_DSN=
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# Sentry
.sentryclirc

# Sentry
next.config.original.js
12 changes: 11 additions & 1 deletion components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ import { checkOpenaiApiKey } from "@/lib/openai/openai";
import { toast } from "react-hot-toast";
import { useTranslation } from 'next-i18next';
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

export default function Header() {
const [userKey, setUserKey] = useLocalStorage<string>("user-openai-apikey-trans");
const [translateEngine, setTranslateEngine] = useLocalStorage<string>("translate-engine");

// note: Hydration error when use SSR, do not use localStorage for display now
const [translateEngine, setTranslateEngine] = useState("");
const [translateEngineStore, setTranslateEngineStore] = useLocalStorage<string>("translate-engine");

const { t } = useTranslation("common");
const { i18n } = useTranslation();
const router = useRouter();
const tooltip = "Current using " + (translateEngine === "google" ? "google translate" : "GPT") + ", Click to change";

useEffect(() => {
setTranslateEngine(translateEngineStore || "");
}, []);

const changeLang = () => {
const newLang = i18n.language === "en" ? "zh-CN" : "en";

Expand All @@ -26,6 +35,7 @@ export default function Header() {
const changeEngine = () => {
const newEngine = translateEngine === "google" ? "openai" : "google";
setTranslateEngine(newEngine);
setTranslateEngineStore(newEngine);
}

const setOpenAIKey = () => {
Expand Down
12 changes: 12 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// This file sets a custom webpack configuration to use your Next.js app
// with Sentry.
// https://nextjs.org/docs/api-reference/next.config.js/introduction
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
const { withSentryConfig } = require('@sentry/nextjs');

/** @type {import('next').NextConfig} */
const {i18n} = require('./next-i18next.config');

Expand All @@ -7,3 +13,9 @@ const nextConfig = {
}

module.exports = nextConfig

module.exports = withSentryConfig(
module.exports,
{ silent: true },
{ hideSourcemaps: true },
);
Loading

1 comment on commit 8e577a4

@vercel
Copy link

@vercel vercel bot commented on 8e577a4 Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.