A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
themebuilder: add support for loading from pr, url & base64
Abdullah Atta0157cf496fe6
diff --git a/apps/theme-builder/src/app.tsx b/apps/theme-builder/src/app.tsxnew file mode 100644index 000000000..8a4a8d4b8--- /dev/null+++ b/apps/theme-builder/src/app.tsx@@ -0,0 +1,80 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { useQueryParams } from "@notesnook/web/src/navigation";+import ThemeBuilder from "./components/theme-builder";+import { useEffect, useState } from "react";+import { Loader } from "@notesnook/web/src/components/loader";+import { Flex } from "@theme-ui/components";+import { useStore } from "@notesnook/web/src/stores/theme-store";+import {+ loadThemeFromBase64,+ loadThemeFromPullRequest,+ loadThemeFromURL+} from "./utils/theme-loader";++export function App() {+ const [isLoading, setIsLoading] = useState(false);+ const [{ from_pr, from_url, from_base64 }] = useQueryParams();++ useEffect(() => {+ (async () => {+ try {+ setIsLoading(true);+ const theme = from_pr+ ? await loadThemeFromPullRequest(from_pr)+ : from_url+ ? await loadThemeFromURL(from_url)+ : from_base64+ ? loadThemeFromBase64(from_base64)+ : null;+ if (!theme) return;+ useStore.getState().setTheme(theme);+ } catch (e) {+ console.error(e);+ } finally {+ setIsLoading(false);+ }+ })();+ }, [from_pr, from_url, from_base64]);++ return (+ <Flex+ sx={{+ width: 260,+ flexShrink: 0,+ bg: "background",+ display: "flex",+ overflow: "hidden",+ flexDirection: "column",+ overflowY: "scroll",+ pt: 2,+ rowGap: 2,+ borderLeft: "1px solid var(--border)",+ zIndex: 1001+ }}+ >+ {isLoading ? (+ <Loader title="Downloading theme" text="Please wait..." />+ ) : (+ <ThemeBuilder />+ )}+ </Flex>+ );+}diff --git a/apps/theme-builder/src/index.tsx b/apps/theme-builder/src/index.tsxindex 797f98f5d..c8d05b865 100644--- a/apps/theme-builder/src/index.tsx+++ b/apps/theme-builder/src/index.tsx@@ -19,8 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { init } from "@notesnook/web/src/bootstrap";import { render } from "react-dom";-import ThemeBuilder from "./components/theme-builder";import { BaseThemeProvider } from "@notesnook/web/src/components/theme-provider";+import { App } from "./app";renderApp();@@ -39,7 +39,7 @@ async function renderApp() {}}><Component route={props?.route || "login:email"} />- <ThemeBuilder />+ <App /></BaseThemeProvider>,document.getElementById("root"),() => {diff --git a/apps/theme-builder/src/utils/theme-loader.ts b/apps/theme-builder/src/utils/theme-loader.tsnew file mode 100644index 000000000..6170393a1--- /dev/null+++ b/apps/theme-builder/src/utils/theme-loader.ts@@ -0,0 +1,76 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { ThemeDefinition, validateTheme } from "@notesnook/theme";+import { tryParse } from "@notesnook/web/src/utils/parse";++export async function loadThemeFromPullRequest(id: string) {+ try {+ //streetwriters/notesnook-themes/3+ const [owner, repo, pullRequestNumber] = id.split("/");+ console.log(owner, repo, pullRequestNumber);+ if (+ !owner ||+ !repo ||+ !pullRequestNumber ||+ owner !== "streetwriters" ||+ repo !== "notesnook-themes" ||+ isNaN(parseInt(pullRequestNumber))+ )+ return null;++ const files = await fetch(+ `https://api.github.com/repos/${owner}/${repo}/pulls/${pullRequestNumber}/files`+ ).then((res) => (res.ok ? res.json() : []));+ if (!files || !Array.isArray(files)) return null;++ for (const file of files) {+ if (file.filename?.endsWith("theme.json") && file.raw_url) {+ return loadThemeFromURL(file.raw_url);+ }+ }+ return null;+ } catch (ex) {+ console.error(ex);+ return null;+ }+}++export async function loadThemeFromURL(url: string) {+ try {+ return loadThemeFromJSON(+ await fetch(`https://cors.notesnook.com/${url}`).then((res) => res.json())+ );+ } catch (err) {+ console.error(err);+ return null;+ }+}++export function loadThemeFromBase64(base64: string) {+ return loadThemeFromJSON(+ tryParse(Buffer.from(base64, "base64url").toString("utf-8"))+ );+}++export function loadThemeFromJSON(themeJSON: any) {+ const result = validateTheme(themeJSON);+ if (result.error) return null;+ return themeJSON as ThemeDefinition;+}