Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/api-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"logoUrl": "",
"columns": 0,
"searchNewTab": true,
"linkStyle": "classic"
"linkStyle": "classic",
"showUrl": true
}
}
```
Expand All @@ -106,6 +107,8 @@

`linkStyle`:首页链接样式,`classic`(默认)| `compact` | `wide`。存量库无此键(或存量值不在枚举内)时按 `classic` 返回。

`showUrl`:首页链接卡是否显示网址,默认 `true`(保持改动前的呈现)。存量库无此键时按 `true` 返回。

### GET /api/wallpaper/today

同源代理 Bing 每日壁纸(无鉴权,公共页未登录也要取图)。
Expand Down Expand Up @@ -218,13 +221,15 @@ Passkey(WebAuthn,/api/auth/passkey/*,@simplewebauthn/server v14):

### 站点设置

- `GET /api/admin/settings` → `{siteTitle, backgroundUrl, wallpaperSource, logoUrl, columns, searchNewTab, linkStyle}`
- `PUT /api/admin/settings` body 可含 `{siteTitle, backgroundUrl, wallpaperSource(bing|custom), logoUrl, columns(0–12), searchNewTab(boolean), linkStyle(classic|compact|wide)}` → 更新后对象
- `GET /api/admin/settings` → `{siteTitle, backgroundUrl, wallpaperSource, logoUrl, columns, searchNewTab, linkStyle, showUrl}`
- `PUT /api/admin/settings` body 可含 `{siteTitle, backgroundUrl, wallpaperSource(bing|custom), logoUrl, columns(0–12), searchNewTab(boolean), linkStyle(classic|compact|wide), showUrl(boolean)}` → 更新后对象

主题(设深色/浅色)**不在**站点设置里:深浅色只由访客本机偏好(顶栏主题按钮,`localStorage` 键 `nav.visitor-theme`)决定,没有偏好时跟随系统外观。`PUT` 传 `theme` 不生效(白名单外的键一律忽略);存量的 `settings.theme` 行不删不迁移,只是停产不用。

`searchNewTab`:搜索是否在新标签页打开。存储键 `search_new_tab`,值 `"1"`/`"0"`(同 `totp_enabled` 的 KV 布尔惯例);库中**无此键**(存量库)时按 `true` 返回。写入只接受布尔值,非布尔返回 400 `searchNewTab must be a boolean`。

`showUrl`:首页链接卡是否显示网址。存储键 `show_url`,值 `"1"`/`"0"`(同 `searchNewTab` 的 KV 布尔惯例);库中**无此键**(存量库)时按 `true` 返回。写入只接受布尔值,非布尔返回 400 `showUrl must be a boolean`,且一次被拒的请求不会写入同包的任何设置键。关闭后只影响普通模式的首页链接卡(`classic`/`wide` 两档不渲染网址行),首页编辑模式恒显示网址。

`wallpaperSource`:首页壁纸来源。存储键 `wallpaper_source`,值 `"bing"`/`"custom"`;库中**无此键**(存量库)时按 `bing` 返回。写入只接受 `bing`/`custom`,其他值返回 400 `wallpaperSource must be 'bing' or 'custom'`。

`linkStyle`:首页链接样式。存储键 `link_style`,值 `"classic"`/`"compact"`/`"wide"`;库中**无此键**(存量库)时按 `classic` 返回,存量脏值同样归一为 `classic`(不改写库行)。写入只接受这三个字符串值(非字符串一律拒绝),其他值返回 400 `linkStyle must be 'classic', 'compact' or 'wide'`,且一次被拒的请求不会写入同包的任何设置键。
Expand Down
11 changes: 11 additions & 0 deletions web/src/components/admin/settings-panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
LINK_STYLE_OPTIONS,
resolveLinkStyle,
} from "@/lib/link-style";
import { resolveShowUrl } from "@/lib/show-url";
import { useNavStore } from "@/lib/nav-store";
import type {
AdminCategory,
Expand Down Expand Up @@ -265,6 +266,8 @@ export function SettingsPanel({
searchNewTab: settings.searchNewTab ?? true,
// 后端该键未落地 / 旧缓存时同样缺字段:契约缺省 classic(读侧统一兜底)
linkStyle: resolveLinkStyle(settings),
// 缺字段时按默认开:首页照旧显示网址(与 linkStyle 同一套读侧兜底)
showUrl: resolveShowUrl(settings),
});

const save = async () => {
Expand Down Expand Up @@ -384,6 +387,14 @@ export function SettingsPanel({
}
/>
</div>
<div className="flex items-center justify-between gap-4 rounded-lg border p-3">
<Label htmlFor="set-show-url">显示网址</Label>
<Switch
id="set-show-url"
checked={resolveShowUrl(form)}
onCheckedChange={(showUrl) => setForm({ ...form, showUrl })}
/>
</div>
</div>
<Button onClick={() => void save()} className="w-fit">
保存设置
Expand Down
73 changes: 59 additions & 14 deletions web/src/components/link-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import type { LinkStyle, SiteCategory, SiteLink } from "@/lib/types";
* 每一条都包一层 `FadeIn`(错峰入场,与改动前一致);外链属性
* `target="_blank" rel="noopener noreferrer"` 三档一致。
*
* 「显示网址」开关(站点设置 `showUrl`,缺省开)只作用于普通模式:关闭后 classic 与
* wide 两档不再渲染网址行(compact 本就没有网址行)。首页编辑模式不传 `showUrl`,
* 走默认 `true` —— 编辑场景要核对地址,恒显示网址。
*
* 首页编辑模式(home-edit-board.tsx)复用同一套「砖的外观」:`linkTileClassName`
* 给出三档外壳类、`LinkTileContent` 给出内层 DOM(图标 / 名称 / 域名)。正常模式
* 仍把外壳挂在 `<a>` 上、DOM 与类名保持改动前一致;编辑模式把它挂在一个
Expand Down Expand Up @@ -47,9 +51,12 @@ export function linkTileClassName(linkStyle: LinkStyle): string {
export function LinkTileContent({
link,
linkStyle,
showUrl = true,
}: {
link: SiteLink;
linkStyle: LinkStyle;
/** 是否渲染网址行(站点设置 `showUrl`);编辑模式不传,恒显示 */
showUrl?: boolean;
}) {
if (linkStyle === "compact") {
return (
Expand Down Expand Up @@ -77,9 +84,11 @@ export function LinkTileContent({
<span className="block truncate text-sm font-medium text-foreground/90">
{link.title}
</span>
<span className="block truncate text-xs text-muted-foreground">
{safeHost(link.url)}
</span>
{showUrl && (
<span className="block truncate text-xs text-muted-foreground">
{safeHost(link.url)}
</span>
)}
</span>
</>
);
Expand All @@ -95,15 +104,25 @@ export function LinkTileContent({
<span className="line-clamp-2 w-full text-sm font-medium text-foreground/90">
{link.title}
</span>
<span className="w-full truncate text-xs text-muted-foreground">
{safeHost(link.url)}
</span>
{showUrl && (
<span className="w-full truncate text-xs text-muted-foreground">
{safeHost(link.url)}
</span>
)}
</>
);
}

/** 经典:图标上、名称下、域名小字(改动前的原样) */
function LinkTile({ link, index }: { link: SiteLink; index: number }) {
function LinkTile({
link,
index,
showUrl,
}: {
link: SiteLink;
index: number;
showUrl: boolean;
}) {
return (
<FadeIn delay={Math.min(index * 0.02, 0.3)}>
<a
Expand All @@ -112,14 +131,22 @@ function LinkTile({ link, index }: { link: SiteLink; index: number }) {
rel="noopener noreferrer"
className={linkTileClassName("classic")}
>
<LinkTileContent link={link} linkStyle="classic" />
<LinkTileContent link={link} linkStyle="classic" showUrl={showUrl} />
</a>
</FadeIn>
);
}

/** 紧凑:小图标左、名称右;无卡片底、不显示域名 */
function CompactLinkTile({ link, index }: { link: SiteLink; index: number }) {
function CompactLinkTile({
link,
index,
showUrl,
}: {
link: SiteLink;
index: number;
showUrl: boolean;
}) {
return (
<FadeIn delay={Math.min(index * 0.02, 0.3)}>
<a
Expand All @@ -129,14 +156,22 @@ function CompactLinkTile({ link, index }: { link: SiteLink; index: number }) {
title={link.title}
className={linkTileClassName("compact")}
>
<LinkTileContent link={link} linkStyle="compact" />
<LinkTileContent link={link} linkStyle="compact" showUrl={showUrl} />
</a>
</FadeIn>
);
}

/** 宽卡片:玻璃底,图标左,右侧名称 + 域名两行 */
function WideLinkTile({ link, index }: { link: SiteLink; index: number }) {
function WideLinkTile({
link,
index,
showUrl,
}: {
link: SiteLink;
index: number;
showUrl: boolean;
}) {
return (
<FadeIn delay={Math.min(index * 0.02, 0.3)}>
<a
Expand All @@ -145,7 +180,7 @@ function WideLinkTile({ link, index }: { link: SiteLink; index: number }) {
rel="noopener noreferrer"
className={linkTileClassName("wide")}
>
<LinkTileContent link={link} linkStyle="wide" />
<LinkTileContent link={link} linkStyle="wide" showUrl={showUrl} />
</a>
</FadeIn>
);
Expand All @@ -154,9 +189,12 @@ function WideLinkTile({ link, index }: { link: SiteLink; index: number }) {
export function LinkGrid({
links,
linkStyle = "classic",
showUrl = true,
}: {
links: SiteLink[];
linkStyle?: LinkStyle;
/** 是否渲染网址行(站点设置 `showUrl`);缺省开 */
showUrl?: boolean;
}) {
const Tile =
linkStyle === "compact"
Expand All @@ -168,7 +206,7 @@ export function LinkGrid({
return (
<div className={LINK_GRID_CLASS[linkStyle]}>
{links.map((link, i) => (
<Tile key={link.id} link={link} index={i} />
<Tile key={link.id} link={link} index={i} showUrl={showUrl} />
))}
</div>
);
Expand Down Expand Up @@ -210,15 +248,22 @@ export function CategorySection({
category,
privateZone = false,
linkStyle = "classic",
showUrl = true,
}: {
category: SiteCategory;
privateZone?: boolean;
linkStyle?: LinkStyle;
/** 是否渲染网址行(站点设置 `showUrl`);缺省开 */
showUrl?: boolean;
}) {
return (
<section id={`cat-${category.id}`} className="scroll-mt-6">
<CategoryHeading category={category} privateZone={privateZone} />
<LinkGrid links={category.links} linkStyle={linkStyle} />
<LinkGrid
links={category.links}
linkStyle={linkStyle}
showUrl={showUrl}
/>
</section>
);
}
4 changes: 4 additions & 0 deletions web/src/components/public-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SearchBar } from "@/components/search-bar";
import { SiteClock } from "@/components/site-clock";
import { WallpaperBackground } from "@/components/wallpaper-background";
import { resolveLinkStyle } from "@/lib/link-style";
import { resolveShowUrl } from "@/lib/show-url";
import { useAuthenticated, useNavStore } from "@/lib/nav-store";
import {
filterByScene,
Expand Down Expand Up @@ -108,6 +109,8 @@ export function PublicPage({ onOpenAdmin }: { onOpenAdmin: () => void }) {
: categories;
// 链接呈现形态(站点设置 linkStyle,缺省 classic;后端无该键时同样回落 classic)
const linkStyle = resolveLinkStyle(settings);
// 是否显示网址(站点设置 showUrl,缺省开):只影响普通模式,编辑模式恒显示
const showUrl = resolveShowUrl(settings);

// 私有标识:/api/private 的分类 DTO 不含 visibility,用「全量里有、公开列表里没有」推导私有分类。
const publicIds = new Set(publicData?.categories.map((c) => c.id) ?? []);
Expand Down Expand Up @@ -208,6 +211,7 @@ export function PublicPage({ onOpenAdmin }: { onOpenAdmin: () => void }) {
category={category}
privateZone={privateIds.has(category.id)}
linkStyle={linkStyle}
showUrl={showUrl}
/>
</FadeIn>
))}
Expand Down
4 changes: 3 additions & 1 deletion web/src/lib/mockDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function seed(): MockState {
columns: 8,
searchNewTab: true,
linkStyle: "classic",
showUrl: true,
},
};
}
Expand All @@ -192,14 +193,15 @@ function load(): MockState {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) {
// 旧 mock 库没有 searchNewTab / wallpaperSource / linkStyle:按契约默认补齐,避免界面读到 undefined
// 旧 mock 库没有 searchNewTab / wallpaperSource / linkStyle / showUrl:按契约默认补齐,避免界面读到 undefined
const state = JSON.parse(raw) as Omit<MockState, "settings"> & {
settings: Partial<SiteSettings> & { theme?: unknown };
};
state.settings = {
searchNewTab: true,
wallpaperSource: "bing",
linkStyle: "classic",
showUrl: true,
...state.settings,
};
// 站点主题已删除:旧 mock 库存下的 theme 行不再属于契约,读入时丢掉(不迁移数据)
Expand Down
13 changes: 13 additions & 0 deletions web/src/lib/show-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { SiteSettings } from "@/lib/types";

/**
* 「显示网址」开关(站点设置 `showUrl`)的唯一取值来源:公共页与后台设置控件共用。
*
* 契约:默认开(保持改动前的呈现)。只有**显式 false** 才隐藏网址行;
* 后端该键未落地、或浏览器里是旧缓存(DTO 无此字段)时一律按开 —— 与 `linkStyle`
* 的读侧兜底同一口径。开关只作用于普通模式(见 components/link-grid.tsx);
* 首页编辑模式恒显示网址(编辑场景要核对地址)。
*/
export function resolveShowUrl(settings?: SiteSettings | null): boolean {
return settings?.showUrl !== false;
}
6 changes: 6 additions & 0 deletions web/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export interface SiteSettings {
* `resolveLinkStyle`(lib/link-style.ts)兜底 classic,与改动前的呈现一致。
*/
linkStyle?: LinkStyle;
/**
* 首页链接卡是否显示网址(后端 KV `show_url`,缺省 true)。
* 声明为可选同样是有意的:缺字段时读侧统一用 `resolveShowUrl`(lib/show-url.ts)
* 兜底 true,即与改动前完全一致的呈现。编辑模式不走这个开关。
*/
showUrl?: boolean;
}

export interface SiteData {
Expand Down
57 changes: 57 additions & 0 deletions web/test/link-grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,60 @@ describe("CategorySection:把样式透传给网格,锚点偏移随顶栏移
expect(screen.getByRole("link", { name: /GitHub/ })).toHaveClass("glass");
});
});

describe("LinkGrid:显示网址开关(showUrl,缺省开)", () => {
it("classic 档 showUrl=false:只留图标与名称,不渲染域名行", () => {
render(<LinkGrid links={links} showUrl={false} />);

const link = screen.getByRole("link", { name: /GitHub/ });
expect(link).toHaveTextContent("GitHub");
expect(link).not.toHaveTextContent("github.com");
});

it("wide 档 showUrl=false:右侧只剩名称一行,不渲染域名", () => {
render(<LinkGrid links={links} linkStyle="wide" showUrl={false} />);

const link = screen.getByRole("link", { name: /GitHub/ });
expect(link).toHaveClass("glass", "glass-hover");
expect(link).toHaveTextContent("GitHub");
expect(link).not.toHaveTextContent("github.com");
});

it("compact 档本来就不渲染域名:开关不改变它的形状", () => {
const { container } = render(
<LinkGrid links={links} linkStyle="compact" showUrl={false} />,
);

const link = screen.getByRole("link", { name: /GitHub/ });
expect(link).not.toHaveTextContent("github.com");
expect(link).toHaveClass("items-center");
expect(grid(container)).toHaveClass("lg:grid-cols-5");
});

it("不传 showUrl(编辑模式口径):classic / wide 仍显示域名", () => {
for (const style of ["classic", "wide"] as LinkStyle[]) {
const { unmount } = render(<LinkGrid links={links} linkStyle={style} />);
expect(screen.getByRole("link", { name: /GitHub/ })).toHaveTextContent(
"github.com",
);
unmount();
}
});

it("CategorySection 把 showUrl 透传给网格", () => {
const category: SiteCategory = {
id: "cat-dev",
name: "开发",
icon: null,
sceneId: "s_default",
sortOrder: 0,
links,
};

render(<CategorySection category={category} showUrl={false} />);

expect(screen.getByRole("link", { name: /GitHub/ })).not.toHaveTextContent(
"github.com",
);
});
});
Loading
Loading