@using ClassObj

Welcome, @RemoteIpAddress!

@code {
  [Inject] public IPAddressService IPAddressService { get; set; } = default!;
  [Inject] public PersistentComponentState ApplicationState { get; set; } = default!;

  private bool _subsequentRender;
  private const string TokenName = "IPAddress";
  private string RemoteIpAddress = "Not Set";

  // Short circuit all the lifecycle stuff - we don't need it
  public override Task SetParametersAsync(ParameterView parameters)
  {
    if (_subsequentRender)
      return Task.CompletedTask;

    // if not prerender, try and get the persisted value
    if (this.ApplicationState.TryTakeFromJson<string>(IPAddressService.TokenName, out var address))
    {
      this.RemoteIpAddress = address ?? "Not Set";
      this.IPAddressService.RemoteIpAddress = this.RemoteIpAddress;
    }

    _subsequentRender = true;
    return Task.CompletedTask;
  }
}
