Skip to content

Commit 8688efa

Browse files
committed
Proper Refresh Fix For Imports
1 parent 4235142 commit 8688efa

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

frontend/static/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ let huntarrUI = {
681681
window.CycleCountdown.cleanup();
682682
}
683683

684+
// Clean up Media Hunt collection when leaving (stops refresh interval and visibility listener)
685+
if (this.currentSection === 'media-hunt-collection' && section !== 'media-hunt-collection' && window.MediaHuntCollection && typeof window.MediaHuntCollection.cleanup === 'function') {
686+
window.MediaHuntCollection.cleanup();
687+
}
688+
684689
// Update active section
685690
this.elements.sections.forEach(s => {
686691
s.classList.remove('active');

frontend/static/js/dist/bundle-app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ let huntarrUI = {
683683
window.CycleCountdown.cleanup();
684684
}
685685

686+
// Clean up Media Hunt collection when leaving (stops refresh interval and visibility listener)
687+
if (this.currentSection === 'media-hunt-collection' && section !== 'media-hunt-collection' && window.MediaHuntCollection && typeof window.MediaHuntCollection.cleanup === 'function') {
688+
window.MediaHuntCollection.cleanup();
689+
}
690+
686691
// Update active section
687692
this.elements.sections.forEach(s => {
688693
s.classList.remove('active');

frontend/static/js/dist/bundle-media.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,6 +3911,35 @@
39113911
if (window.TVHuntCollection && typeof window.TVHuntCollection.setupBackButton === 'function') {
39123912
window.TVHuntCollection.setupBackButton();
39133913
}
3914+
3915+
// Remove any existing refresh listeners before adding new ones
3916+
self.cleanup();
3917+
3918+
// Refresh collection when window regains focus (e.g. after import completes in another tab)
3919+
self._onVisibilityChange = function() {
3920+
if (document.visibilityState === 'visible' && self._movieInstanceId || self._tvInstanceId) {
3921+
self.loadCombinedCollection();
3922+
}
3923+
};
3924+
document.addEventListener('visibilitychange', self._onVisibilityChange);
3925+
3926+
// Periodic refresh while on collection view (catches imports that complete in background)
3927+
self._collectionRefreshInterval = setInterval(function() {
3928+
if (document.visibilityState === 'visible' && (self._movieInstanceId || self._tvInstanceId)) {
3929+
self.loadCombinedCollection();
3930+
}
3931+
}, 90000); // 90 seconds
3932+
},
3933+
3934+
cleanup: function() {
3935+
if (this._onVisibilityChange) {
3936+
document.removeEventListener('visibilitychange', this._onVisibilityChange);
3937+
this._onVisibilityChange = null;
3938+
}
3939+
if (this._collectionRefreshInterval) {
3940+
clearInterval(this._collectionRefreshInterval);
3941+
this._collectionRefreshInterval = null;
3942+
}
39143943
},
39153944

39163945
loadHiddenMediaIds: function() {

frontend/static/js/modules/features/media-hunt-collection.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,35 @@
11721172
if (window.TVHuntCollection && typeof window.TVHuntCollection.setupBackButton === 'function') {
11731173
window.TVHuntCollection.setupBackButton();
11741174
}
1175+
1176+
// Remove any existing refresh listeners before adding new ones
1177+
self.cleanup();
1178+
1179+
// Refresh collection when window regains focus (e.g. after import completes in another tab)
1180+
self._onVisibilityChange = function() {
1181+
if (document.visibilityState === 'visible' && self._movieInstanceId || self._tvInstanceId) {
1182+
self.loadCombinedCollection();
1183+
}
1184+
};
1185+
document.addEventListener('visibilitychange', self._onVisibilityChange);
1186+
1187+
// Periodic refresh while on collection view (catches imports that complete in background)
1188+
self._collectionRefreshInterval = setInterval(function() {
1189+
if (document.visibilityState === 'visible' && (self._movieInstanceId || self._tvInstanceId)) {
1190+
self.loadCombinedCollection();
1191+
}
1192+
}, 90000); // 90 seconds
1193+
},
1194+
1195+
cleanup: function() {
1196+
if (this._onVisibilityChange) {
1197+
document.removeEventListener('visibilitychange', this._onVisibilityChange);
1198+
this._onVisibilityChange = null;
1199+
}
1200+
if (this._collectionRefreshInterval) {
1201+
clearInterval(this._collectionRefreshInterval);
1202+
this._collectionRefreshInterval = null;
1203+
}
11751204
},
11761205

11771206
loadHiddenMediaIds: function() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!-- Bundled scripts (run: python scripts/build_js_bundles.py to rebuild) -->
22
<script defer src="./static/js/dist/bundle-core.js?v=14"></script>
3-
<script defer src="./static/js/dist/bundle-app.js?v=21"></script>
3+
<script defer src="./static/js/dist/bundle-app.js?v=22"></script>
44
<script defer src="./static/js/dist/bundle-settings.js?v=39"></script>
55
<script defer src="./static/js/dist/bundle-features.js?v=17"></script>
6-
<script defer src="./static/js/dist/bundle-media.js?v=19"></script>
6+
<script defer src="./static/js/dist/bundle-media.js?v=20"></script>
77
<script defer src="./static/js/dist/bundle-misc.js?v=21"></script>
88
<!-- Requestarr (Vite-bundled, minified) -->
99
<script defer src="./static/js/dist/requestarr-bundle.js?v=22"></script>

src/primary/apps/requestarr_routes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ def get_unified_collection():
139139
# Fetch TV collection directly from database (test_client is unreliable)
140140
if tv_instance_id:
141141
try:
142-
from src.primary.routes.media_hunt.discovery_tv import _get_collection_config
142+
from src.primary.routes.media_hunt.discovery_tv import _get_collection_config, _merge_detected_episodes_into_collection
143143
try:
144144
tv_id_int = int(tv_instance_id)
145145
except (TypeError, ValueError):
146146
tv_id_int = 0
147147
if tv_id_int:
148148
series_list = _get_collection_config(tv_id_int)
149+
# Merge disk-detected episodes so imported files show correct status on hard refresh
150+
_merge_detected_episodes_into_collection(tv_id_int, series_list)
149151
for s in series_list:
150152
title = s.get('title') or s.get('name') or ''
151153
year = (s.get('first_air_date') or '')[:4]

src/primary/apps/tv_hunt/importer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ def import_episode(client: Dict[str, Any], series_title: str, year: str,
362362
_cleanup_source_folder(local_path, root_folder, series_title, season, episode)
363363
except Exception:
364364
pass
365+
if instance_id:
366+
try:
367+
from src.primary.routes.media_hunt.discovery_tv import _merge_detected_episodes_into_collection
368+
_merge_detected_episodes_into_collection(instance_id)
369+
except Exception as e:
370+
_tv_log().warning("Import: post-import merge failed: %s", e)
365371
return True
366372

367373
# 6. Move file
@@ -386,6 +392,15 @@ def import_episode(client: Dict[str, Any], series_title: str, year: str,
386392
except Exception:
387393
pass
388394

395+
# 10. Merge detected episodes from disk into collection (refresh scan)
396+
# Ensures collection status is correct for hard refresh / next page load
397+
if instance_id:
398+
try:
399+
from src.primary.routes.media_hunt.discovery_tv import _merge_detected_episodes_into_collection
400+
_merge_detected_episodes_into_collection(instance_id)
401+
except Exception as e:
402+
_tv_log().warning("Import: post-import merge failed: %s", e)
403+
389404
_tv_log().info("Import: completed '%s' S%02dE%02d -> %s",
390405
series_title, season or 0, episode or 0, dest_file)
391406
return True

0 commit comments

Comments
 (0)