Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/misc/timestamp.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Tooltip } from "antd";6import { CSSProperties } from "react";7import TimeAgo from "timeago-react";89interface Props {10epoch?: number; // ms since epoch11datetime?: Date | string;12style?: CSSProperties;13dateOnly?: boolean;14absolute?: boolean;15}1617export function processTimestamp(props: Props) {18const { epoch, dateOnly } = props;19let datetime = props.datetime;2021if (epoch && datetime == null) {22datetime = new Date(epoch);23}2425if (!datetime) {26return "-";27}2829if (typeof datetime == "string") {30datetime = new Date(datetime);31if (typeof datetime == "string") throw Error("bug");32}3334const absoluteTimeDateOnly = datetime.toLocaleDateString(undefined, {35year: "numeric",36month: "short",37day: "numeric",38});3940const absoluteTimeFull = datetime.toLocaleString(undefined, {41year: "numeric",42month: "short",43day: "numeric",44hour: "numeric",45minute: "numeric",46});4748const timeShown = dateOnly ? absoluteTimeDateOnly : absoluteTimeFull;4950return { datetime, timeShown, absoluteTimeDateOnly, absoluteTimeFull };51}5253export default function Timestamp(props: Props) {54const { style, absolute } = props;55const data = processTimestamp(props);56if (data === "-") {57return <span style={style}>-</span>;58}59const { datetime, timeShown, absoluteTimeFull } = data;6061if (absolute) {62return (63<Tooltip trigger={["hover", "click"]} title={absoluteTimeFull}>64<span style={style}>{timeShown}</span>65</Tooltip>66);67}6869return (70<Tooltip trigger={["hover", "click"]} title={timeShown}>71<TimeAgo style={style} datetime={datetime} />72</Tooltip>73);74}757677