Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n): use Intl APIs for TimeAgo component #4990

Merged
merged 6 commits into from
Oct 16, 2023

Conversation

rexxars
Copy link
Member

@rexxars rexxars commented Oct 13, 2023

Description

Note: review this commit by commit to more easily understand things.

  1. Introduces an internal intlCache. We will need to instantiate several different instances of the Intl APIs with differing options, and this is a semi-costly operation. The cache ensures we reuse instances where possible.
  2. Made the useIntlListFormat hook utilize said cache to prove it works (also, why not)
  3. Alters the useTimeAgo hook to allow for both past and future dates (X days ago, in Y hours)
  4. Makes the useTimeAgo hook use i18n primitives
  • Initially (in 3cce07c), I made it up to the locales to define all the different variations of strings. This was mainly done in order to match the previous output from this hook.
  • After some discussion with @mikolajdobrucki we landed on the fact that longer strings actually makes more sense - especially when English is not the only target.
  • In e75db31 I reimplemented the hook using only the native Intl APIs, apart from a single "just now" string. I left the old commit around in case we decide to revert to that, if the native Intl approach is not flexible enough.
  1. Renames useTimeAgo and the TimeAgo component to useRelativeTime and RelativeTime, respectively. This is done because it more closely aligns with the Intl.RelativeTimeFormat API, as well as indicating that it can now show dates in the future as well.

Closes SDX-677

@rexxars rexxars requested review from bjoerge and a team October 13, 2023 06:21
@vercel
Copy link

vercel bot commented Oct 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview Oct 16, 2023 9:18pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2023 9:18pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Oct 16, 2023 9:18pm

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2023

Package Documentation Change
sanity +7%
sanity/desk +2%
@sanity/types -11%
Full Report
sanity
This branch Next branch
127 documented 119 documented
849 not documented 825 not documented
sanity/desk
This branch Next branch
84 documented 83 documented
56 not documented 55 not documented
@sanity/diff
This branch Next branch
13 documented 13 documented
16 not documented 16 not documented
@sanity/block-tools
This branch Next branch
4 documented 4 documented
9 not documented 9 not documented
@sanity/portable-text-editor
This branch Next branch
21 documented 21 documented
44 not documented 44 not documented
@sanity/mutator
This branch Next branch
7 documented 7 documented
4 not documented 4 not documented
@sanity/cli
This branch Next branch
1 documented 1 documented
30 not documented 30 not documented
@sanity/schema/_internal
This branch Next branch
0 documented 0 documented
9 not documented 9 not documented
@sanity/util/paths
This branch Next branch
1 documented 1 documented
15 not documented 15 not documented
sanity/router
This branch Next branch
15 documented 15 documented
21 not documented 21 not documented
@sanity/util/legacyDateFormat
This branch Next branch
0 documented 0 documented
3 not documented 3 not documented
@sanity/schema
This branch Next branch
0 documented 0 documented
2 not documented 2 not documented
sanity/cli
This branch Next branch
2 documented 2 documented
0 not documented 0 not documented
@sanity/vision
This branch Next branch
0 documented 0 documented
2 not documented 2 not documented
@sanity/util/fs
This branch Next branch
0 documented 0 documented
3 not documented 3 not documented
sanity/_internal
This branch Next branch
0 documented 0 documented
1 not documented 1 not documented
@sanity/util/content
This branch Next branch
1 documented 1 documented
5 not documented 5 not documented
@sanity/types
This branch Next branch
47 documented 53 documented
240 not documented 233 not documented

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2023

Component Testing Report Updated Oct 16, 2023 9:22 PM (UTC)

File Status Duration Passed Skipped Failed
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 15s 2 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 13s 3 0 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 15s 9 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 53s 18 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 7s 3 0 0

Copy link
Member

@bjoerge bjoerge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is solid, lgtm!

@@ -51,22 +51,22 @@ describe('useTimeAgo', () => {
expect(result.current).toBe('Jun 3, 2022')
})

it('outputs eg "3w" if less than a month apart', () => {
it('outputs eg "3 wks" if less than a month apart', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this replace the "3w" short format with the slightly longer "3 wks" all over? Since these are used in confined areas of the UI, will this risk running into issues with text no longer fitting the space it's given?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does! @mikolajdobrucki approved this from the design side, but we should still have a look around and see if anything breaks - but we can easily change this after merging if problematic.

@rexxars rexxars force-pushed the feat/i18n-relative-time branch from 56d0638 to cd7acf7 Compare October 16, 2023 21:14
@rexxars rexxars merged commit ce6dbe7 into feat/i18n Oct 16, 2023
12 checks passed
@rexxars rexxars deleted the feat/i18n-relative-time branch October 16, 2023 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants