-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [create-pull-request] automated change (#12966) Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com> * Add timezone to right panel profile. * Add setting to publish timezone * Add string for timezone publish * Automatically update timezone when setting changes. * Refactor to using a hook And automatically refresh the timezone every minute. * Check for feature support for extended profiles. * lint * Add timezone * Remove unintentional changes * Use browser default timezone. * lint * tweaks * Set timezone publish at the device level to prevent all devices writing to the timezone field. * Update hook to use external client. * Add test for user timezone. * Update snapshot for preferences tab. * Hide timezone info if not provided. * Stablize test * Fix date test types. * prettier * Add timezone tests * Add test for invalid timezone. * Update screenshot * Remove check for profile. --------- Co-authored-by: ElementRobot <releases@riot.im> Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com>
- Loading branch information
1 parent
f317763
commit eae9d9e
Showing
11 changed files
with
330 additions
and
60 deletions.
There are no files selected for viewing
Binary file modified
BIN
+2.69 KB
(100%)
...b.spec.ts/Preferences-user-settings-tab-should-be-rendered-properly-1-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
import { useEffect, useState } from "react"; | ||
import { MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix"; | ||
|
||
/** | ||
* Fetch a user's delclared timezone through their profile, and return | ||
* a friendly string of the current time for that user. This will keep | ||
* in sync with the current time, and will be refreshed once a minute. | ||
* | ||
* @param cli The Matrix Client instance. | ||
* @param userId The userID to fetch the timezone for. | ||
* @returns A timezone name and friendly string for the user's timezone, or | ||
* null if the user has no timezone or the timezone was not recognised | ||
* by the browser. | ||
*/ | ||
export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone: string; friendly: string } | null => { | ||
const [timezone, setTimezone] = useState<string>(); | ||
const [updateInterval, setUpdateInterval] = useState<number>(); | ||
const [friendly, setFriendly] = useState<string>(); | ||
const [supported, setSupported] = useState<boolean>(); | ||
|
||
useEffect(() => { | ||
if (!cli || supported !== undefined) { | ||
return; | ||
} | ||
cli.doesServerSupportExtendedProfiles() | ||
.then(setSupported) | ||
.catch((ex) => { | ||
console.warn("Unable to determine if extended profiles are supported", ex); | ||
}); | ||
}, [supported, cli]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (updateInterval) { | ||
clearInterval(updateInterval); | ||
} | ||
}; | ||
}, [updateInterval]); | ||
|
||
useEffect(() => { | ||
if (supported !== true) { | ||
return; | ||
} | ||
(async () => { | ||
console.log("Trying to fetch TZ"); | ||
try { | ||
const tz = await cli.getExtendedProfileProperty(userId, "us.cloke.msc4175.tz"); | ||
if (typeof tz !== "string") { | ||
// Err, definitely not a tz. | ||
throw Error("Timezone value was not a string"); | ||
} | ||
// This will validate the timezone for us. | ||
// eslint-disable-next-line new-cap | ||
Intl.DateTimeFormat(undefined, { timeZone: tz }); | ||
|
||
const updateTime = (): void => { | ||
const currentTime = new Date(); | ||
const friendly = currentTime.toLocaleString(undefined, { | ||
timeZone: tz, | ||
hour12: true, | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
timeZoneName: "shortOffset", | ||
}); | ||
setTimezone(tz); | ||
setFriendly(friendly); | ||
setUpdateInterval(setTimeout(updateTime, (60 - currentTime.getSeconds()) * 1000)); | ||
}; | ||
updateTime(); | ||
} catch (ex) { | ||
setTimezone(undefined); | ||
setFriendly(undefined); | ||
setUpdateInterval(undefined); | ||
if (ex instanceof MatrixError && ex.errcode === "M_NOT_FOUND") { | ||
// No timezone set, ignore. | ||
return; | ||
} | ||
console.error("Could not render current timezone for user", ex); | ||
} | ||
})(); | ||
}, [supported, userId, cli]); | ||
|
||
if (!timezone || !friendly) { | ||
return null; | ||
} | ||
|
||
return { | ||
friendly, | ||
timezone, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.