Repository: syncthing/syncthing
File: gui/default/syncthing/core/syncthingController.js (JavaScript)

Code before:
        function refreshErrors() {
            $http.get(urlbase + '/system/error').success(function (data) {
                $scope.errors = data.errors;
                console.log("refreshErrors", data);
            }).error($scope.emitHTTPError);
        }

        function refreshConfig() {
            $http.get(urlbase + '/system/config').success(function (data) {
                updateLocalConfig(data);
                console.log("refreshConfig", data);

                //update web addres
                for (var index in $scope.config.devices) {
                    var device = $scope.config.devices[index];
                    var subId = device.deviceID.substr(0, 6)
                    if (device.webUi) {
                        if (!(subId in $scope.webAddress)) {
                            $scope.webAddress[subId] = ""
                        } 
                    } else {
                        delete $scope.webAddress[subId]
                    }
                }

                // set web address port
                for (var index in $scope.config.devices) {
                    var device = $scope.config.devices[index]
                    if (device.webUi) {
                        $scope.webAddressPort[device.deviceID.substr(0, 6)] = device.webUiPort
                    }
                }
            }).error($scope.emitHTTPError);

            $http.get(urlbase + '/system/config/insync').success(function (data) {
                $scope.configInSync = data.configInSync;
            }).error($scope.emitHTTPError);
        }

        $scope.refreshNeed = function (page, perpage) {
            if (!$scope.neededFolder) {
                return;
            }
            var url = urlbase + "/db/need?folder=" + encodeURIComponent($scope.neededFolder);
            url += "&page=" + page;
            url += "&perpage=" + perpage;
            $http.get(url).success(function (data) {
                console.log("refreshNeed", $scope.neededFolder, data);
                parseNeeded(data);
            }).error($scope.emitHTTPError);
        }

Review comment (suggestion):
I think it would be cleaner to use `$scope.findDevice()` directly to get the device config when needed and grab the port from there, rather than having to maintain `webAddressPort` just for this purpose.

Code after:
        function refreshRemoteGUI(connections) {
            if (!$scope.showRemoteGUI) {
                $scope.idToRemoteGUI = {}
                return
            }
            var newCache = {};
            for (var id in connections) {
                if (!(id in $scope.devices)) {
                    // Avoid errors when called before first updateLocalConfig()
                    continue;
                }
                var port = $scope.devices[id].remoteGUIPort;
                if (port <= 0
                    || !connections[id].address
                    || connections[id].type.includes("relay")) {
                    // Relay connections never work as desired here, nor incomplete addresses
                    $scope.idToRemoteGUI[id] = "";
                    continue;
                }
                var newAddress = "http://" + replaceAddressPort(connections[id].address, port);
                if (!(newAddress in $scope.remoteGUICache)) {
                    // No cached result, trigger a new port probing asynchronously
                    $scope.probeRemoteGUIAddress(id, newAddress);
                } else {
                    newCache[newAddress] = $scope.remoteGUICache[newAddress];
                    // Copy cached probing result in the corner case of duplicate GUI
                    // addresses for different devices.  Which is useless, but
                    // possible when behind the same NAT router.
                    if (newCache[newAddress]) {
                        $scope.idToRemoteGUI[id] = newAddress;
                    } else {
                        $scope.idToRemoteGUI[id] = "";
                    }
                }
            }
            // Replace the cache to discard stale addresses
            $scope.remoteGUICache = newCache;
        }

        function refreshErrors() {
            $http.get(urlbase + '/system/error').success(function (data) {
                $scope.errors = data.errors;
                console.log("refreshErrors", data);
            }).error($scope.emitHTTPError);
        }

        function refreshConfig() {
            $http.get(urlbase + '/config').success(function (data) {
                updateLocalConfig(data);
                console.log("refreshConfig", data);
            }).error($scope.emitHTTPError);