diff --git a/client.js b/client.js index 835faed..8f627dc 100644 --- a/client.js +++ b/client.js @@ -1926,7 +1926,27 @@ try { var activeVoiceRec = null; var isVoiceListening = false; - // Dynamic Search History & Trending Helpers + // Dynamic Search History & Trending Helpers with YakNet Cloud Sync + function isUserLoggedIn() { + try { + var token = localStorage.getItem('access_token'); + var username = localStorage.getItem('username'); + return Boolean(token || username); + } catch (e) { + return false; + } + } + + function isAccountSyncEnabled() { + try { + var userPref = localStorage.getItem('yaktube_sync_search_history'); + if (userPref === 'false') return false; + return isUserLoggedIn(); + } catch (e) { + return false; + } + } + function getRecentSearches() { try { var data = localStorage.getItem('yaktube_recent_searches'); @@ -1943,6 +1963,33 @@ try { return defaults; } + var isSyncingHistory = false; + function syncCloudSearchHistory(callback) { + if (!isAccountSyncEnabled() || isSyncingHistory) return; + isSyncingHistory = true; + + var token = localStorage.getItem('access_token'); + var headers = {}; + if (token) headers['Authorization'] = 'Bearer ' + token; + + fetch('/api-custom/search-history', { + method: 'GET', + headers: headers, + credentials: 'include' + }) + .then(function (res) { return res.json(); }) + .then(function (data) { + isSyncingHistory = false; + if (data && data.status === 'success' && Array.isArray(data.history) && data.history.length > 0) { + localStorage.setItem('yaktube_recent_searches', JSON.stringify(data.history)); + if (typeof callback === 'function') callback(); + } + }) + .catch(function () { + isSyncingHistory = false; + }); + } + function saveRecentSearch(query) { if (!query || query.trim().length < 2) return; var q = query.trim(); @@ -1952,15 +1999,42 @@ try { return item.toLowerCase() !== q.toLowerCase(); }); list.unshift(q); - if (list.length > 8) list = list.slice(0, 8); + if (list.length > 12) list = list.slice(0, 12); localStorage.setItem('yaktube_recent_searches', JSON.stringify(list)); } catch (e) {} + + // If user is logged in and sync is active, sync with YakNet account + if (isAccountSyncEnabled()) { + var token = localStorage.getItem('access_token'); + var headers = { 'Content-Type': 'application/json' }; + if (token) headers['Authorization'] = 'Bearer ' + token; + + fetch('/api-custom/search-history', { + method: 'POST', + headers: headers, + credentials: 'include', + body: JSON.stringify({ query: q }) + }).catch(function () {}); + } } function clearRecentSearches() { try { localStorage.removeItem('yaktube_recent_searches'); } catch (e) {} + + if (isAccountSyncEnabled()) { + var token = localStorage.getItem('access_token'); + var headers = { 'Content-Type': 'application/json' }; + if (token) headers['Authorization'] = 'Bearer ' + token; + + fetch('/api-custom/search-history', { + method: 'DELETE', + headers: headers, + credentials: 'include' + }).catch(function () {}); + } + renderDynamicSearchChips(); announce('Arama geçmişi temizlendi.'); } @@ -1998,17 +2072,49 @@ try { if (!container) return; var recent = getRecentSearches(); + var loggedIn = isUserLoggedIn(); + var syncEnabled = isAccountSyncEnabled(); var html = ''; - // 1. Recent searches section (if any) + // 1. Recent searches section + html += + '