Skip to content

Commit

Permalink
Flip license context
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed Dec 13, 2019
1 parent ec180e1 commit b7e5f18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
22 changes: 16 additions & 6 deletions x-pack/legacy/plugins/apm/public/context/LicenseContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useState, useEffect } from 'react';
import { License } from '../../../../../../plugins/licensing/common/license';
import { ILicense } from '../../../../../../plugins/licensing/public';
import { useLicense } from '../../hooks/useLicense';
import { InvalidLicenseNotification } from './InvalidLicenseNotification';
import { useApmPluginContext } from '../../hooks/useApmPluginContext';

// Set the initial licesnse to a new license with an "undefined license"
// signature. This allows us to always have a license defined.
export const LicenseContext = React.createContext<ILicense>(
new License({ signature: 'undefined license' })
);
const emptyLicense = new License({ signature: 'undefined license' });

export const LicenseContext = React.createContext<ILicense>(emptyLicense);

export function LicenseProvider({ children }: { children: React.ReactChild }) {
const license = useLicense();
// Set the initial licesnse to a new license with an "undefined license"
// signature. This allows us to always have a license defined.
const [license, setLicense] = useState<ILicense>(emptyLicense);
const { license$ } = useApmPluginContext().plugins.licensing;

useEffect(() => {
const subscription = license$.subscribe(nextLicense => {
setLicense(nextLicense);
});
return () => subscription.unsubscribe();
}, [license$]);

const hasValidLicense =
license.signature === 'undefined license' || license.isActive;
Expand Down
22 changes: 3 additions & 19 deletions x-pack/legacy/plugins/apm/public/hooks/useLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useEffect, useState } from 'react';
import { ILicense } from '../../../../../plugins/licensing/public';
import { useApmPluginContext } from './useApmPluginContext';
import { License } from '../../../../../plugins/licensing/common/license';
import { useContext } from 'react';
import { LicenseContext } from '../context/LicenseContext';

export function useLicense() {
// Set the initial licesnse to a new license with an "undefined license"
// signature. This allows us to always have a license defined.
const [license, setLicense] = useState<ILicense>(
new License({ signature: 'undefined license' })
);
const { license$ } = useApmPluginContext().plugins.licensing;

useEffect(() => {
const subscription = license$.subscribe(nextLicense => {
setLicense(nextLicense);
});
return () => subscription.unsubscribe();
}, [license$]);

return license;
return useContext(LicenseContext);
}

0 comments on commit b7e5f18

Please sign in to comment.