Files
bsky-social-app/src/components/hooks/dates.ts
T
Eric Bailey 45f0f7eefe Port post embeds to new arch (#7408)
* Direct port of embeds to new arch

(cherry picked from commit cc3fa1f6cea396dd9222486c633a508bfee1ecd6)

* Re-org

* Split out ListEmbed and FeedEmbed

* Split out ImageEmbed

* DRY up a bit

* Port over ExternalLinkEmbed

* Port over Player and Gif embeds

* Migrate ComposerReplyTo

* Replace other usages of old post-embeds

* Migrate view contexts

* Copy pasta VideoEmbed

* Copy pasta GifEmbed

* Swap in new file location

* Clean up

* Fix up native

* Add back in correct moderation on List and Feed embeds

* Format

* Prettier

* delete old video utils

* move bandwidth-estimate.ts

* Remove log

* Add LazyQuoteEmbed for composer use

* Clean up unused things

* Remove remaining items

* Prettier

* Fix imports

* Handle nested quotes same as prod

* Add back silenced error handling

* Fix lint

---------

Co-authored-by: Samuel Newman <mozzius@protonmail.com>
2025-06-13 12:05:41 -05:00

114 lines
1.6 KiB
TypeScript

/**
* Hooks for date-fns localized formatters.
*
* Our app supports some languages that are not included in date-fns by
* default, in which case it will fall back to English.
*
* {@link https://github.com/date-fns/date-fns/blob/main/docs/i18n.md}
*/
import React from 'react'
import {formatDistance, type Locale} from 'date-fns'
import {
ca,
cy,
da,
de,
el,
enGB,
eo,
es,
eu,
fi,
fr,
fy,
gd,
gl,
hi,
hu,
id,
it,
ja,
km,
ko,
nl,
pl,
pt,
ptBR,
ro,
ru,
sv,
th,
tr,
uk,
vi,
zhCN,
zhHK,
zhTW,
} from 'date-fns/locale'
import {type AppLanguage} from '#/locale/languages'
import {useLanguagePrefs} from '#/state/preferences'
/**
* {@link AppLanguage}
*/
const locales: Record<AppLanguage, Locale | undefined> = {
en: undefined,
an: undefined,
ast: undefined,
ca,
cy,
da,
de,
el,
['en-GB']: enGB,
eo,
es,
eu,
fi,
fr,
fy,
ga: undefined,
gd,
gl,
hi,
hu,
ia: undefined,
id,
it,
ja,
km,
ko,
ne: undefined,
nl,
pl,
['pt-PT']: pt,
['pt-BR']: ptBR,
ro,
ru,
sv,
th,
tr,
uk,
vi,
['zh-Hans-CN']: zhCN,
['zh-Hant-HK']: zhHK,
['zh-Hant-TW']: zhTW,
}
/**
* Returns a localized `formatDistance` function.
* {@link formatDistance}
*/
export function useFormatDistance() {
const {appLanguage} = useLanguagePrefs()
return React.useCallback<typeof formatDistance>(
(date, baseDate, options) => {
const locale = locales[appLanguage as AppLanguage]
return formatDistance(date, baseDate, {...options, locale: locale})
},
[appLanguage],
)
}