Skip to content

Commit 949c5a0

Browse files
authored
Merge pull request #174 from PedroMarianoAlmeida/persist-preferredLanguage
persist-preferredLanguage
2 parents e23bcc9 + e1e9215 commit 949c5a0

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

src/App.jsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom'
22
import { SearchProvider } from './components/context/SearchContext/SearchContext';
3+
import { LanguageProvider } from './components/context/LanguageContext/LanguageContext'
34
import { useEffect } from 'react';
45
import { Toaster } from 'sonner'
56
import { forceChakraDarkTheme } from './utils/utils';
@@ -24,21 +25,23 @@ export default function App() {
2425
<Route exact path="/showcase" element={<ShowcasePage />} />
2526
<Route path="/:category/:subcategory" element={
2627
<SearchProvider>
27-
<main className='app-container'>
28-
<Header />
29-
<section className='category-wrapper'>
30-
<Sidebar />
31-
<CategoryPage />
32-
</section>
33-
<Toaster
34-
toastOptions={toastStyles}
35-
position='bottom-right'
36-
visibleToasts={1}
37-
/>
38-
</main>
28+
<LanguageProvider>
29+
<main className='app-container'>
30+
<Header />
31+
<section className='category-wrapper'>
32+
<Sidebar />
33+
<CategoryPage />
34+
</section>
35+
<Toaster
36+
toastOptions={toastStyles}
37+
position='bottom-right'
38+
visibleToasts={1}
39+
/>
40+
</main>
41+
</LanguageProvider>
3942
</SearchProvider>
4043
} />
4144
</Routes>
4245
</Router>
4346
)
44-
}
47+
}

src/components/code/CodeOptions.jsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import { Children, useEffect, useState } from "react";
22
import { Tabs, TabList, Tab, TabPanels, TabPanel, Icon, Text, Flex, Select } from "@chakra-ui/react";
33
import { RiEmotionSadLine, RiTailwindCssFill } from "react-icons/ri";
44
import { FiCode } from "react-icons/fi";
5+
import { useLanguage } from '../context/LanguageContext/useLanguage';
56

67
const CodeOptions = ({ children }) => {
7-
const [isJS, setIsJS] = useState(true);
8-
const [languagePreset, setLanguagePreset] = useState(null);
9-
10-
useEffect(() => {
11-
const language = localStorage.getItem('preferredLanguage') || 'JS';
12-
13-
handleLanguageSelection(language);
14-
setLanguagePreset(language);
15-
}, [])
8+
const { languagePreset, setLanguagePreset } = useLanguage();
169

1710
const tabComponents = {
1811
JS: { css: CSSTab, tailwind: TailwindTab },
@@ -28,12 +21,10 @@ const CodeOptions = ({ children }) => {
2821
return acc;
2922
}, { JS: { css: null, tailwind: null }, TS: { css: null, tailwind: null } });
3023

31-
const handleLanguageSelection = (language) => setIsJS(language === "JS");
32-
3324
const hasValidContent = (content) => content?.props?.children;
3425

3526
const renderTabContent = (type) => {
36-
const content = isJS ? categorizedTabs.JS[type] : categorizedTabs.TS[type];
27+
const content = languagePreset === "JS" ? categorizedTabs.JS[type] : categorizedTabs.TS[type];
3728
return hasValidContent(content) ? content : (
3829
<Flex alignItems="center" gap={2} my={6} color="#a1a1aa">
3930
<Text>Nothing here yet!</Text>
@@ -68,8 +59,8 @@ const CodeOptions = ({ children }) => {
6859
<Select
6960
width="fit-content"
7061
sx={selectStyles}
71-
onChange={(e) => handleLanguageSelection(e.target.value)}
72-
defaultValue={localStorage.getItem('preferredLanguage') || 'JS'}
62+
onChange={(e) => setLanguagePreset(e.target.value)}
63+
value={languagePreset}
7364
>
7465
<option value="JS">JS</option>
7566
<option value="TS">TS</option>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createContext, useState, useEffect } from 'react';
2+
3+
export const LanguageContext = createContext();
4+
5+
export function LanguageProvider({ children }) {
6+
const [languagePreset, setLanguagePreset] = useState(null);
7+
8+
useEffect(() => {
9+
const language = localStorage.getItem('preferredLanguage') || 'JS';
10+
setLanguagePreset(language);
11+
}, [])
12+
13+
useEffect(() => {
14+
localStorage.setItem('preferredLanguage', languagePreset)
15+
}, [languagePreset])
16+
17+
return (
18+
<LanguageContext.Provider value={{ languagePreset, setLanguagePreset }}>
19+
{children}
20+
</LanguageContext.Provider>
21+
);
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useContext } from 'react';
2+
import { LanguageContext } from './LanguageContext';
3+
4+
export function useLanguage() {
5+
return useContext(LanguageContext);
6+
}

src/components/navs/Header.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { TiStarFullOutline } from "react-icons/ti";
2222
import { useStars } from '../../hooks/useStars';
2323
import { useDeviceOS } from 'react-haiku';
2424
import { useSearch } from '../context/SearchContext/useSearch';
25+
import { useLanguage } from '../context/LanguageContext/useLanguage'
2526

2627

2728
import Logo from '../../assets/logos/reactbits-logo.svg';
@@ -31,6 +32,7 @@ import FadeContent from '../../content/Animations/FadeContent/FadeContent';
3132
const Header = () => {
3233
const { isOpen, onOpen, onClose } = useDisclosure();
3334
const { toggleSearch } = useSearch();
35+
const { languagePreset, setLanguagePreset } = useLanguage();
3436
const stars = useStars();
3537
const os = useDeviceOS();
3638

@@ -84,8 +86,8 @@ const Header = () => {
8486
rounded="xl"
8587
width="fit-content"
8688
fontWeight={600}
87-
onChange={(e) => localStorage.setItem('preferredLanguage', e.target.value)}
88-
defaultValue={localStorage.getItem('preferredLanguage') || 'JS'}
89+
onChange={(e) => setLanguagePreset(e.target.value)}
90+
value={languagePreset}
8991
>
9092
<option value="JS">JS</option>
9193
<option value="TS">TS</option>

0 commit comments

Comments
 (0)