ArchiveView.razor 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. @using WarcViewerBlazorWinForm.Backend.IO.Archiving
  2. @using WarcViewerBlazorWinForm.Backend.Warc
  3. @using WarcViewerBlazorWinForm.Backend.IO
  4. @inject ViewerMainForm MainForm
  5. @inject WarcIndexManager IndexManager
  6. @inject IEventBus EventBus
  7. <ConditionBlock If="_selectedFile==null">
  8. <Button Color="Color.Primary" Icon="fa-solid fa-font-awesome" Text="Open..." @onclick="OpenFile"></Button>
  9. </ConditionBlock>
  10. <ConditionBlock If="_selectedFile != null">
  11. <Row>
  12. <Card>
  13. <HeaderTemplate>
  14. <div class="w-100">
  15. File: @_selectedFile
  16. <Button Color="Color.Danger" Icon="fa-solid fa-close" Text="Close" @onclick="CloseFile"></Button>
  17. <ConditionBlock If="_isDirectlyRead == false">
  18. <Button Color="Color.Primary" Icon="fa-solid fa-check" Text="Load Selected" @onclick="LoadSelectArchiveEntry"></Button>
  19. </ConditionBlock>
  20. </div>
  21. </HeaderTemplate>
  22. <BodyTemplate>
  23. <ConditionBlock If="_isDirectlyRead == null">
  24. Opening file...
  25. </ConditionBlock>
  26. <ConditionBlock If="_isDirectlyRead == true">
  27. TODO: read WARC directly
  28. </ConditionBlock>
  29. <ConditionBlock If="_isDirectlyRead == false">
  30. <div style="height:100%">
  31. <Table TItem="ArchiveEntryRow" TableSize="TableSize.Compact" OnQueryAsync="@DoQueryResult" @bind-SelectedRows="_selectedRows"
  32. ScrollMode="ScrollMode.Virtual" ShowLineNo LineNoText="#"
  33. IsBordered IsStriped IsFixedHeader ShowSkeleton IsMultipleSelect="true">
  34. <TableColumns>
  35. <TableColumn @bind-Field="@context.Name" Text="Name" Filterable></TableColumn>
  36. </TableColumns>
  37. </Table>
  38. </div>
  39. </ConditionBlock>
  40. </BodyTemplate>
  41. </Card>
  42. </Row>
  43. </ConditionBlock>
  44. @code {
  45. private string? _selectedFile;
  46. private bool? _isDirectlyRead;
  47. private ArchiveEntryRow[]? _rows;
  48. private List<ArchiveEntryRow> _selectedRows=new ();
  49. private async void OpenFile()
  50. {
  51. var open = MainForm.OpenFileWarc();
  52. if (open == null) return;
  53. _selectedFile = open;
  54. StateHasChanged();
  55. using var r = new FileReader();
  56. await r.LoadFileAsync(_selectedFile);
  57. StateHasChanged();
  58. if (r.IsDirectRead==true)
  59. {
  60. //TODO: read WARC directly
  61. }
  62. _isDirectlyRead = r.IsDirectRead;
  63. if (_isDirectlyRead == false && r.PackagedEntryNames.HasValue) _rows = r.PackagedEntryNames.Value.ToArray().Select((p, i) => new ArchiveEntryRow(i, p)).ToArray();
  64. StateHasChanged();
  65. }
  66. private void CloseFile() => _selectedFile = null;
  67. private Task<QueryData<ArchiveEntryRow>> DoQueryResult(QueryPageOptions options)
  68. {
  69. if (_rows == null)
  70. {
  71. return Task.FromResult(new QueryData<ArchiveEntryRow>
  72. {
  73. Items = Array.Empty<ArchiveEntryRow>(),
  74. TotalCount = 0,
  75. IsFiltered = false
  76. });
  77. }
  78. var filterFunc = options.Filters.GetFilterFunc<ArchiveEntryRow>();
  79. var data = _rows.Where(filterFunc).Skip(options.StartIndex).Take(options.PageItems);
  80. var ret = new QueryData<ArchiveEntryRow>
  81. {
  82. Items = data,
  83. TotalCount = _rows.Count(filterFunc),
  84. IsFiltered = options.Filters.Any()
  85. };
  86. return Task.FromResult(ret);
  87. }
  88. public void LoadSelectArchiveEntry()
  89. {
  90. if (_selectedFile == null) return; // WTF?
  91. var file = _selectedFile;
  92. _selectedFile = null;
  93. StateHasChanged();
  94. foreach (var row in _selectedRows)
  95. {
  96. var fd = new FileDescriptor(file!,_isDirectlyRead==true,row.Name);
  97. EventBus.Publish(new WarcIndexerLoadRequestEvent(fd));
  98. }
  99. }
  100. private class ArchiveEntryRow
  101. {
  102. public string Name { get; set; }
  103. public ArchiveEntryRow()
  104. {
  105. }
  106. public ArchiveEntryRow(int index, string name)
  107. {
  108. Name = name;
  109. }
  110. }
  111. }