RocketChat / Rocket.Chat.Apps-engine

The Rocket.Chat Apps engine and definitions.
https://rocketchat.github.io/Rocket.Chat.Apps-engine/
MIT License
117 stars 118 forks source link

IUIKitSurface State does not hold value if initialValue is used in a InputBlock #606

Open haemza30 opened 1 year ago

haemza30 commented 1 year ago

What?

When I am trying to read a value from IUIKitViewSubmitIncomingInteraction.IUIKitSurface.state: 1) if the value of say a input box is unchanged (it uses initialValue) then I get undefined 2) if the value of input box is altered (changed once from initialValue) I am successfully able to retrieve the value

Steps to reproduce?

  1. Create a block const block: InputBlock = { type: "input", label: { type: "plain_text", text: labelText, }, element: { type: "plain_text_input", placeholder: { type: "plain_text", text: 'Enter your ID', }, appId: 'app-id, blockId: 'block-id', actionId: 'action-id', initialValue: '12345' }, };

  2. Use the above block in UIKitSurfaceType.MODAL

  3. Try to retrieve the value of input box in the submit handler: const id = data.view.state.'block-id'.'action-id'; // here data is IUIKitViewSubmitIncomingInteraction

  4. If you don't change the initialValue of the input box you will get id as undefined, but if you even change a character in it it will work fine

PS

Expectation:

Must get value even if the input box has unchanged initialValue

eonae commented 3 weeks ago

I faced the same problem. The thing is that if value is unchanged it is contained in field name as UUID, instead of blockId.

image

Painful...

My workaround is something like this:

const getLeafValue = (obj: any, leafKey: string): any | undefined => {
  for (const [key, value] of Object.entries(obj)) {
    if (typeof value === 'object' && value !== null) {
      const inner = getLeafValue(value, leafKey);
      if (inner) {
        return inner;
      }
    }

    if (key === leafKey) {
      return value;
    }
  }

  return undefined;
};

const x = getLeafValue(
  {
    '0b4adc14-3fbb-4ae3-9a4f-9e906ce8ef11': {
      value1: '...',
    },
    'tg.username': {
      value2: '111',
    },
  },
  'value2',
);

console.log(x); // 111

Which works if using unique actionIds. But that definitely a dirty hack.