|
| 1 | +import React, { |
| 2 | + useEffect, |
| 3 | + useState, |
| 4 | + lazy, |
| 5 | + useReducer, |
| 6 | + useRef, |
| 7 | + useMemo, |
| 8 | +} from "react"; |
| 9 | +import type { LiveData } from "../../types/data"; |
| 10 | +import type { LiveWidget } from "../../types/widgets"; |
| 11 | +import { MetricMap } from "../../providers/plausible/utilities"; |
| 12 | +import { useTheme } from "payload/dist/admin/components/utilities/Theme"; |
| 13 | + |
| 14 | +type Props = {}; |
| 15 | + |
| 16 | +const LiveDataWidget: React.FC<Props> = ({}) => { |
| 17 | + const [data, setData] = useState<LiveData>(); |
| 18 | + const [isLoading, setIsLoading] = useState<boolean>(true); |
| 19 | + const theme = useTheme(); |
| 20 | + |
| 21 | + /* const { label } = options; */ |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + const getLiveData = fetch(`/api/analytics/liveData`, { |
| 25 | + method: "post", |
| 26 | + credentials: "include", |
| 27 | + headers: { |
| 28 | + Accept: "application/json", |
| 29 | + "Content-Type": "application/json", |
| 30 | + }, |
| 31 | + body: JSON.stringify({}), |
| 32 | + }).then((response) => response.json()); |
| 33 | + |
| 34 | + getLiveData.then((data: LiveData) => { |
| 35 | + setData(data); |
| 36 | + setIsLoading(false); |
| 37 | + }); |
| 38 | + }, []); |
| 39 | + |
| 40 | + const heading = useMemo(() => { |
| 41 | + /* if (label) return label; */ |
| 42 | + |
| 43 | + return "hidden"; |
| 44 | + }, []); |
| 45 | + |
| 46 | + return ( |
| 47 | + <section |
| 48 | + style={{ |
| 49 | + marginBottom: "1.5rem", |
| 50 | + border: "1px solid", |
| 51 | + borderColor: "var(--theme-elevation-100)", |
| 52 | + padding: "0.5rem", |
| 53 | + width: "100%", |
| 54 | + }} |
| 55 | + > |
| 56 | + {heading !== "hidden" && ( |
| 57 | + <h1 style={{ fontSize: "1.25rem", marginBottom: "0.75rem" }}> |
| 58 | + {heading} |
| 59 | + </h1> |
| 60 | + )} |
| 61 | + <div> |
| 62 | + {isLoading ? ( |
| 63 | + <>Loading...</> |
| 64 | + ) : ( |
| 65 | + <ul style={{ margin: "0", listStyle: "none", padding: "0" }}> |
| 66 | + <li style={{ display: "flex", justifyContent: "space-between" }}> |
| 67 | + <div style={{ fontWeight: "700" }}>Live visitors</div> |
| 68 | + <div>{data?.visitors}</div> |
| 69 | + </li> |
| 70 | + </ul> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + </section> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +export const getLiveDataWidget = (props?: any, options?: LiveWidget) => { |
| 78 | + const combinedProps: Props = { |
| 79 | + ...props, |
| 80 | + options, |
| 81 | + }; |
| 82 | + return <LiveDataWidget {...combinedProps} />; |
| 83 | +}; |
| 84 | + |
| 85 | +export default { LiveDataWidget, getLiveDataWidget }; |
0 commit comments