Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DifuseHQ/Kalmia into gitb…
Browse files Browse the repository at this point in the history
…ook-import
  • Loading branch information
hayzamjs committed Nov 27, 2024
2 parents 16c237e + 5d94447 commit a69f3bc
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 46 deletions.
6 changes: 3 additions & 3 deletions web/public/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"error_description": "Beim Verarbeiten Ihrer Anfrage ist ein Fehler aufgetreten.",
"crafted_by": "Entwickelt von",
"import": "Importieren",
"upload_markdown_file": "Markdown-Datei hochladen"


"upload_markdown_file": "Markdown-Datei hochladen",
"new_gitbook": "Neues Gitbook",
"import_gitbook": "GitBook importieren"
}
4 changes: 3 additions & 1 deletion web/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,7 @@
"error_description": "An error occurred while processing your request.",
"crafted_by": "Crafted by",
"import": "Import",
"upload_markdown_file":"Upload Markdown File"
"upload_markdown_file":"Upload Markdown File",
"new_gitbook":"New Gitbook",
"import_gitbook":"Import Gitbook"
}
6 changes: 3 additions & 3 deletions web/public/locales/hi/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"delete_documentation_successfully": "दस्तावेज़ सफलतापूर्वक हटा दिया गया",
"page_deleted_successfully": "पृष्ठ सफलतापूर्वक हटा दिया गया",
"import": "आयात",
"upload_markdown_file": "मार्कडाउन फ़ाइल अपलोड करें"


"upload_markdown_file": "मार्कडाउन फ़ाइल अपलोड करें",
"new_gitbook": "नया गिटबुक",
"import_gitbook": "गिटबुक आयात करें"
}
6 changes: 3 additions & 3 deletions web/public/locales/ml/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"delete_documentation_successfully": "ഡോക്യുമെന്റേഷൻ വിജയകരമായി നീക്കം ചെയ്തു",
"page_deleted_successfully": "പേജ് വിജയകരമായി നീക്കം ചെയ്തു",
"import": "ഇംപോർട്ട്",
"upload_markdown_file": "മാർക്ക്ഡൗൺ ഫയൽ അപ്ലോഡ് ചെയ്യുക"


"upload_markdown_file": "മാർക്ക്ഡൗൺ ഫയൽ അപ്ലോഡ് ചെയ്യുക",
"new_gitbook": "പുതിയ ഗിറ്റ്ബുക്ക്",
"import_gitbook": "ഗിറ്റ്ബുക്ക് ഇറക്കുമതി ചെയ്യുക"
}
5 changes: 2 additions & 3 deletions web/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@
"error_description": "處理您的請求時發生錯誤。",
"crafted_by": "由 Iridia 精心製作",
"import": "导入",
"upload_markdown_file": "上传Markdown文件"


"upload_markdown_file": "上传Markdown文件",
"import_gitbook": "导入GitBook"
}
9 changes: 9 additions & 0 deletions web/src/api/Requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export interface UserPayload {
admin?: boolean;
}

export interface GitBookPayload {
username: string;
password: string;
url: string;
}

export interface UpdateUserPayload {
id: number;
photo?: string;
Expand Down Expand Up @@ -273,6 +279,9 @@ export const deleteUser = (username: string) =>
export const getRootParentId = (docId: number) =>
makeRequest(`/docs/documentation/root-parent-id?id=${docId}`);

export const importGitBook = (data: GitBookPayload) =>
makeRequest("kal-api/docs/import/gitbook", "post", data);

export const oAuthProviders = async (): Promise<string[]> => {
const response = await makeRequest<string[]>("/kal-api/oauth/providers");
if (response.status === "success") {
Expand Down
125 changes: 125 additions & 0 deletions web/src/components/GitBookModal/GitBookModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Icon } from "@iconify/react";
import { useContext, useState } from "react";
import { ModalContext } from "../../context/ModalContext";
import { importGitBook } from "../../api/Requests";
import { t } from "i18next";

export default function GitBookModal() {
const { closeModal } = useContext(ModalContext);

const [details, setDetails] = useState({
username: "",
password: "",
url: "",
});

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
console.log("submit");
console.log(details);

const res = await importGitBook(details);
console.log("res", res);
};

return (
<div className="fixed inset-0 z-50 flex justify-center items-center bg-black bg-opacity-50">
<div className="relative p-4 w-full max-w-md max-h-full">
<div className="relative bg-white rounded-lg shadow dark:bg-gray-700">
<div className="flex items-center justify-between px-4 py-2 md:px- md:py-25 rounded-t ">
<h3 className="text-xl font-semibold text-gray-900 dark:text-white">
Import GitBook
</h3>
<button
onClick={() => closeModal("gitBookModal")}
type="button"
className="end-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white"
data-modal-hide="authentication-modal"
>
<Icon
icon="line-md:close"
className="w-5 h-5 text-gray-900 dark:text-white "
/>
</button>
</div>
<div className="p-4 md:p-4">
<form className="space-y-4" action="#" onSubmit={handleSubmit}>
<div>
<label
htmlFor="email"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
{t("username")}
</label>
<input
type="email"
name="email"
id="email"
className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
placeholder="[email protected]"
value={details.username}
onChange={(e) => {
setDetails({ ...details, username: e.target.value });
}}
/>
</div>
<div>
<label
htmlFor="password"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
{t("password")}
</label>
<input
type="password"
name="password"
id="password"
placeholder="••••••••"
className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
value={details.password}
onChange={(e) => {
setDetails({ ...details, password: e.target.value });
}}
/>
</div>{" "}
<div>
<label
htmlFor="password"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
{t("url")} <span className="text-red-500 ">*</span>
</label>
<input
type="url"
name="gitBookurl"
id="password"
placeholder="https://github.com/example/ExampleDocumentation"
className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
required
value={details.url}
onChange={(e) => {
setDetails({ ...details, url: e.target.value });
}}
/>
</div>
<div className="flex justify-between gap-3">
<button
onClick={() => closeModal("gitBookModal")}
className="w-full dark:text-white focus:outline-none font-medium rounded-lg text-sm px-5 py-2.5 text-center border border-gary-400 dark:border-gray-500 hover:bg-gray-200 dark:hover:bg-gray-500 "
>
{t("cancel")}
</button>
<button
type="submit"
className="w-full flex items-center text-white bg-blue-700 hover:bg-blue-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 "
>
<Icon icon="ei:plus" className="w-6 h-6" /> {t("new_gitbook")}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
);
}
3 changes: 2 additions & 1 deletion web/src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function Navbar() {
setTranslateDropdown(false);
}
},
[setIsOpen, setTranslateDropdown],
[setIsOpen, setTranslateDropdown]
);

useEffect(() => {
Expand All @@ -57,6 +57,7 @@ export default function Navbar() {
}, [isOpen, translateDropdown, handleClickOutside]);

const changeLanguage = (lng: string) => {
if (lng === i18n.language) return;
i18n.changeLanguage(lng);
setTranslateDropdown(false);
};
Expand Down
124 changes: 93 additions & 31 deletions web/src/components/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Icon } from "@iconify/react";
import { AnimatePresence, motion } from "framer-motion";
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import { useContext, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import {
NavLink,
Expand All @@ -13,7 +13,6 @@ import { getDocumentations } from "../../api/Requests";
import { AuthContext, AuthContextType } from "../../context/AuthContext";
import { ModalContext } from "../../context/ModalContext";
import { Documentation } from "../../types/doc";
import { DOMEvent } from "../../types/dom";
import { handleError, hasPermission } from "../../utils/Common";

export default function Sidebar() {
Expand Down Expand Up @@ -92,33 +91,40 @@ export default function Sidebar() {

const smallestId = documentation.reduce(
(min, doc) => (doc.id < min ? doc.id : min),
documentation[0]?.id,
documentation[0]?.id
);

const sidebarRef = useRef<HTMLDivElement | null>(null);
const handleClickOutside = useCallback(
(event: DOMEvent) => {
if (
sidebarRef.current &&
!sidebarRef.current.contains(event.target as Node)
) {
setIsSidebarOpen(false);
const [newDocumentDropdown, setNewDocumentDropdown] =
useState<boolean>(false);
const dropdownRef = useRef<HTMLDivElement | null>(null);

const handleClickOutside = (
ref: React.RefObject<HTMLElement>,
setState: (state: boolean) => void
) => {
return (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
setState(false);
}
},
[setIsSidebarOpen],
);
};
};

useEffect(() => {
if (isSidebarOpen) {
document.addEventListener("mousedown", handleClickOutside);
} else {
document.removeEventListener("mousedown", handleClickOutside);
}
const handleSidebarClick = handleClickOutside(sidebarRef, setIsSidebarOpen);
const handleDropdownClick = handleClickOutside(
dropdownRef,
setNewDocumentDropdown
);

document.addEventListener("mousedown", handleSidebarClick);
document.addEventListener("mousedown", handleDropdownClick);

return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("mousedown", handleSidebarClick);
document.removeEventListener("mousedown", handleDropdownClick);
};
}, [isSidebarOpen, handleClickOutside]);
}, [sidebarRef, setIsSidebarOpen, dropdownRef, setNewDocumentDropdown]);

return (
<AnimatePresence>
Expand Down Expand Up @@ -151,20 +157,76 @@ export default function Sidebar() {
</NavLink>
</li>
{hasPermission(["all", "write"], userDetails) && (
<li>
<motion.button
<li className="relative inline-block z-20 w-full">
<button
onClick={() => {
openModal("createDocumentation", null);
navigate("/dashboard/create-documentation");
newDocumentDropdown
? setNewDocumentDropdown(false)
: setNewDocumentDropdown(true);
}}
whileHover={{ scale: 1.05 }}
className="flex w-full py-2 px-5 my-5 justify-center text-white bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-md text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800"
id="dropdownDefaultButton"
data-dropdown-toggle="dropdown"
className="w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex justify-center items-center gap-1 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
type="button"
>
<span className=" px-1 pt-1 text-left items-center dark:text-white text-md text-sm">
{t("new_documentation")}
</span>
<Icon icon="ei:plus" className="w-7 h-7 dark:text-white" />
</motion.button>
<Icon
icon="gridicons:add-outline"
className="w-6 h-6 dark:text-white"
/>
New
</button>
{newDocumentDropdown && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
ref={dropdownRef}
className="w-full absolute mt-2 right-0 lg:left-0 bg-white rounded-lg shadow-2xl shadow-gary-400 border border-gray-300 dark:border-none dark:bg-gray-700 z-30"
>
<motion.ul
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className=" text-sm text-gray-700 rounded-lg dark:text-gray-200 divide-y divide-gray-200 dark:divide-gray-600 overflow-hidden"
aria-labelledby="new-documnetation-dropdown"
>
<motion.li
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="relative w-full hover:bg-gray-200 py-1 cursor-pointer dark:hover:bg-gray-600 "
>
<div
className={`flex w-full text-start items-center rounded "}`}
onClick={() => {
openModal("createDocumentation", null);
navigate("/dashboard/create-documentation");
}}
>
<p className="w-full p-2.5 ms-2 text-md font-medium text-gray-900 rounded dark:text-gray-300">
{t("new_documentation")}
</p>
</div>
</motion.li>
<motion.li
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="relative w-full hover:bg-gray-200 py-1 cursor-pointer dark:hover:bg-gray-600 "
>
<div
className={`flex w-full text-start items-center rounded "}`}
onClick={() => {
openModal("gitBookModal", null);
}}
>
<p className="w-full p-2.5 ms-2 text-md font-medium text-gray-900 rounded dark:text-gray-300">
{t("import_gitbook")}
</p>
</div>
</motion.li>
</motion.ul>
</motion.div>
)}
</li>
)}
{!documentation || documentation.length <= 0 ? (
Expand Down
Loading

0 comments on commit a69f3bc

Please sign in to comment.