From 9329e2279d1d216fe10a763db45e9a72b1dc6408 Mon Sep 17 00:00:00 2001 From: "K. Kyle Puchkov" Date: Tue, 7 Jan 2025 01:28:47 +0300 Subject: [PATCH] Refactor getDeviceName() for readability (#6319) * Refactor getDeviceName() for readability Refactored a long if-else into a cleaner, easier to extend mapping * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Move BrowserName to the top of the file Renamed deviceMappings to BrowserName and moved it under the appName declaration as requested --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- CONTRIBUTORS.md | 1 + src/components/apphost.js | 62 +++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4e5e14b873..f73bfe3d4e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -93,6 +93,7 @@ - [Connor Smith](https://github.com/ConnorS1110) - [iFraan](https://github.com/iFraan) - [Ali](https://github.com/bu3alwa) +- [K. Kyle Puchkov](https://github.com/kepper104) ## Emby Contributors diff --git a/src/components/apphost.js b/src/components/apphost.js index b92bb6faa0..984c3a77bf 100644 --- a/src/components/apphost.js +++ b/src/components/apphost.js @@ -8,6 +8,20 @@ import profileBuilder from '../scripts/browserDeviceProfile'; const appName = 'Jellyfin Web'; +const BrowserName = { + tizen: 'Samsung Smart TV', + web0s: 'LG Smart TV', + operaTv: 'Opera TV', + xboxOne: 'Xbox One', + ps4: 'Sony PS4', + chrome: 'Chrome', + edgeChromium: 'Edge Chromium', + edge: 'Edge', + firefox: 'Firefox', + opera: 'Opera', + safari: 'Safari' +}; + function getBaseProfileOptions(item) { const disableHlsVideoAudioCodecs = []; @@ -132,42 +146,26 @@ function getDeviceId() { } function getDeviceName() { - if (!deviceName) { - if (browser.tizen) { - deviceName = 'Samsung Smart TV'; - } else if (browser.web0s) { - deviceName = 'LG Smart TV'; - } else if (browser.operaTv) { - deviceName = 'Opera TV'; - } else if (browser.xboxOne) { - deviceName = 'Xbox One'; - } else if (browser.ps4) { - deviceName = 'Sony PS4'; - } else if (browser.chrome) { - deviceName = 'Chrome'; - } else if (browser.edgeChromium) { - deviceName = 'Edge Chromium'; - } else if (browser.edge) { - deviceName = 'Edge'; - } else if (browser.firefox) { - deviceName = 'Firefox'; - } else if (browser.opera) { - deviceName = 'Opera'; - } else if (browser.safari) { - deviceName = 'Safari'; - } else { - deviceName = 'Web Browser'; - } + if (deviceName) { + return deviceName; + } - if (browser.ipad) { - deviceName += ' iPad'; - } else if (browser.iphone) { - deviceName += ' iPhone'; - } else if (browser.android) { - deviceName += ' Android'; + deviceName = 'Web Browser'; // Default device name + + for (const key in BrowserName) { + if (browser[key]) { + deviceName = BrowserName[key]; + break; } } + if (browser.ipad) { + deviceName += ' iPad'; + } else if (browser.iphone) { + deviceName += ' iPhone'; + } else if (browser.android) { + deviceName += ' Android'; + } return deviceName; }