fastcomments-react-native-sdk
Version:
React Native FastComments Components. Add live commenting to any React Native application.
30 lines (29 loc) • 1.57 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Text, View } from "react-native";
import { useEffect, useState } from "react";
import { getPrettyDate } from "../services/pretty-date";
export function CommentDisplayDate({ date, translations, absoluteDates, absoluteAndRelativeDates, style, absoluteDateStyle }) {
const dateObj = new Date(date);
const [prettyDate, setPrettyDate] = useState(getPrettyDate(translations, dateObj.valueOf()));
// Technically having a separate timer per comment is not optimal. But, JS timers are very light and we'll only render 30 comments most of the time.
// It would be cool to have only one timer, like in the VanillaJS library.
useEffect(() => {
const interval = setInterval(function () {
if (!absoluteDates) {
setPrettyDate(getPrettyDate(translations, dateObj.valueOf()));
}
}, 60_000);
return () => clearInterval(interval);
}, []);
if (absoluteDates) {
return _jsx(Text, { style: style, children: dateObj.toLocaleDateString() });
}
else {
if (absoluteAndRelativeDates) {
return _jsxs(View, { style: [{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, style], children: [_jsx(Text, { style: style, children: prettyDate }), ";", _jsxs(Text, { style: [style, absoluteDateStyle], children: ["(", dateObj.toLocaleDateString(), ")"] }), ";"] });
}
else {
return _jsx(Text, { style: style, children: prettyDate });
}
}
}