@nexusui/components
Version:
These are custom components specially-developed for NexusUI applications. They will make your life easier by giving you out-of-the-box implementations for various high-level UI elements that you can drop directly into your application.
2 lines (1 loc) • 1.36 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("@emotion/react"),t=require("../../locales/index.js"),o=require("./locale.json.js"),n=require("../../locales/i18n.js");const a=6e4,u=36e5,s=864e5,i=7*s,m=30*s,c=365*s;var l;!function(e){e.minute="minute",e.hour="hour",e.day="day",e.week="week",e.month="month",e.year="year"}(l||(l={}));const h={minute:a,hour:u,day:s,week:i,month:m,year:c},d=(e,r)=>{const t=h[e];return Math.round(r/t)},w=(e,r)=>r?`${e}s`:e,y=(e,r)=>{const t=Date.now(),o=e.getTime(),n=Math.abs(t-o);let a=0,u="";return n<6e4?(a="a few",u="seconds"):n<36e5?(a=d(l.minute,n),u=w(l.minute,a>1)):n<1*s?(a=d(l.hour,n),u=w(l.hour,a>1)):n<1*i?(a=d(l.day,n),u=w(l.day,a>1)):n<1*m?(a=d(l.week,n),u=w(l.week,a>1)):n<1*c?(a=d(l.month,n),u=w(l.month,a>1)):(a=d(l.year,n),u=w(l.year,a>1)),r(`${"a few"===a?"a few":1===a?"1":"{{0}}"} ${u} ${t-o>0?"ago":"from now"}`,{0:a})};exports.fromNow=y,exports.useTimeFormat=()=>{const a=e.useContext(r.ThemeContext).language,u=e.useMemo((()=>n.supportedLanguage.includes(a)?a:"en-US"),[a]),s=t.useTranslate(o.default);return{fromNowFormat:e.useCallback((e=>e?y(new Date(e),s):""),[s]),fullTimeFormat:e.useCallback((e=>{if(!e)return"";return new Date(e).toLocaleString(u,{hour:"2-digit",minute:"numeric",hour12:!0,month:"long",day:"numeric",year:"numeric"})}),[u])}};