2023-08-11 21:39:58 +08:00
|
|
|
import { useLazyQuery } from '@apollo/client';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { GET_SERVER_TIME, GetServerTimeResponse } from './queries';
|
|
|
|
import { setTimeSync } from '/imports/ui/core/local-states/useTimeSync';
|
|
|
|
|
|
|
|
const TimeSync: React.FC = () => {
|
|
|
|
const [
|
|
|
|
loadGetServerTime,
|
2023-09-11 21:12:37 +08:00
|
|
|
{
|
|
|
|
called,
|
|
|
|
loading,
|
|
|
|
data,
|
|
|
|
error,
|
|
|
|
},
|
2023-08-11 21:39:58 +08:00
|
|
|
] = useLazyQuery<GetServerTimeResponse>(GET_SERVER_TIME);
|
|
|
|
useEffect(() => {
|
|
|
|
if (!called) {
|
|
|
|
loadGetServerTime();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-09-11 21:12:37 +08:00
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{JSON.stringify(error)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-11 21:39:58 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!loading && data) {
|
2023-09-14 21:44:04 +08:00
|
|
|
const time = new Date(data.current_time[0].currentTimestamp);
|
2024-04-23 12:00:54 +08:00
|
|
|
const dateNow = new Date();
|
|
|
|
setTimeSync(time.getTime() - dateNow.getTime());
|
2023-08-11 21:39:58 +08:00
|
|
|
}
|
|
|
|
}, [data, loading]);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2023-08-23 03:39:16 +08:00
|
|
|
export default TimeSync;
|