private buildVariableStatements()

in packages/codegen-ui-react/lib/react-studio-template-renderer.ts [444:544]


  private buildVariableStatements(component: StudioComponent): Statement[] {
    const statements: Statement[] = [];
    const elements: BindingElement[] = [];
    if (isStudioComponentWithBinding(component)) {
      Object.entries(component.bindingProperties).forEach((entry) => {
        const [propName, binding] = entry;
        if (isSimplePropertyBinding(binding) || isDataPropertyBinding(binding)) {
          const usesHook = bindingPropertyUsesHook(binding);
          const bindingElement = factory.createBindingElement(
            undefined,
            usesHook ? factory.createIdentifier(propName) : undefined,
            factory.createIdentifier(usesHook ? `${propName}Prop` : propName),
            isSimplePropertyBinding(binding) ? this.getDefaultValue(binding) : undefined,
          );
          elements.push(bindingElement);
        }
      });
    }

    if (component.componentType === 'Collection') {
      const bindingElement = this.hasCollectionPropertyNamedItems(component)
        ? factory.createBindingElement(
            undefined,
            factory.createIdentifier('items'),
            factory.createIdentifier('itemsProp'),
            undefined,
          )
        : factory.createBindingElement(undefined, undefined, factory.createIdentifier('items'), undefined);
      elements.push(bindingElement);
    }

    // remove overrides from rest of props
    elements.push(
      factory.createBindingElement(
        undefined,
        factory.createIdentifier('overrides'),
        factory.createIdentifier('overridesProp'),
        undefined,
      ),
    );

    // get rest of props to pass to top level component
    elements.push(
      factory.createBindingElement(
        factory.createToken(ts.SyntaxKind.DotDotDotToken),
        undefined,
        factory.createIdentifier('rest'),
        undefined,
      ),
    );

    const statement = factory.createVariableStatement(
      undefined,
      factory.createVariableDeclarationList(
        [
          factory.createVariableDeclaration(
            factory.createObjectBindingPattern(elements),
            undefined,
            undefined,
            factory.createIdentifier('props'),
          ),
        ],
        ts.NodeFlags.Const,
      ),
    );
    statements.push(statement);

    if (isStudioComponentWithVariants(component)) {
      statements.push(this.buildVariantDeclaration(component.variants));
      // TODO: In components, replace props.override with override (defined here).
    }

    if (isStudioComponentWithVariants(component)) {
      statements.push(this.buildMergeOverridesFunction());
    }

    statements.push(this.buildOverridesDeclaration(isStudioComponentWithVariants(component)));

    const authStatement = this.buildUseAuthenticatedUserStatement(component);
    if (authStatement !== undefined) {
      this.importCollection.addMappedImport(ImportValue.USE_AUTH);
      statements.push(authStatement);
    }

    const collectionBindingStatements = this.buildCollectionBindingStatements(component);
    collectionBindingStatements.forEach((entry) => {
      statements.push(entry);
    });

    const useStoreBindingStatements = this.buildUseDataStoreBindingStatements(component);
    useStoreBindingStatements.forEach((entry) => {
      statements.push(entry);
    });

    const actionStatement = this.buildUseActionsStatement(component);
    if (actionStatement !== undefined) {
      statements.push(actionStatement);
    }

    return statements;
  }