2022-12-26 05:11:33 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
import { getDefaultPageId } from "sagas/selectors";
|
|
|
|
|
import { getSettings } from "selectors/settingsSelectors";
|
2023-06-27 10:45:33 +00:00
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
2023-03-29 17:07:06 +00:00
|
|
|
import { getCurrentApplication } from "@appsmith/selectors/applicationSelectors";
|
2022-12-26 05:11:33 +00:00
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
|
|
|
|
import debounce from "lodash/debounce";
|
2023-03-29 17:07:06 +00:00
|
|
|
import { updateApplication } from "@appsmith/actions/applicationActions";
|
2023-10-12 05:31:22 +00:00
|
|
|
import { viewerURL } from "@appsmith/RouteBuilder";
|
2022-12-26 05:11:33 +00:00
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
IN_APP_EMBED_SETTING,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
2023-07-12 08:45:37 +00:00
|
|
|
import { selectFeatureFlags } from "@appsmith/selectors/featureFlagsSelectors";
|
2023-09-05 08:30:39 +00:00
|
|
|
import { AppsmithFrameAncestorsSetting } from "./Constants/constants";
|
|
|
|
|
import { formatEmbedSettings } from "./Utils/utils";
|
2022-12-26 05:11:33 +00:00
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
const regex = /^[1-9][0-9]{0,3}((px)|(em)|(%)|(vw)|(vh))?$/;
|
|
|
|
|
|
2022-12-26 05:11:33 +00:00
|
|
|
const embedSettingContentConfig = {
|
|
|
|
|
[AppsmithFrameAncestorsSetting.ALLOW_EMBEDDING_EVERYWHERE]: {
|
|
|
|
|
icon: "global-line",
|
|
|
|
|
label: createMessage(IN_APP_EMBED_SETTING.allowEmbeddingLabel),
|
|
|
|
|
tooltip: createMessage(IN_APP_EMBED_SETTING.allowEmbeddingTooltip),
|
|
|
|
|
},
|
|
|
|
|
[AppsmithFrameAncestorsSetting.LIMIT_EMBEDDING]: {
|
|
|
|
|
icon: "lock-2-line",
|
|
|
|
|
label: createMessage(IN_APP_EMBED_SETTING.limitEmbeddingLabel),
|
|
|
|
|
tooltip: createMessage(IN_APP_EMBED_SETTING.limitEmbeddingTooltip),
|
|
|
|
|
},
|
|
|
|
|
[AppsmithFrameAncestorsSetting.DISABLE_EMBEDDING_EVERYWHERE]: {
|
|
|
|
|
icon: "forbid-line",
|
|
|
|
|
label: createMessage(IN_APP_EMBED_SETTING.disableEmbeddingLabel),
|
|
|
|
|
tooltip: createMessage(IN_APP_EMBED_SETTING.disableEmbeddingTooltip),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type EmbedSetting = keyof typeof embedSettingContentConfig;
|
|
|
|
|
|
|
|
|
|
function useUpdateEmbedSnippet() {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const application = useSelector(getCurrentApplication);
|
|
|
|
|
const settings = useSelector(getSettings);
|
|
|
|
|
const user = useSelector(getCurrentUser);
|
|
|
|
|
const defaultPageId = useSelector(getDefaultPageId);
|
2023-06-22 05:00:24 +00:00
|
|
|
const featureFlags = useSelector(selectFeatureFlags);
|
2023-09-05 08:30:39 +00:00
|
|
|
const currentSetting: EmbedSetting = formatEmbedSettings(
|
|
|
|
|
settings["APPSMITH_ALLOWED_FRAME_ANCESTORS"] as string,
|
|
|
|
|
).value;
|
2022-12-26 05:11:33 +00:00
|
|
|
const embedSettingContent = embedSettingContentConfig[currentSetting];
|
|
|
|
|
const [embedSetting, setEmbedSetting] = useState({
|
|
|
|
|
height: "720px",
|
|
|
|
|
width: "1024px",
|
|
|
|
|
...application?.embedSetting,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const areDimensionValuesValid = useCallback((embedSetting: any) => {
|
2023-05-19 18:37:06 +00:00
|
|
|
const isHeightValid = regex.test(embedSetting.height);
|
|
|
|
|
const isWidthValid = regex.test(embedSetting.width);
|
2022-12-26 05:11:33 +00:00
|
|
|
|
|
|
|
|
return isHeightValid && isWidthValid;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onChange = (setting: any) => {
|
|
|
|
|
if (application) {
|
|
|
|
|
const updatedSetting = { ...embedSetting, ...setting };
|
|
|
|
|
setEmbedSetting((state) => {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
...setting,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
areDimensionValuesValid(updatedSetting) &&
|
|
|
|
|
debouncedUpdate(application?.id, updatedSetting);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (user?.isSuperUser) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_ADMIN_SETTINGS,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const debouncedUpdate = useCallback(
|
|
|
|
|
debounce(
|
|
|
|
|
(applicationId, embedSetting) => {
|
|
|
|
|
dispatch(
|
|
|
|
|
updateApplication(applicationId, {
|
|
|
|
|
embedSetting,
|
|
|
|
|
currentApp: true,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
1000,
|
|
|
|
|
{
|
|
|
|
|
leading: true,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const appViewEndPoint = useMemo(() => {
|
|
|
|
|
const url = viewerURL({
|
|
|
|
|
pageId: defaultPageId,
|
|
|
|
|
});
|
2023-06-22 05:00:24 +00:00
|
|
|
const allowHidingShareSettingsInEmbedView =
|
2023-07-12 13:49:49 +00:00
|
|
|
featureFlags.release_embed_hide_share_settings_enabled;
|
2022-12-26 05:11:33 +00:00
|
|
|
const fullUrl = new URL(window.location.origin.toString() + url);
|
|
|
|
|
if (embedSetting?.showNavigationBar) {
|
2023-06-22 05:00:24 +00:00
|
|
|
if (allowHidingShareSettingsInEmbedView) {
|
|
|
|
|
fullUrl.searchParams.append("embed", "true");
|
|
|
|
|
fullUrl.searchParams.append("navbar", "true");
|
|
|
|
|
}
|
2022-12-26 05:11:33 +00:00
|
|
|
return fullUrl.toString();
|
|
|
|
|
}
|
2023-06-22 05:00:24 +00:00
|
|
|
|
|
|
|
|
fullUrl.searchParams.append("embed", "true");
|
2022-12-26 05:11:33 +00:00
|
|
|
return fullUrl.toString();
|
|
|
|
|
}, [defaultPageId, embedSetting?.showNavigationBar]);
|
|
|
|
|
|
|
|
|
|
const snippet = useMemo(() => {
|
|
|
|
|
return `<iframe src="${appViewEndPoint}" width="${embedSetting?.width}" height="${embedSetting?.height}"></iframe>`;
|
|
|
|
|
}, [appViewEndPoint, embedSetting?.width, embedSetting?.height]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
appViewEndPoint,
|
|
|
|
|
snippet,
|
|
|
|
|
onChange,
|
|
|
|
|
embedSettingContent,
|
|
|
|
|
currentEmbedSetting: embedSetting,
|
|
|
|
|
isSuperUser: user?.isSuperUser,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default useUpdateEmbedSnippet;
|