Doceditor: fix error redirects
This commit is contained in:
parent
5031aa9a97
commit
ad71f91ea7
@ -31,19 +31,18 @@ import "@/styles/globals.scss";
|
||||
import Providers from "@/providers";
|
||||
import { getSettings, getUser } from "@/utils/actions";
|
||||
import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getBaseUrl } from "@docspace/shared/utils/next-ssr-helper";
|
||||
|
||||
export default async function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const headersList = headers();
|
||||
const referer = headersList.get("referer");
|
||||
|
||||
if (referer?.includes("health")) return children;
|
||||
|
||||
const [user, settings] = await Promise.all([getUser(), getSettings()]);
|
||||
|
||||
if (settings === "access-restricted") redirect(`${getBaseUrl()}/${settings}`);
|
||||
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
@ -67,9 +67,10 @@ const Root = ({
|
||||
const fileInfo = config?.file;
|
||||
|
||||
const instanceId = config?.document?.referenceData.instanceId;
|
||||
|
||||
const isSkipError =
|
||||
error?.status === "not-found" ||
|
||||
error?.status === "access-denied" ||
|
||||
(error?.status === "access-denied" && error.editorUrl) ||
|
||||
error?.status === "not-supported";
|
||||
|
||||
const { t } = useTranslation(["Editor", "Common"]);
|
||||
@ -81,7 +82,6 @@ const Root = ({
|
||||
const { getErrorMessage } = useError({
|
||||
error,
|
||||
editorUrl: documentserverUrl,
|
||||
t,
|
||||
});
|
||||
|
||||
const { currentDeviceType } = useDeviceType();
|
||||
@ -130,6 +130,7 @@ const Root = ({
|
||||
error &&
|
||||
error.message !== "restore-backup" &&
|
||||
error.message !== "unauthorized" &&
|
||||
error.message !== "unavailable" &&
|
||||
!isSkipError
|
||||
) {
|
||||
throw new Error(error.message);
|
||||
|
@ -160,7 +160,7 @@ const useEditorEvents = ({
|
||||
}, [config?.Error, errorMessage]);
|
||||
|
||||
const onDocumentReady = React.useCallback(() => {
|
||||
console.log("onDocumentReady", { docEditor });
|
||||
// console.log("onDocumentReady", { docEditor });
|
||||
setDocumentReady(true);
|
||||
|
||||
frameCallCommand("setIsLoaded");
|
||||
@ -172,7 +172,7 @@ const useEditorEvents = ({
|
||||
// }
|
||||
|
||||
if (docEditor) {
|
||||
console.log("call assign for asc files editor doceditor");
|
||||
// console.log("call assign for asc files editor doceditor");
|
||||
assign(
|
||||
window as unknown as { [key: string]: {} },
|
||||
["ASC", "Files", "Editor", "docEditor"],
|
||||
@ -599,7 +599,7 @@ const useEditorEvents = ({
|
||||
};
|
||||
|
||||
React.useEffect(() => {
|
||||
console.log("render docspace config", { ...window.DocSpaceConfig });
|
||||
// console.log("render docspace config", { ...window.DocSpaceConfig });
|
||||
if (
|
||||
IS_DESKTOP_EDITOR ||
|
||||
(typeof window !== "undefined" &&
|
||||
|
@ -35,10 +35,9 @@ import { TError } from "@/types";
|
||||
interface UseErrorProps {
|
||||
error?: TError;
|
||||
editorUrl?: string;
|
||||
t: TTranslation;
|
||||
}
|
||||
|
||||
const useError = ({ error, editorUrl, t }: UseErrorProps) => {
|
||||
const useError = ({ error, editorUrl }: UseErrorProps) => {
|
||||
React.useEffect(() => {
|
||||
if (error?.message === "unauthorized") {
|
||||
sessionStorage.setItem("referenceUrl", window.location.href);
|
||||
@ -48,6 +47,19 @@ const useError = ({ error, editorUrl, t }: UseErrorProps) => {
|
||||
"_self",
|
||||
);
|
||||
}
|
||||
if (error?.message === "unavailable") {
|
||||
window.open(
|
||||
combineUrl(window.DocSpaceConfig?.proxy?.url, "/unavailable"),
|
||||
"_self",
|
||||
);
|
||||
}
|
||||
|
||||
if (error?.message === "restore-backup") {
|
||||
window.open(
|
||||
combineUrl(window.DocSpaceConfig?.proxy?.url, "/preparation-portal"),
|
||||
"_self",
|
||||
);
|
||||
}
|
||||
}, [error]);
|
||||
|
||||
React.useEffect(() => {
|
||||
@ -72,10 +84,8 @@ const useError = ({ error, editorUrl, t }: UseErrorProps) => {
|
||||
const getErrorMessage = React.useCallback(() => {
|
||||
if (typeof error !== "string") return error?.message;
|
||||
|
||||
if (error === "restore-backup") return t("Common:PreparationPortalTitle");
|
||||
|
||||
return error;
|
||||
}, [error, t]);
|
||||
}, [error]);
|
||||
|
||||
return { getErrorMessage };
|
||||
};
|
||||
|
@ -164,10 +164,11 @@ export interface IInitialConfig {
|
||||
type: string;
|
||||
Error?: string;
|
||||
errorMessage?: string;
|
||||
message?: undefined;
|
||||
}
|
||||
|
||||
export type TError = {
|
||||
message: "unauthorized" | "restore-backup" | string;
|
||||
message?: "unauthorized" | "restore-backup" | string;
|
||||
status?: "not-found" | "access-denied" | number | string;
|
||||
type?: string;
|
||||
editorUrl?: string;
|
||||
|
@ -62,7 +62,6 @@ const processFillFormDraft = async (
|
||||
const url = new URL(basePath + formUrl);
|
||||
|
||||
const queryFileId = url.searchParams.get("fileid");
|
||||
const queryVersion = url.searchParams.get("version");
|
||||
|
||||
if (!queryFileId) return;
|
||||
|
||||
@ -217,8 +216,6 @@ export async function getData(
|
||||
editorType?: string,
|
||||
) {
|
||||
try {
|
||||
const hdrs = headers();
|
||||
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
if (view) searchParams.append("view", view ? "true" : "false");
|
||||
@ -236,7 +233,7 @@ export async function getData(
|
||||
getSettings(share),
|
||||
]);
|
||||
|
||||
if ("editorConfig" in config) {
|
||||
if ("editorConfig" in config && typeof settings !== "string") {
|
||||
const response: TResponse = {
|
||||
config,
|
||||
user,
|
||||
@ -264,16 +261,26 @@ export async function getData(
|
||||
}
|
||||
}
|
||||
|
||||
if (response.settings?.tenantStatus === TenantStatus.PortalRestore) {
|
||||
response.error = { message: "restore-backup" };
|
||||
}
|
||||
|
||||
const successAuth = !!user;
|
||||
|
||||
if (!successAuth && !doc && !share) {
|
||||
response.error = { message: "unauthorized" };
|
||||
}
|
||||
|
||||
if (
|
||||
typeof response.settings !== "string" &&
|
||||
response.settings?.tenantStatus === TenantStatus.PortalRestore
|
||||
) {
|
||||
response.error = { message: "restore-backup" };
|
||||
}
|
||||
|
||||
if (
|
||||
typeof response.settings !== "string" &&
|
||||
response.settings?.tenantStatus === TenantStatus.PortalDeactivate
|
||||
) {
|
||||
response.error = { message: "unavailable" };
|
||||
}
|
||||
|
||||
const isSharingAccess = response.config.file.canShare;
|
||||
|
||||
if (view) {
|
||||
@ -294,12 +301,26 @@ export async function getData(
|
||||
fileId,
|
||||
};
|
||||
|
||||
if (
|
||||
typeof settings !== "string" &&
|
||||
settings?.tenantStatus === TenantStatus.PortalRestore
|
||||
) {
|
||||
response.error = { message: "restore-backup" };
|
||||
}
|
||||
|
||||
if (
|
||||
typeof settings !== "string" &&
|
||||
settings?.tenantStatus === TenantStatus.PortalDeactivate
|
||||
) {
|
||||
response.error = { message: "unavailable" };
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
const err = e as TCatchError;
|
||||
console.error("initDocEditor failed", err);
|
||||
|
||||
const editorUrl = (await getEditorUrl("", share)).docServiceUrl;
|
||||
// const editorUrl = (await getEditorUrl("", share)).docServiceUrl;
|
||||
|
||||
let message = "";
|
||||
if (typeof err === "string") message = err;
|
||||
@ -319,7 +340,7 @@ export async function getData(
|
||||
const error: TError = {
|
||||
message,
|
||||
status,
|
||||
editorUrl,
|
||||
editorUrl: "",
|
||||
};
|
||||
return { error };
|
||||
}
|
||||
@ -340,6 +361,8 @@ export async function getUser(share?: string) {
|
||||
|
||||
if (userRes.status === 401) return undefined;
|
||||
|
||||
if (!userRes.ok) return;
|
||||
|
||||
const user = await userRes.json();
|
||||
|
||||
return user.response as TUser;
|
||||
@ -357,17 +380,13 @@ export async function getSettings(share?: string) {
|
||||
"GET",
|
||||
);
|
||||
|
||||
const resActions = [];
|
||||
const settingsRes = await fetch(getSettings);
|
||||
|
||||
resActions.push(fetch(getSettings));
|
||||
if (settingsRes.status === 403) return `access-restricted`;
|
||||
|
||||
const [settingsRes] = await Promise.all(resActions);
|
||||
if (!settingsRes.ok) return;
|
||||
|
||||
const actions = [];
|
||||
|
||||
actions.push(settingsRes.json());
|
||||
|
||||
const [settings] = await Promise.all(actions);
|
||||
const settings = await settingsRes.json();
|
||||
|
||||
return settings.response as TSettings;
|
||||
}
|
||||
@ -420,12 +439,15 @@ export async function openEdit(
|
||||
return config.response as IInitialConfig;
|
||||
}
|
||||
|
||||
const editorUrl = (await getEditorUrl("", share)).docServiceUrl;
|
||||
const editorUrl =
|
||||
cookie?.includes("asc_auth_key") || share
|
||||
? (await getEditorUrl("", share)).docServiceUrl
|
||||
: "";
|
||||
|
||||
const status =
|
||||
config.error.type === EditorConfigErrorType.NotFoundScope
|
||||
config.error?.type === EditorConfigErrorType.NotFoundScope
|
||||
? "not-found"
|
||||
: config.error.type === EditorConfigErrorType.AccessDeniedScope
|
||||
: config.error?.type === EditorConfigErrorType.AccessDeniedScope
|
||||
? "access-denied"
|
||||
: res.status === 415
|
||||
? "not-supported"
|
||||
@ -438,7 +460,7 @@ export async function openEdit(
|
||||
? config.error.type === EditorConfigErrorType.LinkScope
|
||||
? { message: message ?? "unauthorized", status, editorUrl }
|
||||
: { ...config.error, status, editorUrl }
|
||||
: { message: message ?? "unauthorized", status, editorUrl };
|
||||
: { message: "unauthorized", status, editorUrl };
|
||||
|
||||
return error as TError;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user