useOrganization.tsx 962 B

123456789101112131415161718192021222324252627282930313233343536
  1. import {useContext} from 'react';
  2. import {Organization} from 'sentry/types';
  3. import {OrganizationContext} from 'sentry/views/organizationContext';
  4. interface Options<AllowNull extends boolean = boolean> {
  5. /**
  6. * Allows null to be returned when there is no organization in context. This
  7. * can happen when the user is not part of an organization
  8. *
  9. * @default false
  10. */
  11. allowNull?: AllowNull;
  12. }
  13. // The additional signatures provide proper type hints for when we set
  14. // `allowNull` to true.
  15. function useOrganization(opts?: Options<false>): Organization;
  16. function useOrganization(opts?: Options<true>): Organization | null;
  17. function useOrganization({allowNull}: Options = {}) {
  18. const organization = useContext(OrganizationContext);
  19. if (allowNull) {
  20. return organization;
  21. }
  22. if (!organization) {
  23. throw new Error('useOrganization called but organization is not set.');
  24. }
  25. return organization;
  26. }
  27. export default useOrganization;