Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fuzzy time for conversation messages
- Loading branch information
Showing
8 changed files
with
73 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import i18n from 'i18next' | ||
import dateFns from 'date-fns' | ||
|
||
const loadedLocales = { | ||
'en': require('date-fns/locale/en'), | ||
'fr': require('date-fns/locale/fr'), | ||
'ja': require('date-fns/locale/ja'), | ||
'pl': require('date-fns/locale/pl'), | ||
'ru': require('date-fns/locale/ru'), | ||
'tr': require('date-fns/locale/tr'), | ||
// 'he': require('date-fns/locale/he'), | ||
// 'uk': require('date-fns/locale/uk'), | ||
} | ||
|
||
const getLocale = () => { | ||
let languageCode = 'en' | ||
try { | ||
languageCode = i18n.language.split('-')[0] | ||
} catch (e) { | ||
// noop | ||
} | ||
|
||
if (loadedLocales[languageCode] !== undefined) { | ||
return loadedLocales[languageCode] | ||
} | ||
|
||
return loadedLocales['en'] | ||
} | ||
|
||
export const distanceInWords = (from, to) => dateFns.distanceInWords( | ||
from, to, { | ||
locale: getLocale(), | ||
} | ||
) | ||
|
||
export const distanceInWordsToNow = from => dateFns.distanceInWordsToNow( | ||
from, | ||
{ | ||
includeSeconds: true, | ||
locale: getLocale(), | ||
} | ||
) | ||
|
||
export const formatTime = date => dateFns.format(date, i18n.t('time.time'), { locale: getLocale() }) | ||
|
||
export const formatDateTime = date => dateFns.format(date, i18n.t('time.datetime'), { locale: getLocale() }) | ||
|
||
export const fuzzyTimeOrFull = date => { | ||
if (dateFns.isToday(date)) { | ||
return distanceInWordsToNow(date) | ||
} else if (dateFns.isBefore(date, dateFns.subDays(Date.now(), 1))) { | ||
return formatTime(date) | ||
} | ||
|
||
return formatDateTime(date) | ||
} |
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
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