Skip to content

Conversation

hughns
Copy link
Member

@hughns hughns commented May 9, 2025

This PR adds a feature to allow a plan management tab to be added to the My Account screens.

When enabled it looks like this:

image

It attempts to resize the iframe to match the size of the embedded content. However, in current testing this has a habit of growing over time.

@hughns hughns added A-Account-Management Related to self-service account management T-Enhancement New feature of request labels May 9, 2025
Copy link

cloudflare-workers-and-pages bot commented May 9, 2025

Deploying matrix-authentication-service-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 55f559e
Status: ✅  Deploy successful!
Preview URL: https://de2da643.matrix-authentication-service-docs.pages.dev
Branch Preview URL: https://hughns-plan-management.matrix-authentication-service-docs.pages.dev

View logs

@hughns hughns marked this pull request as draft May 9, 2025 09:42
@hughns hughns force-pushed the hughns/plan-management branch from 5a71d49 to 26af568 Compare May 9, 2025 10:07
@hughns hughns marked this pull request as ready for review May 9, 2025 10:24
@hughns hughns requested a review from sandhose June 2, 2025 13:06
Copy link
Member

@sandhose sandhose left a comment

Choose a reason for hiding this comment

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

Looks alright, a few nitpicks on how things should be on the react side

Comment on lines 60 to 68
useEffect(() => {
calculateHeight();

const interval = setInterval(() => {
calculateHeight();
}, 1000);

return () => clearInterval(interval);
}, [calculateHeight]);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of polling, we could use a MutationObserver to recalculate. Plus, to setup the observer with a ref callback, so that we get it as soon as we have the DOM node ready.

We also don't need to track the iframe height as state to avoid re-renders, so we end up with something like

  const calculateHeight = useCallback((iframe: HTMLIFrameElement) => {
    const height =
      iframe.contentWindow?.document.body.parentElement?.scrollHeight;

    if (height) {
      iframe.height = `${height}px`;
    } else {
      iframe.height = "500px";
    }
  }, []);

  const ref: Ref<HTMLIFrameElement> = useCallback(
    (iframe: HTMLIFrameElement) => {
      calculateHeight(iframe);

      if (iframe.contentWindow) {
        const iframeDocument = iframe.contentWindow.document;

        const observer = new MutationObserver((_mutationsList) => {
          calculateHeight(iframe);
        });

        observer.observe(iframeDocument.body, {
          childList: true,
          subtree: true,
          attributes: true,
        });

        return () => observer.disconnect();
      }
    },
    [calculateHeight],
  );

  return (
    <iframe
      title="iframe" // no proper title as this is experimental feature
      ref={ref}
      onLoad={(e) => calculateHeight(e.target as HTMLIFrameElement)}
      src={planManagementIframeUri}
      scrolling="no"
    />
  );

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't get the observer to fire after set up. I've tried with a ResizeObserver too. Testing on Chrome and Safari. I will try again tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

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

I found two problems:

  1. The node corresponding to the iframe document body changed, so we weren't always observing the right thing. The fix was to re-observe after the iframe has loaded.
  2. Some rendering changes do not correspond to a mutation on the iframe document body DOM. This is fixed by doing an extra calculation after a delay.

I tried using the resize observer again, but it still didn't fire as expected.


/// Experimental feature to show a plan management tab and iframe
#[serde(skip_serializing_if = "Option::is_none")]
pub plan_management_iframe_uri: Option<Url>,
Copy link
Member

Choose a reason for hiding this comment

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

Not convinced that this has to be a URL? Like if we want to have a relative link here, it won't get accepted?

Copy link
Member Author

@hughns hughns Jun 4, 2025

Choose a reason for hiding this comment

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

I suppose someone might want to configure it with a relative URL.

Are you thinking that it is just a string that is passed through without validation? Or are you thinking the value is parsed from config relative to http.public_base and then the site config still return a full URL? Or something else entirely?

Copy link
Member

Choose a reason for hiding this comment

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

I'm indeed thinking of just passing it as as string without validation

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm indeed thinking of just passing it as as string without validation

I've converted into a String with some additional doc comments in d6dd647

@hughns hughns requested a review from sandhose June 5, 2025 10:35
Copy link
Member

@sandhose sandhose left a comment

Choose a reason for hiding this comment

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

Grr, I had this not submitted since yesterday

@hughns hughns requested a review from sandhose June 6, 2025 10:01
Copy link
Member

@sandhose sandhose left a comment

Choose a reason for hiding this comment

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

This looks great, thank you!

@sandhose sandhose merged commit 8df03bf into main Jun 10, 2025
20 checks passed
@sandhose sandhose deleted the hughns/plan-management branch June 10, 2025 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Account-Management Related to self-service account management T-Enhancement New feature of request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants