/
ListIssues.php
172 lines (143 loc) · 5.14 KB
/
ListIssues.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace App\Livewire;
use App\DataTransferObjects\Issue;
use App\DataTransferObjects\Repository;
use App\Exceptions\GitHubRateLimitException;
use App\Services\IssueService;
use App\Services\RepoService;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cookie;
use Livewire\Component;
final class ListIssues extends Component
{
private const SORTS = [
'random' => [
'friendly' => 'Random', 'field' => 'random',
],
'created_at' => [
'friendly' => 'Created At (Oldest First)', 'field' => 'createdAt', 'direction' => 'asc',
],
'created_at_desc' => [
'friendly' => 'Created At (Newest First)', 'field' => 'createdAt', 'direction' => 'desc',
],
'title' => [
'friendly' => 'Title (A-Z)', 'field' => 'title', 'direction' => 'asc',
],
'title_desc' => [
'friendly' => 'Title (Z-A)', 'field' => 'title', 'direction' => 'desc',
],
'repo_name' => [
'friendly' => 'Repo Name (A-Z)', 'field' => 'repoName', 'direction' => 'asc',
],
'repo_name_desc' => [
'friendly' => 'Repo Name (Z-A)', 'field' => 'repoName', 'direction' => 'desc',
],
'interactions' => [
'friendly' => 'Interactions (Least first)', 'field' => 'interactionsCount', 'direction' => 'asc',
],
'interactions_desc' => [
'friendly' => 'Interactions (Most first)', 'field' => 'interactionsCount', 'direction' => 'desc',
],
];
protected $queryString = [
'sortField' => ['except' => 'random'],
'sortDirection' => ['except' => 'asc'],
'searchTerm' => ['except' => ''],
'showIgnoredIssues' => ['except' => false],
];
public array $labels;
/**
* @var Collection<Repository>
*/
public Collection $repos;
/**
* A collection of the issues when the component was first mounted.
*
* @var Collection<Issue>
*/
public Collection $originalIssues;
/**
* @var array<string>
*/
public array $ignoredUrls = [];
public ?string $sortField = null;
public string $sortDirection = 'asc';
public ?string $sort = null;
public ?string $searchTerm = null;
public bool $shouldDisplayFirstTimeNotice;
public bool $showIgnoredIssues = false;
public function mount(): void
{
$this->setSortOrderOnPageLoad();
$this->labels = config('repos.labels');
$this->repos = app(RepoService::class)->reposToCrawl()->sort();
try {
$this->originalIssues = app(IssueService::class)->getAll()->shuffle();
} catch (GitHubRateLimitException $e) {
abort(503, $e->getMessage());
}
$this->shouldDisplayFirstTimeNotice = ! Cookie::get('firstTimeNoticeClosed');
}
public function render(): View
{
$issues = $this->originalIssues
->filter(fn (Issue $issue): bool => $this->showIgnoredIssues === in_array($issue->url, $this->ignoredUrls, true))
->when($this->searchTerm, $this->applySearch())
->when($this->sort, $this->applySort());
return view('livewire.list-issues', [
'issues' => $issues,
'sorts' => self::SORTS,
]);
}
public function updatedSort(string $newSort): void
{
if (array_key_exists($newSort, self::SORTS)) {
$this->sortField = self::SORTS[$newSort]['field'];
$this->sortDirection = self::SORTS[$newSort]['direction'] ?? 'asc';
}
}
private function applySearch(): \Closure
{
return static function (Collection $issues, string $searchTerm): Collection {
$searchTerm = strtolower($searchTerm);
return $issues->filter(function (Issue $issue) use ($searchTerm): bool {
return str_contains(strtolower($issue->repoName), $searchTerm)
|| str_contains(strtolower($issue->title), $searchTerm);
});
};
}
private function applySort(): \Closure
{
return function (Collection $issues): Collection {
return $issues->sortBy($this->sortField, descending: $this->sortDirection === 'desc');
};
}
public function updatedShouldDisplayFirstTimeNotice(): void
{
// 4 weeks = 40,320 minutes
Cookie::queue('firstTimeNoticeClosed', true, 40_320);
}
public function updateIgnoredUrls(array $urls): void
{
$this->ignoredUrls = collect($urls)
->filter(function (string $url): bool {
return $this->originalIssues->contains(fn (Issue $issue): bool => $url === $issue->url);
})
->toArray();
if (! $this->ignoredUrls) {
$this->showIgnoredIssues = false;
}
}
private function setSortOrderOnPageLoad(): void
{
if ($this->sortField) {
$this->sort = collect(self::SORTS)
->where('field', $this->sortField)
->where('direction', $this->sortDirection)
->keys()
->first();
}
}
}