This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { buildSiteIconResponse, getDynamicSiteIconUrls } from "@/lib/site-icons";
|
||||||
|
|
||||||
import { buildFallbackIconResponse, getDynamicSiteIconUrls } from "@/lib/site-icons";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const revalidate = 0;
|
export const revalidate = 0;
|
||||||
@@ -8,13 +6,5 @@ export const revalidate = 0;
|
|||||||
export async function GET() {
|
export async function GET() {
|
||||||
const iconUrls = await getDynamicSiteIconUrls();
|
const iconUrls = await getDynamicSiteIconUrls();
|
||||||
|
|
||||||
if (iconUrls.faviconAssetUrl) {
|
return buildSiteIconResponse(iconUrls.faviconAssetUrl);
|
||||||
const response = NextResponse.redirect(iconUrls.faviconAssetUrl, 307);
|
|
||||||
|
|
||||||
response.headers.set("Cache-Control", "no-store, max-age=0");
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
return buildFallbackIconResponse(iconUrls.siteName, 180);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { buildSiteIconResponse, getDynamicSiteIconUrls } from "@/lib/site-icons";
|
||||||
|
|
||||||
import { buildFallbackIconResponse, getDynamicSiteIconUrls } from "@/lib/site-icons";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const revalidate = 0;
|
export const revalidate = 0;
|
||||||
@@ -8,13 +6,5 @@ export const revalidate = 0;
|
|||||||
export async function GET() {
|
export async function GET() {
|
||||||
const iconUrls = await getDynamicSiteIconUrls();
|
const iconUrls = await getDynamicSiteIconUrls();
|
||||||
|
|
||||||
if (iconUrls.faviconAssetUrl) {
|
return buildSiteIconResponse(iconUrls.faviconAssetUrl);
|
||||||
const response = NextResponse.redirect(iconUrls.faviconAssetUrl, 307);
|
|
||||||
|
|
||||||
response.headers.set("Cache-Control", "no-store, max-age=0");
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
return buildFallbackIconResponse(iconUrls.siteName, 32);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ export default async function RootSiteSettingsPage({
|
|||||||
headerTitle={copy.title}
|
headerTitle={copy.title}
|
||||||
headerDescription={copy.subtitle}
|
headerDescription={copy.subtitle}
|
||||||
saveFormId="site-settings-form"
|
saveFormId="site-settings-form"
|
||||||
|
reloadDocumentOnSave
|
||||||
>
|
>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{searchParams?.success ? (
|
{searchParams?.success ? (
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type FormSaveButtonProps = {
|
|||||||
formSelector?: string;
|
formSelector?: string;
|
||||||
formSelectors?: string[];
|
formSelectors?: string[];
|
||||||
label?: string;
|
label?: string;
|
||||||
|
reloadDocumentOnSuccess?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function serializeForm(form: HTMLFormElement) {
|
function serializeForm(form: HTMLFormElement) {
|
||||||
@@ -29,6 +30,7 @@ export function FormSaveButton({
|
|||||||
formSelector,
|
formSelector,
|
||||||
formSelectors,
|
formSelectors,
|
||||||
label = "Speichern",
|
label = "Speichern",
|
||||||
|
reloadDocumentOnSuccess = false,
|
||||||
}: FormSaveButtonProps) {
|
}: FormSaveButtonProps) {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -220,6 +222,11 @@ export function FormSaveButton({
|
|||||||
}
|
}
|
||||||
|
|
||||||
pendingSubmissionRef.current = null;
|
pendingSubmissionRef.current = null;
|
||||||
|
if (reloadDocumentOnSuccess) {
|
||||||
|
window.location.reload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
router.refresh();
|
router.refresh();
|
||||||
|
|
||||||
if (!searchParams.has("__saved")) {
|
if (!searchParams.has("__saved")) {
|
||||||
@@ -233,7 +240,7 @@ export function FormSaveButton({
|
|||||||
router.replace(nextQuery ? `${pathname}?${nextQuery}` : pathname, {
|
router.replace(nextQuery ? `${pathname}?${nextQuery}` : pathname, {
|
||||||
scroll: false,
|
scroll: false,
|
||||||
});
|
});
|
||||||
}, [currentUrl, pathname, router, searchParams]);
|
}, [currentUrl, pathname, reloadDocumentOnSuccess, router, searchParams]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button type="submit" form={activeFormId ?? undefined} disabled={!activeFormId || !isDirty || isSubmitting}>
|
<Button type="submit" form={activeFormId ?? undefined} disabled={!activeFormId || !isDirty || isSubmitting}>
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
/* eslint-disable @next/next/no-img-element */
|
/* eslint-disable @next/next/no-img-element */
|
||||||
|
|
||||||
import type { MediaKind } from "@prisma/client";
|
import type { MediaKind } from "@prisma/client";
|
||||||
|
import { Check, Search } from "lucide-react";
|
||||||
|
import { useEffect, useId, useMemo, useRef, useState } from "react";
|
||||||
|
|
||||||
import type { MediaOption } from "@/lib/media";
|
import type { MediaOption } from "@/lib/media";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@@ -15,6 +17,7 @@ export type MediaFieldState = {
|
|||||||
url: string;
|
url: string;
|
||||||
label: string;
|
label: string;
|
||||||
kind: MediaKind;
|
kind: MediaKind;
|
||||||
|
isCleared?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type MediaFieldPickerProps = {
|
type MediaFieldPickerProps = {
|
||||||
@@ -22,36 +25,89 @@ type MediaFieldPickerProps = {
|
|||||||
value: MediaFieldState;
|
value: MediaFieldState;
|
||||||
onChange: (nextValue: MediaFieldState) => void;
|
onChange: (nextValue: MediaFieldState) => void;
|
||||||
options: MediaOption[];
|
options: MediaOption[];
|
||||||
|
hasInitialValue?: boolean;
|
||||||
inputName: string;
|
inputName: string;
|
||||||
fileFieldName: string;
|
fileFieldName: string;
|
||||||
fileLabel?: string;
|
fileLabel?: string;
|
||||||
externalLabel?: string;
|
externalLabel?: string;
|
||||||
libraryLabel?: string;
|
libraryLabel?: string;
|
||||||
accept?: string;
|
accept?: string;
|
||||||
|
allowExternal?: boolean;
|
||||||
|
allowClear?: boolean;
|
||||||
|
clearLabel?: string;
|
||||||
|
emptyValue?: Partial<MediaFieldState>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const modeOptions: Array<MediaFieldState["mode"]> = ["upload", "external", "library"];
|
|
||||||
|
|
||||||
export function MediaFieldPicker({
|
export function MediaFieldPicker({
|
||||||
title,
|
title,
|
||||||
value,
|
value,
|
||||||
onChange,
|
onChange,
|
||||||
options,
|
options,
|
||||||
|
hasInitialValue = false,
|
||||||
inputName,
|
inputName,
|
||||||
fileFieldName,
|
fileFieldName,
|
||||||
fileLabel,
|
fileLabel,
|
||||||
externalLabel,
|
externalLabel,
|
||||||
libraryLabel,
|
libraryLabel,
|
||||||
accept,
|
accept,
|
||||||
|
allowExternal = true,
|
||||||
|
allowClear = false,
|
||||||
|
clearLabel = "Remove",
|
||||||
|
emptyValue,
|
||||||
}: MediaFieldPickerProps) {
|
}: MediaFieldPickerProps) {
|
||||||
|
const hiddenInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
|
const searchId = useId();
|
||||||
|
const [libraryQuery, setLibraryQuery] = useState("");
|
||||||
|
const modeOptions: Array<MediaFieldState["mode"]> = allowExternal
|
||||||
|
? ["upload", "external", "library"]
|
||||||
|
: ["upload", "library"];
|
||||||
const filteredOptions = options.filter((option) => option.kind === value.kind);
|
const filteredOptions = options.filter((option) => option.kind === value.kind);
|
||||||
const selectedOption = filteredOptions.find((option) => option.id === value.assetId) ?? null;
|
const selectedOption = filteredOptions.find((option) => option.id === value.assetId) ?? null;
|
||||||
const previewUrl =
|
const visibleLibraryOptions = useMemo(() => {
|
||||||
|
const normalizedQuery = libraryQuery.trim().toLowerCase();
|
||||||
|
|
||||||
|
if (!normalizedQuery) {
|
||||||
|
return filteredOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredOptions.filter((option) => option.label.toLowerCase().includes(normalizedQuery));
|
||||||
|
}, [filteredOptions, libraryQuery]);
|
||||||
|
const resolvedLabel =
|
||||||
value.mode === "library"
|
value.mode === "library"
|
||||||
? selectedOption?.url ?? ""
|
? selectedOption?.label ?? value.label
|
||||||
: value.mode === "external"
|
: value.label;
|
||||||
? value.url
|
const serializedValue = JSON.stringify({
|
||||||
: "";
|
mode: value.mode,
|
||||||
|
assetId: value.assetId,
|
||||||
|
url: value.url,
|
||||||
|
label: resolvedLabel,
|
||||||
|
kind: value.kind,
|
||||||
|
});
|
||||||
|
const previewUrl =
|
||||||
|
value.isCleared
|
||||||
|
? ""
|
||||||
|
: value.mode === "library"
|
||||||
|
? selectedOption?.url ?? ""
|
||||||
|
: value.mode === "external"
|
||||||
|
? value.url
|
||||||
|
: ""
|
||||||
|
;
|
||||||
|
const hasCurrentValue =
|
||||||
|
!value.isCleared &&
|
||||||
|
((value.mode === "library" && value.assetId.trim() !== "") ||
|
||||||
|
(value.mode === "external" && value.url.trim() !== ""));
|
||||||
|
const canClear = allowClear && (hasCurrentValue || (hasInitialValue && !value.isCleared));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const hiddenInput = hiddenInputRef.current;
|
||||||
|
|
||||||
|
if (!hiddenInput) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
hiddenInput.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
|
hiddenInput.dispatchEvent(new Event("change", { bubbles: true }));
|
||||||
|
}, [serializedValue]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3 rounded-surface border border-border p-4">
|
<div className="space-y-3 rounded-surface border border-border p-4">
|
||||||
@@ -68,6 +124,7 @@ export function MediaFieldPicker({
|
|||||||
mode,
|
mode,
|
||||||
assetId: mode === "library" ? value.assetId : "",
|
assetId: mode === "library" ? value.assetId : "",
|
||||||
url: mode === "external" ? value.url : "",
|
url: mode === "external" ? value.url : "",
|
||||||
|
isCleared: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -80,29 +137,44 @@ export function MediaFieldPicker({
|
|||||||
{mode}
|
{mode}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
|
{canClear ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
onChange({
|
||||||
|
...value,
|
||||||
|
mode: emptyValue?.mode ?? "upload",
|
||||||
|
assetId: emptyValue?.assetId ?? "",
|
||||||
|
url: emptyValue?.url ?? "",
|
||||||
|
label: emptyValue?.label ?? value.label,
|
||||||
|
isCleared: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
className="rounded-pill border border-destructive/25 px-4 py-2 text-sm text-destructive transition-colors hover:border-destructive/50 hover:bg-destructive/5"
|
||||||
|
>
|
||||||
|
{clearLabel}
|
||||||
|
</button>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
|
ref={hiddenInputRef}
|
||||||
type="hidden"
|
type="hidden"
|
||||||
name={inputName}
|
name={inputName}
|
||||||
value={JSON.stringify({
|
value={serializedValue}
|
||||||
mode: value.mode,
|
|
||||||
assetId: value.assetId,
|
|
||||||
url: value.url,
|
|
||||||
label: value.label,
|
|
||||||
kind: value.kind,
|
|
||||||
})}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="space-y-2">
|
{value.mode !== "library" ? (
|
||||||
<Label>Label</Label>
|
<div className="space-y-2">
|
||||||
<Input
|
<Label>Label</Label>
|
||||||
value={value.label}
|
<Input
|
||||||
onChange={(event) => onChange({ ...value, label: event.target.value })}
|
value={value.label}
|
||||||
placeholder="Homepage Hero"
|
onChange={(event) => onChange({ ...value, label: event.target.value, isCleared: false })}
|
||||||
/>
|
placeholder="Homepage Hero"
|
||||||
</div>
|
/>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
{value.mode === "upload" ? (
|
{value.mode === "upload" ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
@@ -116,27 +188,71 @@ export function MediaFieldPicker({
|
|||||||
<Label>{externalLabel ?? "External URL"}</Label>
|
<Label>{externalLabel ?? "External URL"}</Label>
|
||||||
<Input
|
<Input
|
||||||
value={value.url}
|
value={value.url}
|
||||||
onChange={(event) => onChange({ ...value, url: event.target.value })}
|
onChange={(event) => onChange({ ...value, url: event.target.value, isCleared: false })}
|
||||||
placeholder="https://example.com/image.jpg"
|
placeholder="https://example.com/image.jpg"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
{value.mode === "library" ? (
|
{value.mode === "library" ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-3">
|
||||||
<Label>{libraryLabel ?? "Media Library"}</Label>
|
<Label>{libraryLabel ?? "Media Library"}</Label>
|
||||||
<select
|
<div className="space-y-2 rounded-nested border border-border bg-surface-1 p-3">
|
||||||
value={value.assetId}
|
<div className="relative">
|
||||||
onChange={(event) => onChange({ ...value, assetId: event.target.value })}
|
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||||
className="flex h-11 w-full rounded-pill border border-border bg-background px-4 text-sm text-foreground shadow-sm"
|
<Input
|
||||||
>
|
id={searchId}
|
||||||
<option value="">Select media</option>
|
value={libraryQuery}
|
||||||
{filteredOptions.map((option) => (
|
onChange={(event) => setLibraryQuery(event.target.value)}
|
||||||
<option key={option.id} value={option.id}>
|
className="pl-9"
|
||||||
{option.label}
|
placeholder="Search media"
|
||||||
</option>
|
/>
|
||||||
))}
|
</div>
|
||||||
</select>
|
|
||||||
|
<div className="max-h-64 space-y-2 overflow-y-auto">
|
||||||
|
{visibleLibraryOptions.length > 0 ? (
|
||||||
|
visibleLibraryOptions.map((option) => {
|
||||||
|
const isActive = option.id === value.assetId;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={option.id}
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
onChange({
|
||||||
|
...value,
|
||||||
|
assetId: option.id,
|
||||||
|
label: option.label,
|
||||||
|
isCleared: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
className={cn(
|
||||||
|
"flex w-full items-center gap-3 rounded-nested border px-3 py-2 text-left transition-colors",
|
||||||
|
isActive
|
||||||
|
? "border-border-strong bg-background"
|
||||||
|
: "border-border bg-background/70 hover:border-border-strong hover:bg-background",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{option.url ? (
|
||||||
|
<img src={option.url} alt={option.label} className="h-12 w-12 rounded-md object-cover" />
|
||||||
|
) : (
|
||||||
|
<div className="h-12 w-12 rounded-md border border-border bg-background" />
|
||||||
|
)}
|
||||||
|
<div className="min-w-0 flex-1">
|
||||||
|
<p className="truncate text-sm font-medium text-foreground">{option.label}</p>
|
||||||
|
<p className="truncate text-xs text-muted-foreground">{option.source}</p>
|
||||||
|
</div>
|
||||||
|
{isActive ? <Check className="h-4 w-4 text-brand-primary" /> : null}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
) : (
|
||||||
|
<div className="rounded-nested border border-dashed border-border px-3 py-4 text-sm text-muted-foreground">
|
||||||
|
No media found.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ type RootDashboardShellProps = {
|
|||||||
saveFormId?: string;
|
saveFormId?: string;
|
||||||
saveFormSelector?: string;
|
saveFormSelector?: string;
|
||||||
saveButtonLabel?: string;
|
saveButtonLabel?: string;
|
||||||
|
reloadDocumentOnSave?: boolean;
|
||||||
headerActions?: ReactNode;
|
headerActions?: ReactNode;
|
||||||
sidebarTopContent?: ReactNode;
|
sidebarTopContent?: ReactNode;
|
||||||
toolbar?: ReactNode;
|
toolbar?: ReactNode;
|
||||||
@@ -64,6 +65,7 @@ export async function RootDashboardShell({
|
|||||||
saveFormId,
|
saveFormId,
|
||||||
saveFormSelector,
|
saveFormSelector,
|
||||||
saveButtonLabel,
|
saveButtonLabel,
|
||||||
|
reloadDocumentOnSave = false,
|
||||||
headerActions,
|
headerActions,
|
||||||
sidebarTopContent,
|
sidebarTopContent,
|
||||||
toolbar,
|
toolbar,
|
||||||
@@ -98,6 +100,7 @@ export async function RootDashboardShell({
|
|||||||
formIds={saveFormId ? ["sidebar-maintenance-form", saveFormId] : ["sidebar-maintenance-form"]}
|
formIds={saveFormId ? ["sidebar-maintenance-form", saveFormId] : ["sidebar-maintenance-form"]}
|
||||||
formSelectors={saveFormSelector ? [saveFormSelector] : undefined}
|
formSelectors={saveFormSelector ? [saveFormSelector] : undefined}
|
||||||
label={saveButtonLabel ?? "Speichern"}
|
label={saveButtonLabel ?? "Speichern"}
|
||||||
|
reloadDocumentOnSuccess={reloadDocumentOnSave}
|
||||||
/>
|
/>
|
||||||
<ThemeToggle ariaLabel="Theme wechseln" />
|
<ThemeToggle ariaLabel="Theme wechseln" />
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ function createImageFieldState(
|
|||||||
url: "",
|
url: "",
|
||||||
label,
|
label,
|
||||||
kind: MediaKind.IMAGE,
|
kind: MediaKind.IMAGE,
|
||||||
|
isCleared: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +76,7 @@ function createImageFieldState(
|
|||||||
url,
|
url,
|
||||||
label,
|
label,
|
||||||
kind: MediaKind.IMAGE,
|
kind: MediaKind.IMAGE,
|
||||||
|
isCleared: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +86,7 @@ function createImageFieldState(
|
|||||||
url: "",
|
url: "",
|
||||||
label,
|
label,
|
||||||
kind: MediaKind.IMAGE,
|
kind: MediaKind.IMAGE,
|
||||||
|
isCleared: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +95,10 @@ function getMediaPreviewUrl(
|
|||||||
options: MediaOption[],
|
options: MediaOption[],
|
||||||
fallbackUrl: string | null | undefined,
|
fallbackUrl: string | null | undefined,
|
||||||
) {
|
) {
|
||||||
|
if (value.isCleared) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
if (value.mode === "external") {
|
if (value.mode === "external") {
|
||||||
return value.url || fallbackUrl || "";
|
return value.url || fallbackUrl || "";
|
||||||
}
|
}
|
||||||
@@ -280,12 +287,20 @@ export function SiteSettingsForm({
|
|||||||
value={favicon}
|
value={favicon}
|
||||||
onChange={setFavicon}
|
onChange={setFavicon}
|
||||||
options={mediaOptions}
|
options={mediaOptions}
|
||||||
|
hasInitialValue={Boolean(initialBindings.favicon?.assetId || initialBindings.favicon?.url)}
|
||||||
inputName="faviconMedia"
|
inputName="faviconMedia"
|
||||||
fileFieldName="faviconFile"
|
fileFieldName="faviconFile"
|
||||||
fileLabel="Upload Favicon"
|
fileLabel="Upload Favicon"
|
||||||
externalLabel="Favicon URL"
|
|
||||||
libraryLabel="Favicon Library"
|
libraryLabel="Favicon Library"
|
||||||
accept=".png,.svg,.ico,image/png,image/svg+xml,image/x-icon,image/vnd.microsoft.icon"
|
accept=".png,.svg,.ico,image/png,image/svg+xml,image/x-icon,image/vnd.microsoft.icon"
|
||||||
|
allowExternal={false}
|
||||||
|
allowClear
|
||||||
|
clearLabel="Remove Favicon"
|
||||||
|
emptyValue={{
|
||||||
|
mode: "upload",
|
||||||
|
assetId: "",
|
||||||
|
url: "",
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<MediaFieldPicker
|
<MediaFieldPicker
|
||||||
@@ -293,12 +308,20 @@ export function SiteSettingsForm({
|
|||||||
value={defaultOgImage}
|
value={defaultOgImage}
|
||||||
onChange={setDefaultOgImage}
|
onChange={setDefaultOgImage}
|
||||||
options={mediaOptions}
|
options={mediaOptions}
|
||||||
|
hasInitialValue={Boolean(initialBindings.defaultOgImage?.assetId || initialBindings.defaultOgImage?.url)}
|
||||||
inputName="defaultOgImageMedia"
|
inputName="defaultOgImageMedia"
|
||||||
fileFieldName="defaultOgImageFile"
|
fileFieldName="defaultOgImageFile"
|
||||||
fileLabel="Upload OG Image"
|
fileLabel="Upload OG Image"
|
||||||
externalLabel="OG Image URL"
|
|
||||||
libraryLabel="OG Image Library"
|
libraryLabel="OG Image Library"
|
||||||
accept="image/*,.svg"
|
accept="image/*,.svg"
|
||||||
|
allowExternal={false}
|
||||||
|
allowClear
|
||||||
|
clearLabel="Remove OG Image"
|
||||||
|
emptyValue={{
|
||||||
|
mode: "upload",
|
||||||
|
assetId: "",
|
||||||
|
url: "",
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</AppCard>
|
</AppCard>
|
||||||
|
|||||||
+5
-1
@@ -98,7 +98,9 @@ export async function getSiteSettingsMediaBindings(): Promise<SiteSettingsMediaB
|
|||||||
entityType: SITE_SETTINGS_ENTITY_TYPE,
|
entityType: SITE_SETTINGS_ENTITY_TYPE,
|
||||||
entityId: SITE_SETTINGS_ENTITY_ID,
|
entityId: SITE_SETTINGS_ENTITY_ID,
|
||||||
},
|
},
|
||||||
include: {
|
select: {
|
||||||
|
fieldKey: true,
|
||||||
|
updatedAt: true,
|
||||||
asset: {
|
asset: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -114,6 +116,7 @@ export async function getSiteSettingsMediaBindings(): Promise<SiteSettingsMediaB
|
|||||||
result.favicon = {
|
result.favicon = {
|
||||||
assetId: usage.asset.id,
|
assetId: usage.asset.id,
|
||||||
url: usage.asset.url,
|
url: usage.asset.url,
|
||||||
|
version: usage.updatedAt.toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +124,7 @@ export async function getSiteSettingsMediaBindings(): Promise<SiteSettingsMediaB
|
|||||||
result.defaultOgImage = {
|
result.defaultOgImage = {
|
||||||
assetId: usage.asset.id,
|
assetId: usage.asset.id,
|
||||||
url: usage.asset.url,
|
url: usage.asset.url,
|
||||||
|
version: usage.updatedAt.toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -72,7 +72,7 @@ export function buildAppMetadataFromConfig(
|
|||||||
const openGraphImages = buildMetadataImages(bindings.defaultOgImage?.url);
|
const openGraphImages = buildMetadataImages(bindings.defaultOgImage?.url);
|
||||||
const siteIconUrls = buildSiteIconUrls({
|
const siteIconUrls = buildSiteIconUrls({
|
||||||
siteName: defaultLocaleSettings.siteName,
|
siteName: defaultLocaleSettings.siteName,
|
||||||
faviconAssetId: bindings.favicon?.assetId,
|
faviconVersion: bindings.favicon?.version,
|
||||||
faviconUrl: bindings.favicon?.url,
|
faviconUrl: bindings.favicon?.url,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+50
-39
@@ -1,17 +1,30 @@
|
|||||||
import { ImageResponse } from "next/og";
|
import { readFile } from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
import { routing } from "@/i18n/routing";
|
import { routing } from "@/i18n/routing";
|
||||||
|
|
||||||
import { getSiteSettings, getSiteSettingsMediaBindings } from "./app-config";
|
import { getSiteSettings, getSiteSettingsMediaBindings } from "./app-config";
|
||||||
|
import { isManagedMediaFilePath, resolveMediaUploadPath } from "./media-storage";
|
||||||
|
|
||||||
const INTERNAL_FAVICON_PATH = "/favicon.ico";
|
const INTERNAL_FAVICON_PATH = "/favicon.ico";
|
||||||
const INTERNAL_APPLE_ICON_PATH = "/apple-icon.png";
|
const INTERNAL_APPLE_ICON_PATH = "/apple-icon.png";
|
||||||
const INTERNAL_MANIFEST_PATH = "/manifest.webmanifest";
|
const INTERNAL_MANIFEST_PATH = "/manifest.webmanifest";
|
||||||
const DEFAULT_ICON_VERSION = "default";
|
const DEFAULT_ICON_VERSION = "default";
|
||||||
|
const TRANSPARENT_PNG_BASE64 =
|
||||||
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9sot5WQAAAAASUVORK5CYII=";
|
||||||
|
|
||||||
|
const ICON_CONTENT_TYPES: Record<string, string> = {
|
||||||
|
".ico": "image/x-icon",
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".jpeg": "image/jpeg",
|
||||||
|
".png": "image/png",
|
||||||
|
".svg": "image/svg+xml",
|
||||||
|
".webp": "image/webp",
|
||||||
|
};
|
||||||
|
|
||||||
type SiteIconInput = {
|
type SiteIconInput = {
|
||||||
siteName: string;
|
siteName: string;
|
||||||
faviconAssetId?: string | null;
|
faviconVersion?: string | null;
|
||||||
faviconUrl?: string | null;
|
faviconUrl?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,15 +50,9 @@ function appendVersionToUrl(url: string, version: string): string {
|
|||||||
return resolved.toString();
|
return resolved.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFallbackLabel(siteName: string): string {
|
|
||||||
const firstCharacter = siteName.trim().charAt(0).toUpperCase() || "M";
|
|
||||||
|
|
||||||
return firstCharacter;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildSiteIconUrls(input: SiteIconInput): SiteIconUrls {
|
export function buildSiteIconUrls(input: SiteIconInput): SiteIconUrls {
|
||||||
const siteName = input.siteName.trim() || "Moh";
|
const siteName = input.siteName.trim() || "Moh";
|
||||||
const version = input.faviconAssetId?.trim() || DEFAULT_ICON_VERSION;
|
const version = input.faviconVersion?.trim() || DEFAULT_ICON_VERSION;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
siteName,
|
siteName,
|
||||||
@@ -63,40 +70,44 @@ export async function getDynamicSiteIconUrls(): Promise<SiteIconUrls> {
|
|||||||
|
|
||||||
return buildSiteIconUrls({
|
return buildSiteIconUrls({
|
||||||
siteName: defaultLocaleSettings.siteName,
|
siteName: defaultLocaleSettings.siteName,
|
||||||
faviconAssetId: bindings.favicon?.assetId,
|
faviconVersion: bindings.favicon?.version,
|
||||||
faviconUrl: bindings.favicon?.url,
|
faviconUrl: bindings.favicon?.url,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildFallbackIconResponse(siteName: string, size: number) {
|
function getResolvedPathname(iconUrl: string) {
|
||||||
const label = getFallbackLabel(siteName);
|
const resolvedUrl = new URL(iconUrl, "https://local.invalid");
|
||||||
|
|
||||||
return new ImageResponse(
|
return resolvedUrl.pathname;
|
||||||
(
|
}
|
||||||
<div
|
|
||||||
style={{
|
async function createFileResponse(absolutePath: string) {
|
||||||
width: "100%",
|
const fileBuffer = await readFile(absolutePath);
|
||||||
height: "100%",
|
const contentType = ICON_CONTENT_TYPES[path.extname(absolutePath).toLowerCase()] ?? "application/octet-stream";
|
||||||
display: "flex",
|
|
||||||
alignItems: "center",
|
return new Response(fileBuffer, {
|
||||||
justifyContent: "center",
|
status: 200,
|
||||||
background: "linear-gradient(135deg, #111827 0%, #0f766e 100%)",
|
headers: {
|
||||||
color: "#f8fafc",
|
"Content-Type": contentType,
|
||||||
fontSize: size * 0.52,
|
"Cache-Control": "no-store, max-age=0",
|
||||||
fontWeight: 700,
|
|
||||||
letterSpacing: "-0.08em",
|
|
||||||
borderRadius: size * 0.22,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{label}
|
|
||||||
</div>
|
|
||||||
),
|
|
||||||
{
|
|
||||||
width: size,
|
|
||||||
height: size,
|
|
||||||
headers: {
|
|
||||||
"Cache-Control": "no-store, max-age=0",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function buildSiteIconResponse(iconUrl: string | null) {
|
||||||
|
if (iconUrl) {
|
||||||
|
const pathname = getResolvedPathname(iconUrl);
|
||||||
|
|
||||||
|
if (isManagedMediaFilePath(pathname)) {
|
||||||
|
return createFileResponse(resolveMediaUploadPath(pathname));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(Buffer.from(TRANSPARENT_PNG_BASE64, "base64"), {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "image/png",
|
||||||
|
"Cache-Control": "no-store, max-age=0",
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ export type SiteSettings = {
|
|||||||
export type SiteSettingsMediaBinding = {
|
export type SiteSettingsMediaBinding = {
|
||||||
assetId: string;
|
assetId: string;
|
||||||
url: string;
|
url: string;
|
||||||
|
version: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SiteSettingsMediaBindings = {
|
export type SiteSettingsMediaBindings = {
|
||||||
|
|||||||
Reference in New Issue
Block a user