1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/utils/is-allowed-resource.injectable.ts
Sebastian Malton 645fb23d05 Fix rerendering of KubeObject views every 30s
- This was being caused by the Cluster.refreshAllowedResources() on main

- Using comparer.deepEquals on the set of allowedResources dependency
  means that changes are not propogated unless required

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-02-02 16:59:20 -05:00

39 lines
1.2 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
import allowedResourcesInjectable from "../cluster-store/allowed-resources.injectable";
import type { KubeResource } from "../rbac";
export type IsAllowedResource = (...resources: KubeResource[]) => boolean;
interface Dependencies {
allowedResources: IComputedValue<Set<string>>;
}
const isAllowedResource = ({ allowedResources }: Dependencies) => (
(...resource) => {
const resources = resource.flat(2);
if (resources.length === 0) {
// Fix for the fact that JS's Array.every method is not the same as ∀
return true;
}
const allowed = allowedResources.get();
return resources.every(resource => allowed.has(resource));
}
) as IsAllowedResource;
const isAllowedResourceInjectable = getInjectable({
instantiate: (di) => isAllowedResource({
allowedResources: di.inject(allowedResourcesInjectable),
}),
lifecycle: lifecycleEnum.singleton,
});
export default isAllowedResourceInjectable;