From afa8f06ef66ada4077ceffddb90ff657a89132d6 Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 15 Apr 2023 00:40:13 +0900 Subject: [PATCH] improve city search --- packages/client/src/pages/user/home.vue | 34 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/client/src/pages/user/home.vue b/packages/client/src/pages/user/home.vue index 3fa01d9c06..ef5a6544d5 100644 --- a/packages/client/src/pages/user/home.vue +++ b/packages/client/src/pages/user/home.vue @@ -353,18 +353,28 @@ const age = $computed(() => { }); const timeForThem = $computed(() => { - const tzInfo = cityTimezones.lookupViaCity( - props.user.location!.replace(/\s.*/, "") - ); - if (tzInfo.length == 0) return ""; - const tz = tzInfo[0].timezone; - const theirTime = new Date().toLocaleString("en-US", { - timeZone: tz, - hour12: false, - }); - return ` (${theirTime.split(",")[1].trim().split(":")[0]}:${theirTime - .split(" ")[1] - .slice(-5, -3)})`; + const maybeCityNames = [ + props.user.location!, + props.user.location!.replace(/[^A-Za-z].*/, ""), + props.user.location!.replace(/[^A-Za-z\-\'\.].*/, ""), + props.user.location!.replace(/[^A-Za-z0-9\-\'\.\s].*/, ""), + ]; + + for (const city of maybeCityNames) { + let tzInfo = cityTimezones.lookupViaCity(city); + if (tzInfo.length == 0) continue; + + const tz = tzInfo[0].timezone; + const theirTime = new Date().toLocaleString("en-US", { + timeZone: tz, + hour12: false, + }); + return ` (${theirTime.split(",")[1].trim().split(":")[0]}:${theirTime + .split(" ")[1] + .slice(-5, -3)})`; + } + + return ""; }); function menu(ev) {