Skip to content

Commit

Permalink
overload useUIExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
orouz committed Jan 31, 2023
1 parent 4aad0dd commit 2f642c7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,10 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
]
);

const extensionView = useUIExtension(packagePolicy.package?.name ?? '', 'package-policy-create');
const replaceDefineStepView = useUIExtension(
packagePolicy.package?.name ?? '',
'package-policy-replace-define-step'
);

if (replaceDefineStepView && extensionView) {
// eslint-disable-next-line no-console
console.warn(
"'package-policy-create' is ignored when 'package-policy-replace-define-step' is defined"
);
}
const [extensionView, replaceDefineStepView] = useUIExtension(packagePolicy.package?.name ?? '', [
'package-policy-create',
'package-policy-replace-define-step',
] as const);

const replaceStepConfigurePackagePolicy = replaceDefineStepView && packageInfo?.name && (
<ExtensionWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,15 @@ export const EditPackagePolicyForm = memo<{
}
};

const extensionView = useUIExtension(packagePolicy.package?.name ?? '', 'package-policy-edit');
const replaceDefineStepView = useUIExtension(
const [extensionView, replaceDefineStepView, extensionTabsView] = useUIExtension(
packagePolicy.package?.name ?? '',
'package-policy-replace-define-step'
);
const extensionTabsView = useUIExtension(
packagePolicy.package?.name ?? '',
'package-policy-edit-tabs'
[
'package-policy-edit',
'package-policy-replace-define-step',
'package-policy-edit-tabs',
] as const
);

if (replaceDefineStepView && extensionView) {
// eslint-disable-next-line no-console
console.warn(
"'package-policy-edit' is ignored when 'package-policy-replace-define-step' is defined"
);
}

const tabsViews = extensionTabsView?.tabs;
const [selectedTab, setSelectedTab] = useState(0);

Expand Down
39 changes: 30 additions & 9 deletions x-pack/plugins/fleet/public/hooks/use_ui_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,42 @@ type NarrowExtensionPoint<V extends UIExtensionPoint['view'], A = UIExtensionPoi
? A
: never;

export const useUIExtension = <V extends UIExtensionPoint['view'] = UIExtensionPoint['view']>(
type ExclusiveExtensions<T extends ReadonlyArray<UIExtensionPoint['view']>> =
| ReplaceWithUndefined<T, 'package-policy-edit' | 'package-policy-create'>
| ReplaceWithUndefined<T, 'package-policy-replace-define-step'>;

type ReplaceWithUndefined<Tuple extends readonly unknown[], Id> = Tuple extends readonly []
? []
: Tuple extends readonly [infer Head, ...infer Rest]
? Head extends Id
? readonly [undefined, ...ReplaceWithUndefined<Rest, Id>]
: Head extends UIExtensionPoint['view']
? readonly [NarrowExtensionPoint<Head> | undefined, ...ReplaceWithUndefined<Rest, Id>]
: never
: Tuple;

export function useUIExtension<
V extends ReadonlyArray<UIExtensionPoint['view']> = ReadonlyArray<UIExtensionPoint['view']>
>(packageName: UIExtensionPoint['package'], view: V): ExclusiveExtensions<V>;

export function useUIExtension<V extends UIExtensionPoint['view'] = UIExtensionPoint['view']>(
packageName: UIExtensionPoint['package'],
view: V
): NarrowExtensionPoint<V> | undefined => {
const registeredExtensions = useContext(UIExtensionsContext);
): NarrowExtensionPoint<V> | undefined;

export function useUIExtension<V extends UIExtensionPoint['view']>(
packageName: UIExtensionPoint['package'],
view: V | readonly V[]
) {
const registeredExtensions = useContext(UIExtensionsContext);
if (!registeredExtensions) {
throw new Error('useUIExtension called outside of UIExtensionsContext');
}

const extension = registeredExtensions?.[packageName]?.[view];

if (extension) {
// FIXME:PT Revisit ignore below and see if TS error can be addressed
// @ts-ignore
if (typeof view === 'string') {
const extension = registeredExtensions?.[packageName]?.[view];
return extension;
}
};

return view.map((v) => registeredExtensions?.[packageName]?.[v]);
}

0 comments on commit 2f642c7

Please sign in to comment.