<commit_before>import Component from '@glimmer/component';
import Layer2Network from '../../../services/layer2-network';
import CardstackLogoForQR from '../../../images/icons/cardstack-logo-opaque-bg.svg';
import CardstackLogo from '../../../images/icons/cardstack.svg';
import CardstackMobileAppPhone from '../../../images/cardstack-mobile-app-phone.png';
import CardstackMobileAppPhone2x from '../../../images/cardstack-mobile-app-phone@2x.png';
import TestFlightIcon from '../../../images/test-flight-icon.png';
import TestFlightIcon2x from '../../../images/test-flight-icon@2x.png';
import config from '@cardstack/web-client/config/environment';
import { inject as service } from '@ember/service';
import { reads } from 'macro-decorators';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';
import { next } from '@ember/runloop';
import { timeout } from 'ember-concurrency';
import { action } from '@ember/object';
import { WorkflowCardComponentArgs } from '@cardstack/web-client/models/workflow/workflow-card';

interface CardPayLayerTwoConnectCardComponentArgs
  extends WorkflowCardComponentArgs {
  onConnect: (() => void) | undefined;
  onDisconnect: (() => void) | undefined;
}

class CardPayLayerTwoConnectCardComponent extends Component<CardPayLayerTwoConnectCardComponentArgs> {
  @service declare layer2Network: Layer2Network;
  cardstackLogoForQR = CardstackLogoForQR;
  cardstackLogo = CardstackLogo;
  cardstackMobileAppPhone = CardstackMobileAppPhone;
  cardstackMobileAppPhone2x = CardstackMobileAppPhone2x;
  testFlightIcon = TestFlightIcon;
  testFlightIcon2x = TestFlightIcon2x;
  testFlightLink = config.urls.testFlightLink;

  @reads('layer2Network.isConnected') declare isConnected: boolean;
  constructor(owner: unknown, args: CardPayLayerTwoConnectCardComponentArgs) {
    super(owner, args);
    if (this.isConnected) {
      next(this, () => {
        this.args.onComplete?.();
      });
    }
    if (!this.isConnected) {
      taskFor(this.connectWalletTask).perform();
    }
  }

  get balancesToShow() {
    return [
      {
        symbol: 'DAI.CPXD',
        amount: this.layer2Network.defaultTokenBalance,
      },
      {
        symbol: 'CARD.CPXD',
        amount: this.layer2Network.cardBalance,
      },
    ].filter((o) => o.amount && !o.amount?.isZero());
  }

  @task *connectWalletTask() {
    yield this.layer2Network.waitForAccount;
    yield timeout(500); // allow time for strategy to verify connected chain -- it might not accept the connection
    if (this.isConnected) {
      this.args.onConnect?.();
      this.args.onComplete?.();
    }
  }

  @action disconnect() {
    this.layer2Network.disconnect();
  }

  @action onDisconnect() {
    this.args.onDisconnect?.();
    taskFor(this.connectWalletTask).perform();
  }

  get cardState(): string {
    if (this.isConnected || this.args.isComplete) {
      return 'memorialized';
    } else {
      return 'default';
    }
  }
}

export default CardPayLayerTwoConnectCardComponent;
<commit_msg>Persist layer2 address<commit_after>import Component from '@glimmer/component';
import Layer2Network from '../../../services/layer2-network';
import CardstackLogoForQR from '../../../images/icons/cardstack-logo-opaque-bg.svg';
import CardstackLogo from '../../../images/icons/cardstack.svg';
import CardstackMobileAppPhone from '../../../images/cardstack-mobile-app-phone.png';
import CardstackMobileAppPhone2x from '../../../images/cardstack-mobile-app-phone@2x.png';
import TestFlightIcon from '../../../images/test-flight-icon.png';
import TestFlightIcon2x from '../../../images/test-flight-icon@2x.png';
import config from '@cardstack/web-client/config/environment';
import { inject as service } from '@ember/service';
import { reads } from 'macro-decorators';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';
import { next } from '@ember/runloop';
import { timeout } from 'ember-concurrency';
import { action } from '@ember/object';
import { WorkflowCardComponentArgs } from '@cardstack/web-client/models/workflow/workflow-card';

interface CardPayLayerTwoConnectCardComponentArgs
  extends WorkflowCardComponentArgs {
  onConnect: (() => void) | undefined;
  onDisconnect: (() => void) | undefined;
}

class CardPayLayerTwoConnectCardComponent extends Component<CardPayLayerTwoConnectCardComponentArgs> {
  @service declare layer2Network: Layer2Network;
  cardstackLogoForQR = CardstackLogoForQR;
  cardstackLogo = CardstackLogo;
  cardstackMobileAppPhone = CardstackMobileAppPhone;
  cardstackMobileAppPhone2x = CardstackMobileAppPhone2x;
  testFlightIcon = TestFlightIcon;
  testFlightIcon2x = TestFlightIcon2x;
  testFlightLink = config.urls.testFlightLink;

  @reads('layer2Network.isConnected') declare isConnected: boolean;
  constructor(owner: unknown, args: CardPayLayerTwoConnectCardComponentArgs) {
    super(owner, args);
    if (this.isConnected) {
      next(this, () => {
        this.args.onComplete?.();
      });
    }
    if (!this.isConnected) {
      taskFor(this.connectWalletTask).perform();
    }
  }

  get balancesToShow() {
    return [
      {
        symbol: 'DAI.CPXD',
        amount: this.layer2Network.defaultTokenBalance,
      },
      {
        symbol: 'CARD.CPXD',
        amount: this.layer2Network.cardBalance,
      },
    ].filter((o) => o.amount && !o.amount?.isZero());
  }

  @task *connectWalletTask() {
    yield this.layer2Network.waitForAccount;
    yield timeout(500); // allow time for strategy to verify connected chain -- it might not accept the connection
    if (this.isConnected) {
      this.args.onConnect?.();
      this.args.workflowSession.update(
        'layer2WalletAddress',
        this.layer2Network.walletInfo.firstAddress
      );
      this.args.onComplete?.();
    }
  }

  @action disconnect() {
    this.layer2Network.disconnect();
  }

  @action onDisconnect() {
    this.args.onDisconnect?.();
    taskFor(this.connectWalletTask).perform();
  }

  get cardState(): string {
    if (this.isConnected || this.args.isComplete) {
      return 'memorialized';
    } else {
      return 'default';
    }
  }
}

export default CardPayLayerTwoConnectCardComponent;

{'fraction_non_alphanumeric': 0.1369467607440667, 'fraction_numerical': 0.006895445798588839, 'mean_word_length': 3.7719969395562356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5398331', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1896, 'n_words': 361}