mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
34 lines
1003 B
TypeScript
34 lines
1003 B
TypeScript
import { createContext, useContext } from 'react';
|
|
import React from 'react';
|
|
|
|
import type { OrganisationSession } from '@documenso/trpc/server/organisation-router/get-organisation-session.types';
|
|
|
|
type OrganisationProviderValue = OrganisationSession;
|
|
|
|
interface OrganisationProviderProps {
|
|
children: React.ReactNode;
|
|
organisation: OrganisationProviderValue | null;
|
|
}
|
|
|
|
const OrganisationContext = createContext<OrganisationProviderValue | null>(null);
|
|
|
|
export const useCurrentOrganisation = () => {
|
|
const context = useContext(OrganisationContext);
|
|
|
|
if (!context) {
|
|
throw new Error('useCurrentOrganisation must be used within a OrganisationProvider');
|
|
}
|
|
|
|
return context;
|
|
};
|
|
|
|
export const useOptionalCurrentOrganisation = () => {
|
|
return useContext(OrganisationContext);
|
|
};
|
|
|
|
export const OrganisationProvider = ({ children, organisation }: OrganisationProviderProps) => {
|
|
return (
|
|
<OrganisationContext.Provider value={organisation}>{children}</OrganisationContext.Provider>
|
|
);
|
|
};
|