Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before hook for onRun of Catalog entities #3911

Merged
merged 38 commits into from
Oct 5, 2021

Conversation

chenhunghan
Copy link
Contributor

@chenhunghan chenhunghan commented Sep 29, 2021

  • Add extension api Renderer.Catalog.catalogEntities.addOnRunHook

Can be used like

Renderer.Catalog.catalogEntities.addOnRunHook(catalogEntityUid, (entity) => {
   // return true => invoke onRun, false => don't invoke onRun;
   return true;
});
  • Add prettier to be able to use .toMatchInlineSnapshot()

@chenhunghan chenhunghan added enhancement New feature or request area/catalog labels Sep 29, 2021
@chenhunghan chenhunghan requested a review from a team as a code owner September 29, 2021 13:17
@chenhunghan chenhunghan requested review from jweak and jim-docker and removed request for a team September 29, 2021 13:17
package.json Show resolved Hide resolved
src/common/catalog/catalog-entity.ts Outdated Show resolved Hide resolved
src/main/catalog-sources/weblinks.ts Outdated Show resolved Hide resolved
Comment on lines 49 to 54
onClickDetailIcon(context: CatalogEntityActionContext) {
catalogCategoryRegistry
.getCategoryForEntity<WebLinkCategory>(this)
?.emit("onClickDetailIcon", this, context);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From talking to @msa0311 I was under the impression that we were wanting to block the "onRun" if any of these pre-hooks calls the equivalent of preventDefault which this form cannot support because the handlers are run concurrently.

@jim-docker
Copy link
Contributor

I don't understand the need for this but instead of adding onClickDetailIcon can't you add something to the run context that lets onRun determine if the icon was clicked? Breaking change?

@Nokel81
Copy link
Collaborator

Nokel81 commented Sep 29, 2021

I don't understand the need for this but instead of adding onClickDetailIcon can't you add something to the run context that lets onRun determine if the icon was clicked? Breaking change?

That would also be a possible solution. However, according to @msa0311 this is desirable to happen on attempts for onRun. So from that POV the name of even this proposed function is wrong.

@msa0311 msa0311 added this to the 5.3.0 milestone Sep 30, 2021
@msa0311 msa0311 linked an issue Sep 30, 2021 that may be closed by this pull request
@chenhunghan chenhunghan changed the title Emit "onClickDetailIcon" when user clicks the icon in detail panel Hook for onRun of Catalog entities Sep 30, 2021
@chenhunghan chenhunghan force-pushed the feat/emit-on-click-detail-icon branch from bcf0480 to d6f5517 Compare September 30, 2021 11:19
@chenhunghan chenhunghan reopened this Sep 30, 2021
@chenhunghan
Copy link
Contributor Author

I don't understand the need for this but instead of adding onClickDetailIcon can't you add something to the run context that lets onRun determine if the icon was clicked? Breaking change?

Because we need to add the hooks to onRun via extension, from extension there is no way of changing onRun...

@chenhunghan chenhunghan force-pushed the feat/emit-on-click-detail-icon branch from 9d37487 to 600ee9d Compare October 1, 2021 05:28
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
src/extensions/renderer-api/catalog.ts Outdated Show resolved Hide resolved
Comment on lines 62 to 68
/**
* Returns one catalog entity onRun hook by catalog entity uid
*/
getOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatelogEntityOnRunHook | undefined {
return registry.getOnRunHook(catalogEntityUid);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support several onRunHooks. You sort of do already but not very well since you only ever return the first hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be another PR,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because this will have to be supported until the next major

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in current timeframe I don't think I can do this before 5.3. @Nokel81 would you like to to take over on the multiple hooks part?

src/renderer/api/catalog-entity-registry.ts Outdated Show resolved Hide resolved
src/renderer/api/catalog-entity-registry.ts Show resolved Hide resolved
Comment on lines 43 to 48
protected entityOnRunHooks = observable.set<{
catalogEntityUid: CatalogEntityUid;
onRunHook: CatelogEntityOnRunHook
}>([], {
deep: false,
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very strange type, why is it not observable.map<string, CatalogEnitytOnRunHook[]>([], { deep: false });?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need catalogEntityUid because there are getOnRunHook

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The top level map's key would be CatalogEntityUid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string is not as obvious as catalogEntityUid, but again, just my opinion, maybe you know more about the convention in Lens.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really have a convention... (namely we use both styles). What I meant was I should have suggested the type observable.map<CatalogEntityUid, IObservableSet<CatelogEntityOnRunHook>>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for clarify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed c863134

onClick={() => item.onRun(catalogEntityRunContext)}
size={128} />
onClick={() => {
this.props.onClickDetailPanelIcon(item);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should pass this in as props, we should have an unexported function that takes an CatalogEntity and does the work once (DRY as it were).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the refactoring be another PR?

src/renderer/components/+catalog/catalog-entity-item.tsx Outdated Show resolved Hide resolved
Comment on lines 108 to 114
if (!onRunHook) {
this.entity.onRun(ctx);

return;
}

if (typeof onRunHook === "function") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two checks can be combined:

if (typeof onRunHook !== "function") {
  ...
  return;
}

...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but with less readability IMHO.

* @param onRunHook The function that should return a boolean if the onRun of catalog entity should be triggered.
* @returns A function to remove that hook
*/
addOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"], onRunHook: CatelogEntityOnRunHook) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be instead called onBeforeRun so as to be more descriptive of when it will be run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to support multiple hooks in the future, onBeforeRun is less descriptive IMO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it less descriptive? It better shows when we plan on running them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's just my opinion, if you think "onBeforeRun" is better, I would just change it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the argument would be onBeforeRun and this method would be addOnBeforeRun

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, addOnBeforeRun makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited 74eb89c

Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
@@ -114,14 +114,14 @@ export class Icon extends React.PureComponent<IconProps> {
};

// render as inline svg-icon
if (svg) {
if (typeof svg === "string") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here just for easier mocking in tests (less crashes in components)

@@ -77,7 +77,7 @@ export class LensRendererExtension extends LensExtension {
}

/**
* Add a filtering function for the catalog catogries. This will be removed if the extension is disabled.
* Add a filtering function for the catalog categories. This will be removed if the extension is disabled.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a typo.

@chenhunghan chenhunghan requested a review from jakolehm October 5, 2021 07:01
chenhunghan and others added 4 commits October 5, 2021 16:11
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
Signed-off-by: Sebastian Malton <[email protected]>
Copy link
Contributor

@jim-docker jim-docker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed @Nokel81 's commits, LGTM but my head is spinning. @Nokel81 should give the final approval...

Signed-off-by: Sebastian Malton <[email protected]>
Signed-off-by: Sebastian Malton <[email protected]>
Signed-off-by: Sebastian Malton <[email protected]>
@Nokel81 Nokel81 requested a review from jim-docker October 5, 2021 19:17
@Nokel81 Nokel81 changed the title Hook for onRun of Catalog entities Before hook for onRun of Catalog entities Oct 5, 2021
@Nokel81 Nokel81 merged commit 4af796c into master Oct 5, 2021
@Nokel81 Nokel81 deleted the feat/emit-on-click-detail-icon branch October 5, 2021 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hook for onRun of Catalog Entities
5 participants