refactor: Update next.config.mjs to allow specific origins for server actions and update download.tsx to open download link in a new tab

This commit is contained in:
mauro 🤙
2024-07-04 10:54:11 +00:00
parent a1149838e6
commit 26dd892f21
4 changed files with 35 additions and 27 deletions

View File

@@ -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;

View File

@@ -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() {
<SelectContent>
<SelectGroup>
<SelectLabel>Operating System</SelectLabel>
<SelectItem value="Windows">Windows Installer</SelectItem>
<SelectItem value="Windows" disabled>Windows Installer</SelectItem>
<SelectItem value="WindowsZip">Windows (Zip)</SelectItem>
<SelectItem value="MacOS" disabled>MacOS</SelectItem>
<SelectItem value="Linux">Linux</SelectItem>

View File

@@ -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);
`;
}

5
src/lib/releases.ts Normal file
View File

@@ -0,0 +1,5 @@
export const releases: any = {
WindowsZip: "zen.win64.zip",
//MacOS: [],
Linux: "zen.linux.tar.bz2",
};