Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit e532c80

Browse files
committed
all install nitric components should have same OS state
1 parent d95e4d4 commit e532c80

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

src/app/providers.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useEffect } from 'react'
44
import { ThemeProvider, useTheme } from 'next-themes'
5+
import { OSProvider } from '@/components/InstallNitric/OSContext'
56

67
function ThemeWatcher() {
78
let { resolvedTheme, setTheme } = useTheme()
@@ -31,7 +32,7 @@ export function Providers({ children }: { children: React.ReactNode }) {
3132
return (
3233
<ThemeProvider attribute="class" disableTransitionOnChange>
3334
<ThemeWatcher />
34-
{children}
35+
<OSProvider>{children}</OSProvider>
3536
</ThemeProvider>
3637
)
3738
}

src/components/InstallNitric/InstallNitricTabs.client.tsx

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,24 @@
11
'use client'
22

3-
import React, { useEffect, useState } from 'react'
3+
import React from 'react'
44
import { HighlightedCode, RawCode } from 'codehike/code'
55
import { Tabs, TabsContent, TabsList, TabsTrigger } from '../ui/tabs'
66
import Pre from '../code/Pre'
7+
import { OS, useOS } from './OSContext'
78

89
interface InstallNitricProps {
910
highlighted: HighlightedCode[]
1011
tabs: RawCode[]
1112
}
1213

13-
// this function detects the operating system of the user and returns the name that will be used to sort the instructions
14-
function detectOS() {
15-
const userAgent = window.navigator.userAgent
16-
if (userAgent.includes('Mac')) {
17-
return 'macOS'
18-
} else if (userAgent.includes('Win')) {
19-
return 'Windows'
20-
}
21-
22-
return 'Linux'
23-
}
24-
2514
export const InstallNitricTabs: React.FC<InstallNitricProps> = ({
2615
highlighted,
2716
tabs,
2817
}) => {
29-
const [value, setValue] = useState('')
30-
31-
useEffect(() => {
32-
// this will only run on the client side
33-
const OS = detectOS()
34-
35-
highlighted.some((code) => {
36-
// check based on the title prop of the children with the detected OS
37-
if (code.meta === OS) {
38-
setValue(code.meta)
39-
return true
40-
}
41-
})
42-
}, [])
43-
44-
if (!value) return null
18+
const { currentOS, setCurrentOS } = useOS()
4519

4620
return (
47-
<Tabs value={value} onValueChange={setValue}>
21+
<Tabs value={currentOS} onValueChange={(val) => setCurrentOS(val as OS)}>
4822
<TabsList className="mt-auto h-10 bg-transparent">
4923
{tabs.map((tab) => (
5024
<TabsTrigger
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use client'
2+
3+
import React, {
4+
createContext,
5+
PropsWithChildren,
6+
useContext,
7+
useEffect,
8+
useState,
9+
} from 'react'
10+
11+
export type OS = 'Windows' | 'macOS' | 'Linux'
12+
13+
export interface OSContextProps {
14+
currentOS: OS
15+
setCurrentOS: (os: OS) => void
16+
}
17+
18+
// this function detects the operating system of the user and returns the name that will be used to sort the instructions
19+
function detectOS() {
20+
const userAgent = window.navigator.userAgent
21+
if (userAgent.includes('Mac')) {
22+
return 'macOS'
23+
} else if (userAgent.includes('Win')) {
24+
return 'Windows'
25+
}
26+
27+
return 'Linux'
28+
}
29+
30+
export const OSContext = createContext<OSContextProps>({
31+
currentOS: 'macOS', // Default value
32+
setCurrentOS: () => {}, // Default value
33+
})
34+
35+
export const OSProvider: React.FC<PropsWithChildren> = ({ children }) => {
36+
const [currentOS, setCurrentOS] = useState<OS>('macOS') // Set the initial OS here
37+
38+
useEffect(() => {
39+
// this will only run on the client side
40+
setCurrentOS(detectOS())
41+
}, [])
42+
43+
return (
44+
<OSContext.Provider value={{ currentOS, setCurrentOS }}>
45+
{children}
46+
</OSContext.Provider>
47+
)
48+
}
49+
50+
export const useOS = (): OSContextProps => {
51+
return useContext(OSContext)
52+
}

0 commit comments

Comments
 (0)