Skip to content

Commit 1c85fc6

Browse files
committed
feat(context): add Program context and provider for managing program selection
1 parent ceec09b commit 1c85fc6

File tree

2 files changed

+98
-16
lines changed

2 files changed

+98
-16
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { createContext, useContext, useState, ReactNode } from 'react'
2+
import { ProgramType } from '@pythnetwork/xc-admin-common'
3+
import { useQueryState, parseAsStringLiteral } from 'nuqs'
4+
5+
/**
6+
* Interface defining the shape of the Program context
7+
*/
8+
interface ProgramContextType {
9+
/**
10+
* Currently selected program type
11+
*/
12+
programType: ProgramType
13+
14+
/**
15+
* Function to set the current program type
16+
*/
17+
setProgramType: (type: ProgramType) => void
18+
19+
/**
20+
* Whether the selected program is supported on the current cluster
21+
*/
22+
isProgramSupported: boolean
23+
}
24+
25+
/**
26+
* Default context values
27+
*/
28+
const defaultContext: ProgramContextType = {
29+
programType: ProgramType.PYTH_CORE,
30+
setProgramType: () => undefined,
31+
isProgramSupported: true,
32+
}
33+
34+
/**
35+
* Context for managing the currently selected Pyth program (Core, Lazer, etc.)
36+
*/
37+
const ProgramContext = createContext<ProgramContextType>(defaultContext)
38+
39+
/**
40+
* Provider component for the Program context
41+
*/
42+
export const ProgramProvider = ({ children }: { children: ReactNode }) => {
43+
// Use URL query parameter to persist program selection across page reloads
44+
const [programTypeParam, setProgramTypeParam] = useQueryState(
45+
'program',
46+
parseAsStringLiteral(
47+
Object.values(ProgramType) as readonly string[]
48+
).withDefault(ProgramType.PYTH_CORE)
49+
)
50+
51+
// Local state for program support
52+
const [isProgramSupported] = useState(true)
53+
54+
/**
55+
* Update both the URL parameter and context state
56+
*/
57+
const setProgramType = (type: ProgramType) => {
58+
setProgramTypeParam(type)
59+
}
60+
61+
// TODO: Add effect to check if the selected program is supported on the current cluster
62+
// This will be implemented when we have the adapter implementations
63+
64+
const value = {
65+
programType: programTypeParam as ProgramType,
66+
setProgramType,
67+
isProgramSupported,
68+
}
69+
70+
return (
71+
<ProgramContext.Provider value={value}>{children}</ProgramContext.Provider>
72+
)
73+
}
74+
75+
/**
76+
* Hook for accessing the Program context
77+
* @returns The Program context values
78+
*/
79+
export const useProgramContext = () => useContext(ProgramContext)

governance/xc_admin/packages/xc_admin_frontend/pages/_app.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Head from 'next/head'
2020
import { useMemo } from 'react'
2121
import { Toaster } from 'react-hot-toast'
2222
import { ClusterProvider } from '../contexts/ClusterContext'
23+
import { ProgramProvider } from '../contexts/ProgramContext'
2324
import SEO from '../next-seo.config'
2425
import '../styles/globals.css'
2526
import { NuqsAdapter } from 'nuqs/adapters/next/pages'
@@ -68,23 +69,25 @@ function MyApp({ Component, pageProps }: AppProps) {
6869
<WalletProvider wallets={wallets} autoConnect>
6970
<WalletModalProvider>
7071
<ClusterProvider>
71-
<Head>
72-
<meta
73-
name="viewport"
74-
content="width=device-width, initial-scale=1.0, maximum-scale=1.0"
72+
<ProgramProvider>
73+
<Head>
74+
<meta
75+
name="viewport"
76+
content="width=device-width, initial-scale=1.0, maximum-scale=1.0"
77+
/>
78+
</Head>
79+
<DefaultSeo {...SEO} />
80+
<Component {...pageProps} />
81+
<Toaster
82+
position="bottom-left"
83+
toastOptions={{
84+
style: {
85+
wordBreak: 'break-word',
86+
},
87+
}}
88+
reverseOrder={false}
7589
/>
76-
</Head>
77-
<DefaultSeo {...SEO} />
78-
<Component {...pageProps} />
79-
<Toaster
80-
position="bottom-left"
81-
toastOptions={{
82-
style: {
83-
wordBreak: 'break-word',
84-
},
85-
}}
86-
reverseOrder={false}
87-
/>
90+
</ProgramProvider>
8891
</ClusterProvider>
8992
</WalletModalProvider>
9093
</WalletProvider>

0 commit comments

Comments
 (0)