mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
create import
This commit is contained in:
1 parent
5244dcd901
commit
992ae61432
12 files changed
+264
-15
No files matched your search
@@ -23,7 +23,10 @@ function Dialogs() {
|
||||
key={id}
|
||||
isOpen={modal.isOpen}
|
||||
fullscreen={modal.fullscreen ?? false}
|
||||
onRequestClose={() => dialogs.closeModal(id)}
|
||||
onRequestClose={() => {
|
||||
modal.onClose?.();
|
||||
dialogs.closeModal(id);
|
||||
}}
|
||||
title={modal.title}
|
||||
style={modal.style}
|
||||
>
|
||||
|
||||
@@ -8,6 +8,7 @@ type DialogDefinition = {
|
||||
isOpen: boolean;
|
||||
fullscreen?: boolean;
|
||||
style?: React.CSSProperties;
|
||||
onClose?: () => void;
|
||||
};
|
||||
|
||||
export default class DialogsStore {
|
||||
@@ -50,6 +51,7 @@ export default class DialogsStore {
|
||||
fullscreen,
|
||||
replace,
|
||||
style,
|
||||
onClose,
|
||||
}: {
|
||||
id?: string;
|
||||
title: string;
|
||||
@@ -57,6 +59,7 @@ export default class DialogsStore {
|
||||
content: React.ReactNode;
|
||||
style?: React.CSSProperties;
|
||||
replace?: boolean;
|
||||
onClose?: () => void;
|
||||
}) => {
|
||||
setTimeout(
|
||||
action(() => {
|
||||
@@ -70,6 +73,7 @@ export default class DialogsStore {
|
||||
fullscreen,
|
||||
style,
|
||||
isOpen: true,
|
||||
onClose,
|
||||
});
|
||||
}),
|
||||
0
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { observer } from "mobx-react";
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useHistory, useLocation } from "react-router-dom";
|
||||
import { toast } from "sonner";
|
||||
import env from "@shared/env";
|
||||
import { IntegrationService } from "@shared/types";
|
||||
@@ -16,23 +17,45 @@ export const Notion = observer(() => {
|
||||
const { t } = useTranslation();
|
||||
const { dialogs } = useStores();
|
||||
const team = useCurrentTeam();
|
||||
const appName = env.APP_NAME;
|
||||
const history = useHistory();
|
||||
const location = useLocation();
|
||||
const queryParams = useQuery();
|
||||
|
||||
const appName = env.APP_NAME;
|
||||
const authUrl = NotionUtils.authUrl({ state: { teamId: team.id } });
|
||||
|
||||
const queryParams = useQuery();
|
||||
const service = queryParams.get("service");
|
||||
const oauthSuccess = queryParams.get("success") === "";
|
||||
const oauthError = queryParams.get("error");
|
||||
const integrationId = queryParams.get("integrationId");
|
||||
|
||||
const clearQueryParams = React.useCallback(() => {
|
||||
history.replace({
|
||||
pathname: location.pathname,
|
||||
search: "",
|
||||
});
|
||||
}, [history, location]);
|
||||
|
||||
const handleSubmit = React.useCallback(() => {
|
||||
dialogs.closeAllModals();
|
||||
clearQueryParams();
|
||||
}, [dialogs, clearQueryParams]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (oauthSuccess && service === IntegrationService.Notion) {
|
||||
if (
|
||||
integrationId &&
|
||||
oauthSuccess &&
|
||||
service === IntegrationService.Notion
|
||||
) {
|
||||
dialogs.openModal({
|
||||
title: t("Import data"),
|
||||
content: <ImportDialog />,
|
||||
content: (
|
||||
<ImportDialog integrationId={integrationId} onSubmit={handleSubmit} />
|
||||
),
|
||||
onClose: clearQueryParams,
|
||||
});
|
||||
}
|
||||
}, [t, dialogs, oauthSuccess, service]);
|
||||
}, [t, dialogs, oauthSuccess, service, clearQueryParams]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!oauthError) {
|
||||
|
||||
@@ -3,13 +3,16 @@ import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import styled from "styled-components";
|
||||
import { s } from "@shared/styles";
|
||||
import { CollectionPermission } from "@shared/types";
|
||||
import {
|
||||
CollectionPermission,
|
||||
ImportData,
|
||||
IntegrationService,
|
||||
} from "@shared/types";
|
||||
import Button from "~/components/Button";
|
||||
import { Emoji } from "~/components/Emoji";
|
||||
import Flex from "~/components/Flex";
|
||||
import InputSelectPermission from "~/components/InputSelectPermission";
|
||||
import Text from "~/components/Text";
|
||||
import useQuery from "~/hooks/useQuery";
|
||||
import useRequest from "~/hooks/useRequest";
|
||||
import { EmptySelectValue } from "~/types";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
@@ -19,10 +22,13 @@ type PageWithPermission = Page & {
|
||||
permission?: CollectionPermission;
|
||||
};
|
||||
|
||||
export function ImportDialog() {
|
||||
type Props = {
|
||||
integrationId: string;
|
||||
onSubmit: () => void;
|
||||
};
|
||||
|
||||
export function ImportDialog({ integrationId, onSubmit }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const queryParams = useQuery();
|
||||
const integrationId = queryParams.get("integrationId"); // integration id will be available here since dialog renders only in success flow.
|
||||
const [pagesWithPermission, setPagesWithPermission] =
|
||||
React.useState<PageWithPermission[]>();
|
||||
|
||||
@@ -53,9 +59,30 @@ export function ImportDialog() {
|
||||
[]
|
||||
);
|
||||
|
||||
const handleStartImport = React.useCallback(() => {
|
||||
console.log("start import");
|
||||
}, []);
|
||||
const handleStartImport = React.useCallback(async () => {
|
||||
const data: ImportData = {
|
||||
collection: pagesWithPermission!.map((page) => ({
|
||||
externalId: page.id,
|
||||
permission: page.permission,
|
||||
})),
|
||||
};
|
||||
|
||||
try {
|
||||
await client.post("/imports.create", {
|
||||
integrationId,
|
||||
service: IntegrationService.Notion,
|
||||
data,
|
||||
});
|
||||
|
||||
toast.success(
|
||||
t("Your import is being processed, you can safely leave this page")
|
||||
);
|
||||
|
||||
onSubmit();
|
||||
} catch (err) {
|
||||
toast.error(err.message);
|
||||
}
|
||||
}, [pagesWithPermission, onSubmit]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (pages?.length) {
|
||||
@@ -74,10 +101,14 @@ export function ImportDialog() {
|
||||
return <div>Loading data</div>;
|
||||
}
|
||||
|
||||
if (pagesWithPermission.length === 0) {
|
||||
return <div>No pages available for import</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<Flex column gap={8}>
|
||||
<div>
|
||||
{pagesWithPermission?.map((page) => (
|
||||
{pagesWithPermission.map((page) => (
|
||||
<PageItem
|
||||
key={page.id}
|
||||
page={page}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable("imports", {
|
||||
id: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
service: {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
state: {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
data: {
|
||||
type: Sequelize.JSONB,
|
||||
allowNull: false,
|
||||
},
|
||||
pendingTaskCount: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
createdById: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: "users",
|
||||
},
|
||||
},
|
||||
integrationId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: "integrations",
|
||||
},
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
await queryInterface.dropTable("imports");
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
import { InferAttributes, InferCreationAttributes } from "sequelize";
|
||||
import {
|
||||
BelongsTo,
|
||||
Column,
|
||||
DataType,
|
||||
Default,
|
||||
ForeignKey,
|
||||
IsIn,
|
||||
Table,
|
||||
} from "sequelize-typescript";
|
||||
import {
|
||||
type ImportData,
|
||||
ImportState,
|
||||
IntegrationService,
|
||||
} from "@shared/types";
|
||||
import Integration from "./Integration";
|
||||
import User from "./User";
|
||||
import IdModel from "./base/IdModel";
|
||||
import Fix from "./decorators/Fix";
|
||||
|
||||
@Table({ tableName: "imports", modelName: "import" })
|
||||
@Fix
|
||||
class Import<T = unknown> extends IdModel<
|
||||
InferAttributes<Import<T>>,
|
||||
Partial<InferCreationAttributes<Import<T>>>
|
||||
> {
|
||||
@IsIn([Object.values(IntegrationService)])
|
||||
@Column(DataType.STRING)
|
||||
service: IntegrationService;
|
||||
|
||||
@IsIn([Object.values(ImportState)])
|
||||
@Column(DataType.STRING)
|
||||
state: ImportState;
|
||||
|
||||
@Column(DataType.JSONB)
|
||||
data: ImportData;
|
||||
|
||||
@Default(0)
|
||||
@Column(DataType.INTEGER)
|
||||
pendingTaskCount: number;
|
||||
|
||||
// associations
|
||||
|
||||
@BelongsTo(() => User, "createdById")
|
||||
createdBy: User;
|
||||
|
||||
@ForeignKey(() => User)
|
||||
@Column(DataType.UUID)
|
||||
createdById: string;
|
||||
|
||||
@BelongsTo(() => Integration, "integrationId")
|
||||
integration: Integration;
|
||||
|
||||
@ForeignKey(() => Integration)
|
||||
@Column(DataType.UUID)
|
||||
integrationId: string;
|
||||
}
|
||||
|
||||
export default Import;
|
||||
@@ -24,6 +24,8 @@ export { default as Group } from "./Group";
|
||||
|
||||
export { default as GroupUser } from "./GroupUser";
|
||||
|
||||
export { default as Import } from "./Import";
|
||||
|
||||
export { default as Integration } from "./Integration";
|
||||
|
||||
export { default as IntegrationAuthentication } from "./IntegrationAuthentication";
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import Router from "koa-router";
|
||||
import { ImportState, UserRole } from "@shared/types";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import validate from "@server/middlewares/validate";
|
||||
import Import from "@server/models/Import";
|
||||
import { authorize } from "@server/policies";
|
||||
import { APIContext } from "@server/types";
|
||||
import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post(
|
||||
"imports.create",
|
||||
auth({ role: UserRole.Admin }),
|
||||
validate(T.ImportsCreateSchema),
|
||||
async (ctx: APIContext<T.ImportsCreateReq>) => {
|
||||
const { integrationId, service, data } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
|
||||
authorize(user, "createImport", user.team);
|
||||
|
||||
await Import.create({
|
||||
service,
|
||||
state: ImportState.Created,
|
||||
data,
|
||||
createdById: user.id,
|
||||
integrationId,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from "./imports";
|
||||
@@ -0,0 +1,20 @@
|
||||
import { z } from "zod";
|
||||
import { CollectionPermission, IntegrationService } from "@shared/types";
|
||||
import { BaseSchema } from "../schema";
|
||||
|
||||
export const ImportsCreateSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
integrationId: z.string().uuid(),
|
||||
service: z.nativeEnum(IntegrationService),
|
||||
data: z.object({
|
||||
collection: z.array(
|
||||
z.object({
|
||||
externalId: z.string(),
|
||||
permission: z.nativeEnum(CollectionPermission).optional(),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export type ImportsCreateReq = z.infer<typeof ImportsCreateSchema>;
|
||||
@@ -20,6 +20,7 @@ import events from "./events";
|
||||
import fileOperationsRoute from "./fileOperations";
|
||||
import groupMemberships from "./groupMemberships";
|
||||
import groups from "./groups";
|
||||
import imports from "./imports";
|
||||
import installation from "./installation";
|
||||
import integrations from "./integrations";
|
||||
import apiErrorHandler from "./middlewares/apiErrorHandler";
|
||||
@@ -97,6 +98,7 @@ router.use("/", fileOperationsRoute.routes());
|
||||
router.use("/", urls.routes());
|
||||
router.use("/", userMemberships.routes());
|
||||
router.use("/", reactions.routes());
|
||||
router.use("/", imports.routes());
|
||||
|
||||
if (!env.isCloudHosted) {
|
||||
router.use("/", installation.routes());
|
||||
|
||||
@@ -54,6 +54,18 @@ export enum FileOperationState {
|
||||
Expired = "expired",
|
||||
}
|
||||
|
||||
export enum ImportState {
|
||||
Created = "created",
|
||||
InProgress = "in_progress",
|
||||
Processed = "processed",
|
||||
Completed = "completed",
|
||||
Errored = "errored",
|
||||
}
|
||||
|
||||
export type ImportData = {
|
||||
collection: { externalId: string; permission?: CollectionPermission }[];
|
||||
};
|
||||
|
||||
export enum MentionType {
|
||||
User = "user",
|
||||
Document = "document",
|
||||
|
||||
Reference in new issue
Block a user