This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/hooks/useImportDocument.js
Tom Moor 44920a25f4 feat: Nested document sharing (#2075)
* migration

* frontend routing, api permissioning

* feat: apiVersion=2

* feat: re-writing document links to point to share

* poc nested documents on share links

* fix: nested shareId permissions

* ui and language tweaks, comments

* breadcrumbs

* Add icons to reference list items

* refactor: Breadcrumb component

* tweaks

* Add shared parent note
2021-05-22 19:34:05 -07:00

70 lines
1.8 KiB
JavaScript
Raw 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.

// @flow
import invariant from "invariant";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import useStores from "hooks/useStores";
let importingLock = false;
export default function useImportDocument(
collectionId: string,
documentId?: string
): {| handleFiles: (files: File[]) => Promise<void>, isImporting: boolean |} {
const { documents, ui } = useStores();
const [isImporting, setImporting] = React.useState(false);
const { t } = useTranslation();
const history = useHistory();
const handleFiles = React.useCallback(
async (files = []) => {
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 doc = await documents.import(file, documentId, cId, {
publish: true,
});
if (redirect) {
history.push(doc.url);
}
}
} catch (err) {
ui.showToast(`${t("Could not import file")}. ${err.message}`, {
type: "error",
});
} finally {
setImporting(false);
importingLock = false;
}
},
[t, ui, documents, history, collectionId, documentId]
);
return {
handleFiles,
isImporting,
};
}