2023-07-12 06:42:16 +00:00
|
|
|
import React, { lazy, useEffect, useState, Suspense } from "react";
|
|
|
|
|
import type { FeatureParams } from "./walkthroughContext";
|
2023-07-25 13:11:00 +00:00
|
|
|
import { DEFAULT_DELAY } from "./walkthroughContext";
|
2023-07-12 06:42:16 +00:00
|
|
|
import WalkthroughContext from "./walkthroughContext";
|
|
|
|
|
import { createPortal } from "react-dom";
|
|
|
|
|
import { hideIndicator } from "pages/Editor/GuidedTour/utils";
|
|
|
|
|
import { retryPromise } from "utils/AppsmithUtils";
|
|
|
|
|
import { useLocation } from "react-router-dom";
|
2023-07-27 13:00:23 +00:00
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
2023-07-12 06:42:16 +00:00
|
|
|
|
|
|
|
|
const WalkthroughRenderer = lazy(() => {
|
|
|
|
|
return retryPromise(
|
|
|
|
|
() =>
|
|
|
|
|
import(
|
|
|
|
|
/* webpackChunkName: "walkthrough-renderer" */ "./walkthroughRenderer"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const LoadingFallback = () => null;
|
|
|
|
|
|
|
|
|
|
export default function Walkthrough({ children }: any) {
|
|
|
|
|
const [activeWalkthrough, setActiveWalkthrough] =
|
|
|
|
|
useState<FeatureParams | null>();
|
|
|
|
|
const [feature, setFeature] = useState<FeatureParams[]>([]);
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
|
|
|
|
|
const pushFeature = (value: FeatureParams) => {
|
|
|
|
|
const alreadyExists = feature.some((f) => f.targetId === value.targetId);
|
|
|
|
|
if (!alreadyExists) {
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
setFeature((e) => [...e, ...value]);
|
|
|
|
|
} else {
|
|
|
|
|
setFeature((e) => [...e, value]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateActiveWalkthrough();
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-27 13:00:23 +00:00
|
|
|
const popFeature = (triggeredFrom?: string) => {
|
2023-07-12 06:42:16 +00:00
|
|
|
hideIndicator();
|
2023-07-27 13:00:23 +00:00
|
|
|
const eventParams = activeWalkthrough?.eventParams || {};
|
|
|
|
|
if (triggeredFrom) {
|
|
|
|
|
eventParams.from = triggeredFrom;
|
|
|
|
|
}
|
|
|
|
|
AnalyticsUtil.logEvent("WALKTHROUGH_DISMISSED", eventParams);
|
|
|
|
|
if (activeWalkthrough && activeWalkthrough.onDismiss) {
|
|
|
|
|
activeWalkthrough.onDismiss();
|
|
|
|
|
}
|
2023-07-12 06:42:16 +00:00
|
|
|
setFeature((e) => {
|
|
|
|
|
e.shift();
|
|
|
|
|
return [...e];
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateActiveWalkthrough = () => {
|
|
|
|
|
if (feature.length > 0) {
|
2023-07-27 13:00:23 +00:00
|
|
|
const highlightArea = document.getElementById(feature[0].targetId);
|
2023-07-25 13:11:00 +00:00
|
|
|
setActiveWalkthrough(null);
|
2023-07-12 06:42:16 +00:00
|
|
|
if (highlightArea) {
|
2023-07-25 13:11:00 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
setActiveWalkthrough(feature[0]);
|
|
|
|
|
}, feature[0].delay || DEFAULT_DELAY);
|
2023-07-12 06:42:16 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setActiveWalkthrough(null);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (feature.length > -1) updateActiveWalkthrough();
|
|
|
|
|
}, [feature.length, location]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<WalkthroughContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
pushFeature,
|
|
|
|
|
popFeature,
|
|
|
|
|
feature,
|
|
|
|
|
isOpened: !!activeWalkthrough,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
{activeWalkthrough &&
|
|
|
|
|
createPortal(
|
|
|
|
|
<Suspense fallback={<LoadingFallback />}>
|
|
|
|
|
<WalkthroughRenderer {...activeWalkthrough} />
|
|
|
|
|
</Suspense>,
|
|
|
|
|
document.body,
|
|
|
|
|
)}
|
|
|
|
|
</WalkthroughContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|