-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move feature available in application into a provider
- Loading branch information
Showing
8 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
createContext, | ||
type FC, | ||
type PropsWithChildren, | ||
useContext, | ||
} from 'react' | ||
import { env } from '~/env.mjs' | ||
|
||
type FeatureContextProps = { | ||
storage: boolean | ||
sgid: boolean | ||
} | ||
|
||
// Exported for testing. | ||
export const FeatureContext = createContext<FeatureContextProps | undefined>( | ||
undefined | ||
) | ||
|
||
/** | ||
* Provider component that wraps your app and makes the available features available to any | ||
* child component that calls `useFeatures()`. | ||
*/ | ||
export const FeatureProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const features = useProvideFeatures() | ||
|
||
return ( | ||
<FeatureContext.Provider value={features}> | ||
{children} | ||
</FeatureContext.Provider> | ||
) | ||
} | ||
|
||
/** | ||
* Hook for components nested in FeatureProvider component to get the current feature object. | ||
*/ | ||
export const useFeatures = (): FeatureContextProps => { | ||
const context = useContext(FeatureContext) | ||
if (!context) { | ||
throw new Error( | ||
`useFeatures must be used within a FeatureProvider component` | ||
) | ||
} | ||
return context | ||
} | ||
|
||
// Provider hook that creates auth object and handles state | ||
const useProvideFeatures = () => { | ||
// Return the user object and auth methods | ||
return { | ||
storage: !!env.NEXT_PUBLIC_ENABLE_STORAGE, | ||
sgid: !!env.NEXT_PUBLIC_ENABLE_SGID, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './FeatureProvider' |
7 changes: 3 additions & 4 deletions
7
src/features/posts/components/NewPostModal/NewPostModalButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters