Skip to content

Commit 652ebb9

Browse files
committed
fix: async changelog prefetch
chore: poll changelog sooner chore: cap changelog polling
1 parent d020dee commit 652ebb9

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

app/Livewire/SettingsDropdown.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class SettingsDropdown extends Component
1111
{
1212
public $showWhatsNewModal = false;
1313

14+
public bool $shouldPollChangelog = false;
15+
16+
public bool $changelogFetchRequested = false;
17+
18+
public int $changelogPollAttempts = 0;
19+
1420
public function getUnreadCountProperty()
1521
{
1622
return Auth::user()->getUnreadChangelogCount();
@@ -31,6 +37,53 @@ public function getCurrentVersionProperty()
3137
public function openWhatsNewModal()
3238
{
3339
$this->showWhatsNewModal = true;
40+
41+
$this->prefetchChangelog();
42+
}
43+
44+
public function prefetchChangelog()
45+
{
46+
$entries = app(ChangelogService::class)->getEntriesForUser(Auth::user());
47+
if ($entries->isEmpty()) {
48+
if (! $this->changelogFetchRequested) {
49+
$this->changelogFetchRequested = true;
50+
$this->shouldPollChangelog = true;
51+
$this->changelogPollAttempts = 0;
52+
PullChangelog::dispatch();
53+
}
54+
55+
return;
56+
}
57+
58+
$this->dispatch('changelog-entries', entries: $entries->values()->all());
59+
}
60+
61+
public function refreshChangelog()
62+
{
63+
if (! $this->shouldPollChangelog) {
64+
return;
65+
}
66+
67+
$this->changelogPollAttempts++;
68+
if ($this->changelogPollAttempts > 10) {
69+
$this->shouldPollChangelog = false;
70+
$this->changelogFetchRequested = false;
71+
72+
return;
73+
}
74+
75+
$entries = app(ChangelogService::class)->getEntriesForUser(Auth::user());
76+
if ($entries->isEmpty()) {
77+
return;
78+
}
79+
80+
$this->shouldPollChangelog = false;
81+
$this->changelogFetchRequested = false;
82+
$this->changelogPollAttempts = 0;
83+
$this->dispatch('changelog-entries', entries: $entries->values()->all());
84+
85+
cache()->forget('user_unread_changelog_count_'.Auth::id());
86+
$this->dispatch('$refresh');
3487
}
3588

3689
public function closeWhatsNewModal()

resources/views/livewire/settings-dropdown.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
this.mounted();
99
// Load all entries when component initializes
1010
this.allEntries = @js($entries->toArray());
11+
window.addEventListener('changelog-entries', (event) => {
12+
this.allEntries = event.detail.entries ?? [];
13+
});
1114
},
1215
markEntryAsRead(tagName) {
1316
// Update the entry in our local Alpine data
@@ -103,7 +106,10 @@
103106
return new Date(b.published_at) - new Date(a.published_at);
104107
});
105108
}
106-
}" @click.outside="dropdownOpen = false">
109+
}" @click.outside="dropdownOpen = false" wire:init="prefetchChangelog">
110+
@if ($shouldPollChangelog)
111+
<span class="hidden" wire:poll.1s="refreshChangelog"></span>
112+
@endif
107113
<!-- Custom Dropdown without arrow -->
108114
<div class="relative">
109115
<button @click="dropdownOpen = !dropdownOpen"

0 commit comments

Comments
 (0)