Files
outline/app/hooks/useImportDocument.ts
Tom MoorandClaude Fable 5 b17f2611bf chore: React 18 upgrade (#13559)
* React 18 upgrade

* fix: Force synchronous React rendering in server HTML export

React 18 can defer a legacy ReactDOM.render commit when sync work is
queued, leaving exported component nodes empty. Wrap the render in
flushSync, and flush unmount-scheduled passive effects before global
env cleanup so React never runs without a window.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: Move server ComponentView to createRoot

ReactDOM.render and unmountComponentAtNode are removed in React 19.
flushSync around root.render keeps the mount synchronous for the
HTML-export path, which reads the DOM immediately after.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Enable CI

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-03 01:43:47 -04:00

85 lines
2.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import invariant from "invariant";
import { useState, useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { toast } from "sonner";
import { errToString } from "@shared/utils/error";
import { useSidebarContext } from "~/components/Sidebar/components/SidebarContext";
import useStores from "~/hooks/useStores";
import { documentPath } from "~/utils/routeHelpers";
let importingLock = false;
export default function useImportDocument(
collectionId?: string | null,
documentId?: string
): {
handleFiles: (files: File[]) => Promise<void>;
isImporting: boolean;
} {
const { documents } = useStores();
const sidebarContext = useSidebarContext();
const [isImporting, setImporting] = useState(false);
const { t } = useTranslation();
const history = useHistory();
const handleFiles = useCallback(
async (files: File[] = []) => {
if (importingLock) {
return;
}
// Because this is the onChange handler it's possible for the change to be
// from previously selecting a file to not selecting a file – aka empty
if (!files.length) {
return;
}
setImporting(true);
importingLock = true;
try {
let cId = collectionId;
const redirect = files.length === 1;
if (documentId && !collectionId) {
const document = await documents.fetch(documentId);
invariant(document, "Document not available");
cId = document.collectionId;
}
for (const file of files) {
const toastId = toast.loading(`${t("Uploading")}…`);
try {
const doc = await documents.import(file, documentId, cId, {
publish: true,
});
if (redirect) {
history.push({
pathname: documentPath(doc),
state: { sidebarContext },
});
}
} catch (err) {
toast.error(errToString(err));
} finally {
toast.dismiss(toastId);
}
}
} catch (err) {
toast.error(`${t("Could not import file")}. ${errToString(err)}`);
} finally {
setImporting(false);
importingLock = false;
}
},
[t, documents, history, collectionId, sidebarContext, documentId]
);
return {
handleFiles,
isImporting,
};
}