26 lines
1014 B
TypeScript
26 lines
1014 B
TypeScript
// Formatters of the console shared by server and client components (no server imports).
|
||
import { intlLocale, type Translator } from "@peppa/shared/i18n";
|
||
|
||
/** «5 мин назад» in the page's language. */
|
||
export function ago(d: Date | null | undefined, t: Translator): string {
|
||
if (!d) return "—";
|
||
const s = Math.round((Date.now() - d.getTime()) / 1000);
|
||
if (s < 60) return t("{n} с назад", { n: s });
|
||
if (s < 3600) return t("{n} мин назад", { n: Math.round(s / 60) });
|
||
if (s < 86400) return t("{n} ч назад", { n: Math.round(s / 3600) });
|
||
return d.toLocaleDateString(intlLocale(t.locale));
|
||
}
|
||
|
||
/** Text with its http(s) addresses as links. */
|
||
export function linkify(text: string) {
|
||
return text.split(/(https?:\/\/[^\s<>«»"]+[^\s<>«»".,;:!?)])/g).map((part, i) =>
|
||
i % 2 ? (
|
||
<a key={i} href={part} target={part.startsWith(process.env.PEPPA_WEB_URL ?? "\0") ? undefined : "_blank"} rel="noreferrer">
|
||
{part}
|
||
</a>
|
||
) : (
|
||
part
|
||
),
|
||
);
|
||
}
|