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} */ /** @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; export default nextConfig;

View File

@@ -8,24 +8,18 @@ import { Form, FormField, FormItem, FormLabel } from "./ui/form";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { z } from 'zod'; import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod' import { zodResolver } from '@hookform/resolvers/zod'
import { releases } from "@/lib/releases";
import { addDownload } from "@/lib/db"; import { addDownload } from "@/lib/db";
const BASE_URL = "https://github.com/zen-browser/desktop/releases/download/latest"; 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() { function getDefaultPlatformBasedOnUserAgent() {
let userAgent = ""; let userAgent = "";
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
userAgent = window.navigator.userAgent; userAgent = window.navigator.userAgent;
} }
if (userAgent.includes("Win")) { if (userAgent.includes("Win")) {
return "Windows"; return "WindowsZip";
} }
if (userAgent.includes("Mac")) { if (userAgent.includes("Mac")) {
return "MacOS"; return "MacOS";
@@ -58,6 +52,7 @@ export default function DownloadPage() {
console.log("Releases for platform: ", releasesForPlatform) console.log("Releases for platform: ", releasesForPlatform)
const url = `${BASE_URL}/${releasesForPlatform}`; const url = `${BASE_URL}/${releasesForPlatform}`;
console.log("URL: ", url) console.log("URL: ", url)
window.open(url, "_blank");
} }
return ( return (
@@ -102,7 +97,7 @@ export default function DownloadPage() {
<SelectContent> <SelectContent>
<SelectGroup> <SelectGroup>
<SelectLabel>Operating System</SelectLabel> <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="WindowsZip">Windows (Zip)</SelectItem>
<SelectItem value="MacOS" disabled>MacOS</SelectItem> <SelectItem value="MacOS" disabled>MacOS</SelectItem>
<SelectItem value="Linux">Linux</SelectItem> <SelectItem value="Linux">Linux</SelectItem>

View File

@@ -1,31 +1,32 @@
import { sql } from '@vercel/postgres'; "use server";
import { config } from 'dotenv';
var hasLoadedEnv = false; import { sql } from '@vercel/postgres';
function loadEnv() { import { releases } from './releases';
if (hasLoadedEnv) {
return;
}
config();
}
async function createDownloadsTable() { async function createDownloadsTable() {
loadEnv();
await sql` await sql`
CREATE TABLE IF NOT EXISTS downloads ( CREATE TABLE IF NOT EXISTS downloads (
id SERIAL PRIMARY KEY, platform TEXT NOT NULL PRIMARY KEY CONSTRAINT platform_check CHECK (platform IN ('WindowsZip', 'Linux')),
platform TEXT NOT NULL, count INT NOT NULL DEFAULT 0
count INT NOT NULL
); );
`; `;
} }
export async function addDownload(platform: string) { export async function addDownload(platform: string) {
await createDownloadsTable(); 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` await sql`
INSERT INTO downloads (platform, count) INSERT INTO downloads (platform, count)
VALUES (${platform}, 1) VALUES (${platform}, 1);
ON CONFLICT (platform)
DO UPDATE SET count = downloads.count + 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",
};