This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Add timezone to user profile #12967
Closed
Closed
Add timezone to user profile #12967
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
05b1826
Add timezone to right panel profile.
Half-Shot c103109
Add setting to publish timezone
Half-Shot d9238c7
Add string for timezone publish
Half-Shot e78dfc2
Automatically update timezone when setting changes.
Half-Shot 6989162
Refactor to using a hook
Half-Shot 34d3176
Check for feature support for extended profiles.
Half-Shot 8e39e06
lint
Half-Shot 6fc5450
Add timezone
Half-Shot 96d7fda
Remove unintentional changes
Half-Shot 8ec6294
Use browser default timezone.
Half-Shot 45285f2
lint
Half-Shot 5ae6e2e
tweaks
Half-Shot b12e4cd
Set timezone publish at the device level to prevent all devices writi…
Half-Shot 8c18fce
Update hook to use external client.
Half-Shot 4e11e7d
Add test for user timezone.
Half-Shot b176ca8
Update snapshot for preferences tab.
Half-Shot 2a14aee
Hide timezone info if not provided.
Half-Shot c43048d
Stablize test
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating feels like it might be overkill here, especially since it doesn't look like there's any UI to suggest it's dynamically refreshing, but seems fine I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just felt weird to me that the time would slowly drift out of date if you left the user open for a minute. I suppose it's an incredibly minor thing, but also I don't think it's a costly rerender.