mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
Let page owners turn logo.dev logos on and off
Link pages get a "Website logos" switch on the appearance tab (stored as pages.show_logos, default on). When off, buttons show the letter placeholder and the logo.dev credit is hidden. The switch is disabled with a hint when the server has no LOGO_DEV_TOKEN, which /me now reports as `logoDev`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ import { Hono } from "hono";
|
||||
import { bodyLimit } from "hono/body-limit";
|
||||
import { desc, eq, and } from "drizzle-orm";
|
||||
import type { z } from "zod";
|
||||
import type { ApiToken, CreatedToken } from "../shared/api-types.js";
|
||||
import type { ApiToken, CreatedToken, Me } from "../shared/api-types.js";
|
||||
import {
|
||||
createBlockSchema,
|
||||
createPageSchema,
|
||||
@@ -65,7 +65,7 @@ export function apiRoutes(deps: Deps) {
|
||||
|
||||
.get("/me", (c) => {
|
||||
const { username, isAdmin } = c.var.actor;
|
||||
return c.json({ username, isAdmin });
|
||||
return c.json({ username, isAdmin, logoDev: !!config.logoDevToken } satisfies Me);
|
||||
})
|
||||
|
||||
// Pages (redirects and link pages)
|
||||
|
||||
@@ -27,6 +27,8 @@ export const pages = sqliteTable(
|
||||
title: text("title"),
|
||||
avatar: text("avatar"),
|
||||
theme: text("theme", { mode: "json" }),
|
||||
// Link pages only: show website logos from logo.dev (needs LOGO_DEV_TOKEN).
|
||||
showLogos: integer("show_logos", { mode: "boolean" }).notNull().default(true),
|
||||
createdBy: integer("created_by").references(() => users.id, { onDelete: "set null" }),
|
||||
createdAt: createdAt(),
|
||||
updatedAt: integer("updated_at", { mode: "timestamp" })
|
||||
|
||||
@@ -103,8 +103,8 @@ export function createPage(db: Db, actor: Actor, input: CreatePageInput): PageSu
|
||||
export function updatePage(db: Db, actor: Actor, id: number, input: UpdatePageInput): PageDetail {
|
||||
const page = loadManagedPage(db, actor, id);
|
||||
if (input.dest !== undefined && page.kind !== "redirect") throw new HttpError(400, "Only redirects have a destination");
|
||||
if ((input.title !== undefined || input.theme !== undefined) && page.kind !== "linkpage") {
|
||||
throw new HttpError(400, "Only link pages have a title and theme");
|
||||
if ((input.title !== undefined || input.theme !== undefined || input.showLogos !== undefined) && page.kind !== "linkpage") {
|
||||
throw new HttpError(400, "Only link pages have a title, theme and logos");
|
||||
}
|
||||
if (input.code !== undefined && input.code !== page.code && codeTaken(db, page.namespace, input.code)) {
|
||||
throw new HttpError(409, "Shortcode is not available", { code: ["Already in use or reserved"] });
|
||||
@@ -207,6 +207,7 @@ export function getPageDetail(db: Db, actor: Actor, id: number): PageDetail {
|
||||
return {
|
||||
...toSummary(page, clicks, getPinnedPageId(db) === id),
|
||||
theme: (page.theme as Theme | null) ?? DEFAULT_THEME,
|
||||
showLogos: page.showLogos,
|
||||
avatarUrl: avatarUrl(page.avatar),
|
||||
blocks: page.kind === "linkpage" ? getBlocks(db, id) : [],
|
||||
};
|
||||
|
||||
@@ -52,7 +52,8 @@ export function publicRoutes({ db, config }: Deps) {
|
||||
avatarUrl={avatarUrl(page.avatar)}
|
||||
theme={(page.theme as Theme | null) ?? DEFAULT_THEME}
|
||||
blocks={getBlocks(db, page.id)}
|
||||
logoDevToken={config.logoDevToken}
|
||||
// Logos and their attribution only appear when the page owner enabled them.
|
||||
logoDevToken={page.showLogos ? config.logoDevToken : undefined}
|
||||
showAttribution={!config.disableAttribution}
|
||||
/>,
|
||||
);
|
||||
|
||||
@@ -6,6 +6,8 @@ export type PageKind = "redirect" | "linkpage";
|
||||
export interface Me {
|
||||
username: string;
|
||||
isAdmin: boolean;
|
||||
/** Whether the server has a logo.dev token, i.e. link pages can show website logos. */
|
||||
logoDev: boolean;
|
||||
}
|
||||
|
||||
export interface PageSummary {
|
||||
@@ -31,6 +33,7 @@ export interface Block {
|
||||
|
||||
export interface PageDetail extends PageSummary {
|
||||
theme: Theme;
|
||||
showLogos: boolean;
|
||||
avatarUrl: string | null;
|
||||
blocks: Block[];
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ export const updatePageSchema = z.object({
|
||||
dest: destSchema.optional(),
|
||||
title: titleSchema.optional(),
|
||||
theme: themeSchema.optional(),
|
||||
showLogos: z.boolean().optional(),
|
||||
});
|
||||
export type UpdatePageInput = z.infer<typeof updatePageSchema>;
|
||||
|
||||
|
||||
@@ -102,3 +102,45 @@ export function ColorInput({ value, onChange, label }: { value: string; onChange
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// On/off switch with a label and optional description, e.g. for settings.
|
||||
export function Switch({
|
||||
checked,
|
||||
onChange,
|
||||
label,
|
||||
description,
|
||||
disabled,
|
||||
}: {
|
||||
checked: boolean;
|
||||
onChange: (checked: boolean) => void;
|
||||
label: string;
|
||||
description?: ReactNode;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
const id = useId();
|
||||
return (
|
||||
<div className={cx("flex items-start justify-between gap-4", disabled && "opacity-60")}>
|
||||
<div className="min-w-0">
|
||||
<label htmlFor={id} className="text-sm font-medium text-fg">
|
||||
{label}
|
||||
</label>
|
||||
{description && <div className="mt-0.5 text-xs text-muted">{description}</div>}
|
||||
</div>
|
||||
<button
|
||||
id={id}
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={checked}
|
||||
disabled={disabled}
|
||||
onClick={() => onChange(!checked)}
|
||||
className={cx(
|
||||
"relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full transition-colors",
|
||||
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent disabled:cursor-not-allowed",
|
||||
checked ? "bg-accent" : "bg-border-strong",
|
||||
)}
|
||||
>
|
||||
<span className={cx("inline-block size-5 rounded-full bg-white shadow transition-transform", checked ? "translate-x-5.5" : "translate-x-0.5")} />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { useRef, useState, type ReactNode } from "react";
|
||||
import { Check, ExternalLink, ImageUp, Trash2 } from "lucide-react";
|
||||
import type { PageDetail } from "../../../shared/api-types.js";
|
||||
import type { Me, PageDetail } from "../../../shared/api-types.js";
|
||||
import type { Theme } from "../../../shared/validation.js";
|
||||
import { api } from "../../api.js";
|
||||
import { Button } from "../../components/ui/button.js";
|
||||
import { ColorInput, Field, Input, Segmented } from "../../components/ui/form.js";
|
||||
import { ColorInput, Field, Input, Segmented, Switch } from "../../components/ui/form.js";
|
||||
import { Card, CardBody, CardFooter, CardHeader } from "../../components/ui/surface.js";
|
||||
import { useAction } from "../../components/ui/toast.js";
|
||||
import { cx } from "../../lib/cx.js";
|
||||
@@ -241,16 +241,17 @@ function MockPreview({ title, avatarUrl, theme, page }: { title: string; avatarU
|
||||
);
|
||||
}
|
||||
|
||||
export function AppearanceTab({ page, onChange }: { page: PageDetail; onChange: (page: PageDetail) => void }) {
|
||||
export function AppearanceTab({ page, me, onChange }: { page: PageDetail; me: Me; onChange: (page: PageDetail) => void }) {
|
||||
const [title, setTitle] = useState(page.title ?? "");
|
||||
const [theme, setTheme] = useState(page.theme);
|
||||
const [showLogos, setShowLogos] = useState(page.showLogos);
|
||||
const { busy, run } = useAction();
|
||||
const upload = useAction();
|
||||
const fileInput = useRef<HTMLInputElement>(null);
|
||||
|
||||
const dirty = title !== (page.title ?? "") || !sameTheme(theme, page.theme);
|
||||
const dirty = title !== (page.title ?? "") || !sameTheme(theme, page.theme) || showLogos !== page.showLogos;
|
||||
|
||||
const save = () => run(async () => onChange(await api<PageDetail>("PATCH", `/pages/${page.id}`, { title, theme })), "Appearance saved");
|
||||
const save = () => run(async () => onChange(await api<PageDetail>("PATCH", `/pages/${page.id}`, { title, theme, showLogos })), "Appearance saved");
|
||||
|
||||
const uploadAvatar = (file: File | undefined) => {
|
||||
if (!file) return;
|
||||
@@ -311,8 +312,29 @@ export function AppearanceTab({ page, onChange }: { page: PageDetail; onChange:
|
||||
|
||||
<Card>
|
||||
<CardHeader title="Theme" description="Pick a preset, use a palette from coolors.co or set your own colors." />
|
||||
<CardBody>
|
||||
<CardBody className="flex flex-col gap-7">
|
||||
<ThemePicker theme={theme} onChange={setTheme} />
|
||||
<div className="border-t border-border pt-6">
|
||||
<Switch
|
||||
label="Website logos"
|
||||
checked={me.logoDev && showLogos}
|
||||
onChange={setShowLogos}
|
||||
disabled={!me.logoDev}
|
||||
description={
|
||||
me.logoDev ? (
|
||||
<>
|
||||
Show the logo of each link's website on its button, provided by{" "}
|
||||
<a href="https://logo.dev" target="_blank" rel="noreferrer" className="underline">
|
||||
logo.dev
|
||||
</a>
|
||||
. When off, buttons show the first letter instead and the logo.dev credit is hidden.
|
||||
</>
|
||||
) : (
|
||||
"Not available on this server. An admin can enable it by setting LOGO_DEV_TOKEN."
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</CardBody>
|
||||
<CardFooter>
|
||||
{dirty && <span className="mr-auto text-sm text-muted">Unsaved changes</span>}
|
||||
@@ -322,6 +344,7 @@ export function AppearanceTab({ page, onChange }: { page: PageDetail; onChange:
|
||||
onClick={() => {
|
||||
setTitle(page.title ?? "");
|
||||
setTheme(page.theme);
|
||||
setShowLogos(page.showLogos);
|
||||
}}
|
||||
>
|
||||
Discard
|
||||
|
||||
@@ -109,7 +109,7 @@ export function DetailScreen({ id, kind, tab, me }: { id: number; kind: PageKind
|
||||
|
||||
<div className="animate-fade-in" key={active}>
|
||||
{active === "content" && <ContentTab page={p} me={me} onChange={page.set} />}
|
||||
{active === "appearance" && <AppearanceTab page={p} onChange={page.set} />}
|
||||
{active === "appearance" && <AppearanceTab page={p} me={me} onChange={page.set} />}
|
||||
{active === "analytics" && <AnalyticsTab pageId={p.id} />}
|
||||
{active === "qr" && <QrTab url={url} name={p.code} />}
|
||||
{active === "settings" && <SettingsTab page={p} me={me} onChange={page.set} reload={page.reload} />}
|
||||
|
||||
Reference in New Issue
Block a user