-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.tsx
90 lines (76 loc) · 2.34 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { __ } from '@wordpress/i18n';
import { DateTimePicker } from '@wordpress/components';
import { getSettings, dateI18n } from '@wordpress/date';
import { useEntityProp } from '@wordpress/core-data';
import type { DateSettings } from '@wordpress/date';
import { usePopover } from '../../hooks/use-popover';
import { usePost } from '../../hooks';
interface PostDatePickerProps {
date?: string;
setDate: (date: string | null) => void;
}
export const PostDatePicker: React.FC<PostDatePickerProps> = ({ date, setDate }) => {
const settings: DateSettings = getSettings();
// To know if the current time format is a 12 hour time, look for "a".
// Also make sure this "a" is not escaped by a "/".
const is12Hour = /a(?!\\)/i.test(
settings.formats.time
.toLowerCase() // Test only for the lower case "a".
.replace(/\\\\/g, '') // Replace "//" with empty strings.
.split('')
.reverse()
.join(''), // Reverse the string and test for "a" not followed by a slash.
);
return <DateTimePicker currentDate={date} onChange={setDate} is12Hour={is12Hour} />;
};
interface PostDateProps {
/**
* The placeholder to show when no date is set.
*/
placeholder: string;
/**
* The date format to use.
*/
format?: string;
/**
* Remaining props to pass to the time element.
*/
[key: string]: unknown;
}
export const PostDate: React.FC<PostDateProps> = ({
placeholder = __('No date set', 'tenup'),
format,
...rest
}) => {
const { postId, postType, isEditable } = usePost();
const [date, setDate] = useEntityProp('postType', postType, 'date', postId as string);
const [siteFormat] = useEntityProp('root', 'site', 'date_format');
const settings: DateSettings = getSettings();
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const resolvedFormat = format || siteFormat || settings.formats.date;
const { toggleProps, Popover } = usePopover();
const timeString = dateI18n(resolvedFormat, date, timezone) || placeholder;
let parentProps = { ...rest };
if (isEditable) {
parentProps = {
...toggleProps,
...parentProps,
};
}
return (
<>
<time
dateTime={dateI18n('c', date, timezone)}
itemProp="datePublished"
{...parentProps}
>
{timeString}
</time>
{isEditable && (
<Popover>
<PostDatePicker date={date} setDate={(date: string | null) => setDate(date)} />
</Popover>
)}
</>
);
};