Configure Vercel deployment and preserve deployed theme support
This commit is contained in:
@@ -4,6 +4,8 @@ import Icon from "./Icon";
|
||||
import AttachmentList from "./AttachmentList";
|
||||
|
||||
const imageTypes = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
||||
const maxUploadMB = Number(import.meta.env.VITE_MAX_UPLOAD_MB ?? 20);
|
||||
const uploadLimitMB = (purpose) => Math.min(purpose === "banner" ? 8 : 20, maxUploadMB);
|
||||
const isImage = (file) => imageTypes.includes(file.type) || (!file.type && /\.(jpe?g|png|webp|gif)$/i.test(file.name));
|
||||
const fileSize = (size) => size < 1024 * 1024 ? `${Math.max(1, Math.round(size / 1024))} KB` : `${(size / 1024 / 1024).toFixed(1)} MB`;
|
||||
|
||||
@@ -27,7 +29,7 @@ function Dropzone({ purpose, disabled, onFiles, hasBanner }) {
|
||||
<button type="button" className="upload-browse" disabled={disabled} aria-describedby={hintId} onClick={() => input.current.click()}>
|
||||
<Icon name="plus" size={16} /> {banner ? hasBanner ? "Replace banner" : "Choose banner" : "Browse files"}
|
||||
</button>
|
||||
<small id={hintId}>{banner ? "JPG, PNG, WebP or GIF · Up to 8 MB" : "Up to 10 files · 20 MB each"}</small>
|
||||
<small id={hintId}>{banner ? `JPG, PNG, WebP or GIF · Up to ${uploadLimitMB(purpose)} MB` : `Up to 10 files · ${uploadLimitMB(purpose)} MB each`}</small>
|
||||
<input ref={input} className="upload-input" type="file" tabIndex={-1}
|
||||
aria-label={banner ? "Banner image" : "Additional photos & files"}
|
||||
accept={banner ? "image/jpeg,image/png,image/webp,image/gif" : undefined}
|
||||
@@ -106,9 +108,9 @@ export default function ArticleUploads({ banner, attachments, busy, title, subti
|
||||
const additions = [];
|
||||
const existing = [...attachments, ...queue.current.filter((task) => task.purpose === "attachment").map((task) => task.file)];
|
||||
for (const file of files) {
|
||||
const limit = (purpose === "banner" ? 8 : 20) * 1024 * 1024;
|
||||
const limit = uploadLimitMB(purpose) * 1024 * 1024;
|
||||
if (!file.size) { errors.push(`${file.name}: this file is empty.`); continue; }
|
||||
if (file.size > limit) { errors.push(`${file.name}: exceeds the ${purpose === "banner" ? 8 : 20} MB limit.`); continue; }
|
||||
if (file.size > limit) { errors.push(`${file.name}: exceeds the ${uploadLimitMB(purpose)} MB limit.`); continue; }
|
||||
if (purpose === "banner" && !isImage(file)) { errors.push(`${file.name}: choose a JPG, PNG, WebP, or GIF image.`); continue; }
|
||||
if (purpose === "attachment" && existing.some((item) => item.name === file.name && item.size === file.size)) {
|
||||
errors.push(`${file.name}: already added.`); continue;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
const paths = {
|
||||
sun: <><circle cx="12" cy="12" r="4" /><path d="M12 2v2m0 16v2M2 12h2m16 0h2M4.93 4.93l1.42 1.42m11.3 11.3 1.42 1.42M4.93 19.07l1.42-1.42m11.3-11.3 1.42-1.42" /></>,
|
||||
moon: <path d="M20.9 13.1A9 9 0 0 1 10.9 3.1a9 9 0 1 0 10 10Z" />,
|
||||
grip: <path d="M9 5h.01M15 5h.01M9 12h.01M15 12h.01M9 19h.01M15 19h.01" strokeWidth="3" />,
|
||||
upload: <path d="M12 16V3m-5 5 5-5 5 5M4 16v5h16v-5" />,
|
||||
photo: <><rect x="3" y="3" width="18" height="18" rx="3" /><circle cx="8" cy="8" r="1.5" /><path d="m3 17 6-6 4 4 3-3 5 5" /></>,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useId, useState } from "react";
|
||||
import { NavLink, useLocation } from "react-router-dom";
|
||||
import Icon from "./Icon";
|
||||
import { useAdmin } from "./AdminSession";
|
||||
import ThemeToggle from "./ThemeToggle";
|
||||
|
||||
const navItems = [
|
||||
{ label: "About", to: "/" },
|
||||
@@ -55,6 +56,7 @@ function Header({ name }) {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<ThemeToggle />
|
||||
<button type="button" className={`icon-button header-signin ${isAdmin ? "is-admin" : ""}`}
|
||||
aria-label={isAdmin ? "Admin account" : "Admin sign in"} title={isAdmin ? "Admin account" : "Admin sign in"}
|
||||
aria-haspopup="dialog" disabled={checking} onClick={openSignIn}>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import Icon from "./Icon";
|
||||
|
||||
const STORAGE_KEY = "portfolio-theme";
|
||||
const isTheme = (value) => value === "light" || value === "dark";
|
||||
|
||||
function storedTheme() {
|
||||
try {
|
||||
const value = localStorage.getItem(STORAGE_KEY);
|
||||
return isTheme(value) ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default function ThemeToggle() {
|
||||
const [preference, setPreference] = useState(storedTheme);
|
||||
const [systemDark, setSystemDark] = useState(() => window.matchMedia("(prefers-color-scheme: dark)").matches);
|
||||
const theme = preference ?? (systemDark ? "dark" : "light");
|
||||
const nextTheme = theme === "dark" ? "light" : "dark";
|
||||
|
||||
useEffect(() => {
|
||||
const media = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const onSystemChange = (event) => setSystemDark(event.matches);
|
||||
const onStorageChange = (event) => {
|
||||
if (event.key === STORAGE_KEY || event.key === null) {
|
||||
setPreference(isTheme(event.newValue) ? event.newValue : null);
|
||||
}
|
||||
};
|
||||
media.addEventListener("change", onSystemChange);
|
||||
window.addEventListener("storage", onStorageChange);
|
||||
return () => {
|
||||
media.removeEventListener("change", onSystemChange);
|
||||
window.removeEventListener("storage", onStorageChange);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.dataset.theme = theme;
|
||||
document.documentElement.style.colorScheme = theme;
|
||||
document.querySelector('meta[name="theme-color"]')?.setAttribute("content", theme === "dark" ? "#141d19" : "#f7f8f4");
|
||||
}, [theme]);
|
||||
|
||||
function toggle() {
|
||||
setPreference(nextTheme);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, nextTheme);
|
||||
} catch {
|
||||
// Keep the in-memory preference for this visit.
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button type="button" className="icon-button theme-toggle" onClick={toggle}
|
||||
aria-label={`Switch to ${nextTheme} mode`} title={`Switch to ${nextTheme} mode`}>
|
||||
<Icon name={nextTheme === "dark" ? "moon" : "sun"} />
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user