From 26dd892f2188e767ce3ea42e2a9addecfe161c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?mauro=20=F0=9F=A4=99?= Date: Thu, 4 Jul 2024 10:54:11 +0000 Subject: [PATCH] refactor: Update next.config.mjs to allow specific origins for server actions and update download.tsx to open download link in a new tab --- next.config.mjs | 9 ++++++++- src/components/download.tsx | 13 ++++--------- src/lib/db.ts | 35 ++++++++++++++++++----------------- src/lib/releases.ts | 5 +++++ 4 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 src/lib/releases.ts diff --git a/next.config.mjs b/next.config.mjs index 4678774..244af26 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,4 +1,11 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {}; +const nextConfig = { + experimental: { + serverActions: { + // edit: updated to new key. Was previously `allowedForwardedHosts` + allowedOrigins: ['localhost:3000', 'get-zen.vercel.app'], + }, + }, +}; export default nextConfig; diff --git a/src/components/download.tsx b/src/components/download.tsx index ee1c748..2cce79c 100644 --- a/src/components/download.tsx +++ b/src/components/download.tsx @@ -8,24 +8,18 @@ import { Form, FormField, FormItem, FormLabel } from "./ui/form"; import { useForm } from "react-hook-form"; import { z } from 'zod'; import { zodResolver } from '@hookform/resolvers/zod' +import { releases } from "@/lib/releases"; import { addDownload } from "@/lib/db"; const BASE_URL = "https://github.com/zen-browser/desktop/releases/download/latest"; -const releases: any = { - Windows: "zen.installer.exe", - WindowsZip: "zen.win64.zip", - //MacOS: [], - Linux: "zen.linux.tar.bz2", -}; - function getDefaultPlatformBasedOnUserAgent() { let userAgent = ""; if (typeof window !== "undefined") { userAgent = window.navigator.userAgent; } if (userAgent.includes("Win")) { - return "Windows"; + return "WindowsZip"; } if (userAgent.includes("Mac")) { return "MacOS"; @@ -58,6 +52,7 @@ export default function DownloadPage() { console.log("Releases for platform: ", releasesForPlatform) const url = `${BASE_URL}/${releasesForPlatform}`; console.log("URL: ", url) + window.open(url, "_blank"); } return ( @@ -102,7 +97,7 @@ export default function DownloadPage() { Operating System - Windows Installer + Windows Installer Windows (Zip) MacOS Linux diff --git a/src/lib/db.ts b/src/lib/db.ts index bcd3c19..897e916 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,31 +1,32 @@ +"use server"; + import { sql } from '@vercel/postgres'; -import { config } from 'dotenv'; - -var hasLoadedEnv = false; -function loadEnv() { - if (hasLoadedEnv) { - return; - } - config(); -} +import { releases } from './releases'; async function createDownloadsTable() { - loadEnv(); await sql` CREATE TABLE IF NOT EXISTS downloads ( - id SERIAL PRIMARY KEY, - platform TEXT NOT NULL, - count INT NOT NULL + platform TEXT NOT NULL PRIMARY KEY CONSTRAINT platform_check CHECK (platform IN ('WindowsZip', 'Linux')), + count INT NOT NULL DEFAULT 0 ); `; -} +} export async function addDownload(platform: string) { await createDownloadsTable(); + let hasPlatform: any = await sql` + SELECT COUNT(*) FROM downloads WHERE platform = ${platform}; + `; + if (hasPlatform.rows[0].count > 0) { + await sql` + UPDATE downloads + SET count = count + 1 + WHERE platform = ${platform}; + `; + return; + } await sql` INSERT INTO downloads (platform, count) - VALUES (${platform}, 1) - ON CONFLICT (platform) - DO UPDATE SET count = downloads.count + 1; + VALUES (${platform}, 1); `; } diff --git a/src/lib/releases.ts b/src/lib/releases.ts new file mode 100644 index 0000000..6d07da8 --- /dev/null +++ b/src/lib/releases.ts @@ -0,0 +1,5 @@ +export const releases: any = { + WindowsZip: "zen.win64.zip", + //MacOS: [], + Linux: "zen.linux.tar.bz2", +}; \ No newline at end of file