Skip to content

Commit 7650105

Browse files
committed
refactor(components): optimize theme handling and cleanup unused code
- Removed unnecessary useEffect in Markdown component for theme setting - Introduced useRef in Sakana component for better widget management - Updated theme setting logic in Index page to use useEffect for improved performance
1 parent ba208e7 commit 7650105

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

components/markdown.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useTheme} from 'next-themes'
2-
import React, {useEffect, useState} from 'react'
2+
import React, {useState} from 'react'
33
import ReactMarkdown from 'react-markdown'
44
import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter'
55
import {a11yDark as dark, prism as light} from 'react-syntax-highlighter/dist/cjs/styles/prism'
@@ -66,15 +66,11 @@ const Code: CodeComponent = ({
6666
children = '',
6767
...properties
6868
}) => {
69-
const {theme, setTheme} = useTheme()
69+
const {theme} = useTheme()
7070
const [copied, setCopied] = useState(false)
7171
const match = /language-(\w+)/.exec(className || '')
7272
const isCodeBlock = Boolean(match) || String(children).includes('\n')
7373

74-
useEffect(() => {
75-
setTheme(theme!)
76-
}, [setTheme, theme])
77-
7874
const handleCopy = async () => {
7975
const text = String(children).replace(/\n$/, '')
8076
try {

components/sakana.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { useTheme } from 'next-themes';
2-
import { useEffect } from 'react';
2+
import { useEffect, useRef } from 'react';
33
import SakanaWidget from 'sakana-widget';
44
import 'sakana-widget/lib/index.css';
55

66
const Sakana = () => {
77
const { theme } = useTheme();
8+
const widgetRef = useRef<SakanaWidget | null>(null);
89

910
useEffect(() => {
10-
new SakanaWidget({
11+
widgetRef.current?.unmount();
12+
13+
const widget = new SakanaWidget({
1114
character: Math.random() < 0.6 ? 'takina' : 'chisato',
1215
controls: false,
1316
stroke: { color: theme === 'dark' ? '#333333' : '#e6e6e6' },
1417
}).setState({ i: 0.001, d: 1 }).mount('#sakana-widget');
18+
19+
widgetRef.current = widget;
20+
21+
return () => {
22+
widget.unmount();
23+
widgetRef.current = null;
24+
};
1525
}, [theme]);
1626

1727
return (

pages/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fetchPosts, fetchUser } from '../lib';
55
import genRSS from '../lib/rss';
66

77
import { useTheme } from 'next-themes';
8+
import { useEffect } from 'react';
89

910
import Image from 'next/image';
1011

@@ -27,7 +28,9 @@ export const getStaticProps: GetStaticProps = async () => {
2728
const Index: NextPageWithLayout<{ posts: Post[]; user: User }> = ({ posts, user }) => {
2829
const { setTheme } = useTheme();
2930

30-
if (process.env.theme !== 'both') setTheme(process.env.theme || 'dark');
31+
useEffect(() => {
32+
if (process.env.theme !== 'both') setTheme(process.env.theme || 'dark');
33+
}, [setTheme]);
3134

3235
return (
3336
<div>

0 commit comments

Comments
 (0)