Compare commits
4 Commits
main
...
dev/timezo
Author | SHA1 | Date |
---|---|---|
|
5772d61a85 | 1 year ago |
|
c4f13b5b8e | 1 year ago |
|
aa62fc183e | 1 year ago |
|
7014f704d3 | 1 year ago |
@ -1,44 +1,94 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Typography } from 'antd';
|
import { Typography, Select } from 'antd';
|
||||||
// import { useConversationState } from '@/stores/ConversationContext';
|
import ct from 'countries-and-timezones';
|
||||||
import dayjs from 'dayjs';
|
import { useVisibilityState } from '@/hooks/useVisibilityState';
|
||||||
import utc from 'dayjs/plugin/utc';
|
|
||||||
import timezone from 'dayjs/plugin/timezone';
|
|
||||||
|
|
||||||
dayjs.extend(utc);
|
const LocalTimeClock = ({ region, timezone, ...props }) => {
|
||||||
dayjs.extend(timezone);
|
const isVisible = useVisibilityState();
|
||||||
|
|
||||||
const LocalTimeClock = ((props) => {
|
|
||||||
// const { customerOrderProfile: orderInfo } = useConversationState();
|
|
||||||
|
|
||||||
|
const [countryTimezones, setCountryTimezones] = useState([]);
|
||||||
|
const [timezoneP, setTimezoneP] = useState(timezone);
|
||||||
const [customerDateTime, setCustomerDateTime] = useState();
|
const [customerDateTime, setCustomerDateTime] = useState();
|
||||||
|
// console.log(timezone, timezoneP);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (timezone) {
|
||||||
|
setTimezoneP(timezone);
|
||||||
|
} else {
|
||||||
|
const _countryTimezones = Object.values(ct.getTimezonesForCountry(region || 'CN')).map((tz) => {
|
||||||
|
const date = new Date();
|
||||||
|
const options = { timeZone: tz.name, timeZoneName: 'short' };
|
||||||
|
const shortName = date.toLocaleTimeString('en-US', options).split(' ')[2];
|
||||||
|
return { ...tz, fullName: `${tz.name} (${shortName})`, shortName };
|
||||||
|
});
|
||||||
|
// console.log(_countryTimezones);
|
||||||
|
setCountryTimezones(_countryTimezones);
|
||||||
|
setTimezoneP(_countryTimezones[0].name);
|
||||||
|
}
|
||||||
|
return () => {};
|
||||||
|
}, [region, timezone]);
|
||||||
|
|
||||||
// todo: 用dayjs 显示指定时区的时间
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const intervalId = setInterval(() => {
|
let interval;
|
||||||
// if (customerProfile && customerProfile.timezone) {
|
if (isVisible) {
|
||||||
const date = new Date();
|
interval = setInterval(() => {
|
||||||
const options = {
|
const options = {
|
||||||
year: 'numeric',
|
timeZone: timezoneP,
|
||||||
month: 'long',
|
timeZoneName: 'short',
|
||||||
day: 'numeric',
|
hour12: false,
|
||||||
hour: 'numeric',
|
month: '2-digit',
|
||||||
minute: 'numeric',
|
day: '2-digit',
|
||||||
second: 'numeric',
|
hour: '2-digit',
|
||||||
hour12: true,
|
minute: '2-digit',
|
||||||
};
|
// second: '2-digit',
|
||||||
const formatter = new Intl.DateTimeFormat('cn-ZH', options); // todo:
|
};
|
||||||
setCustomerDateTime(formatter.format(date));
|
// const date = new Date();
|
||||||
// }
|
// const formatter = new Intl.DateTimeFormat('en-US', options);
|
||||||
}, 1000); // Update every second
|
// setCustomerDateTime(formatter.format(date));
|
||||||
|
setCustomerDateTime(new Date().toLocaleTimeString('cn-ZH', options));
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, [isVisible, timezoneP]);
|
||||||
|
|
||||||
// Cleanup function to clear the interval when the component is unmounted
|
return region ? (
|
||||||
return () => clearInterval(intervalId);
|
|
||||||
}, []);
|
|
||||||
return (
|
|
||||||
<>
|
<>
|
||||||
<Typography.Text>{customerDateTime}</Typography.Text>
|
{/* 🌏🌐🧭 */}
|
||||||
|
<Typography.Text>🌐{region}</Typography.Text>
|
||||||
|
{countryTimezones.length > 1 ? (
|
||||||
|
<Select
|
||||||
|
size={'small'}
|
||||||
|
style={{ width: 150 }}
|
||||||
|
dropdownStyle={{ width: 250 }}
|
||||||
|
variant='borderless'
|
||||||
|
onSelect={(value) => {
|
||||||
|
setTimezoneP(value);
|
||||||
|
// setOrderPropValue(currentOrder, 'orderlabel', value)
|
||||||
|
// .then(() => {
|
||||||
|
// message.success('设置成功')
|
||||||
|
// })
|
||||||
|
// .catch(reason => {
|
||||||
|
// notification.error({
|
||||||
|
// message: '设置出错',
|
||||||
|
// description: reason.message,
|
||||||
|
// placement: 'top',
|
||||||
|
// duration: 60,
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
}}
|
||||||
|
value={timezoneP}
|
||||||
|
options={countryTimezones}
|
||||||
|
fieldNames={{ label: 'fullName', value: 'name' }}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{/* <Typography.Text>{timezoneP}</Typography.Text> */}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
<Typography.Text>⏰{customerDateTime}</Typography.Text>
|
||||||
</>
|
</>
|
||||||
);
|
) : null;
|
||||||
});
|
};
|
||||||
export default LocalTimeClock;
|
export default LocalTimeClock;
|
||||||
|
Loading…
Reference in New Issue