From 6bec039595239c119355249d9bb8da8e77cd6a05 Mon Sep 17 00:00:00 2001 From: gallery Date: Sun, 15 Nov 2026 20:33:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E4=BA=AB=E9=A1=B5:=20=E7=85=A7?= =?UTF-8?q?=E7=89=87=E6=8C=89=E6=97=B6=E9=97=B4=E5=80=92=E5=BA=8F=E3=80=81?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E9=97=B4=E8=B7=9D=E3=80=81=E5=8D=A1=E7=89=87?= =?UTF-8?q?=E5=8F=B3=E4=B8=8B=E8=A7=92=E6=98=BE=E7=A4=BA=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E4=B8=8E=E5=9F=8E=E5=B8=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AlbumPage/__generated__/albumQuery.ts | 1 + ui/src/Pages/SharePage/AlbumSharePage.tsx | 7 +- .../__generated__/AlbumGalleryFields.ts | 1 + .../photoGallery/MediaGallery.test.tsx | 4 + .../components/photoGallery/MediaGallery.tsx | 10 +- .../photoGallery/MediaThumbnail.tsx | 23 +++- .../__generated__/MediaGalleryFields.ts | 1 + .../photoGallery/photoGalleryReducer.test.ts | 3 + .../presentView/PresentMedia.test.tsx | 2 + ui/src/helpers/mediaCaption.test.ts | 37 ++++++ ui/src/helpers/mediaCaption.ts | 125 ++++++++++++++++++ ui/src/hooks/useOrderingParams.ts | 7 +- 12 files changed, 213 insertions(+), 8 deletions(-) create mode 100644 ui/src/helpers/mediaCaption.test.ts create mode 100644 ui/src/helpers/mediaCaption.ts diff --git a/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts b/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts index 3aaad18..dafdc9d 100644 --- a/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts +++ b/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts @@ -71,6 +71,7 @@ export interface albumQuery_album_media_videoWeb { export interface albumQuery_album_media { __typename: "Media"; id: string; + title: string; type: MediaType; /** * A short string that can be used to generate a blured version of the media, to show while the original is loading diff --git a/ui/src/Pages/SharePage/AlbumSharePage.tsx b/ui/src/Pages/SharePage/AlbumSharePage.tsx index 7ff7687..ae6eef8 100644 --- a/ui/src/Pages/SharePage/AlbumSharePage.tsx +++ b/ui/src/Pages/SharePage/AlbumSharePage.tsx @@ -6,6 +6,7 @@ import { gql, useQuery } from '@apollo/client' import { useTranslation } from 'react-i18next' import useURLParameters from '../../hooks/useURLParameters' import useOrderingParams from '../../hooks/useOrderingParams' +import { OrderDirection } from '../../__generated__/globalTypes' import { shareAlbumQuery } from './__generated__/shareAlbumQuery' import useScrollPagination from '../../hooks/useScrollPagination' import PaginateLoader from '../../components/PaginateLoader' @@ -103,7 +104,11 @@ const AlbumSharePage = ({ albumID, token, password }: AlbumSharePageProps) => { const { t } = useTranslation() const urlParams = useURLParameters() - const orderParams = useOrderingParams(urlParams) + const orderParams = useOrderingParams( + urlParams, + 'date_shot', + OrderDirection.DESC + ) const { data, error, loading, fetchMore } = useQuery( SHARE_ALBUM_QUERY, diff --git a/ui/src/components/albumGallery/__generated__/AlbumGalleryFields.ts b/ui/src/components/albumGallery/__generated__/AlbumGalleryFields.ts index 410c0b9..6314657 100644 --- a/ui/src/components/albumGallery/__generated__/AlbumGalleryFields.ts +++ b/ui/src/components/albumGallery/__generated__/AlbumGalleryFields.ts @@ -71,6 +71,7 @@ export interface AlbumGalleryFields_media_videoWeb { export interface AlbumGalleryFields_media { __typename: "Media"; id: string; + title: string; type: MediaType; /** * A short string that can be used to generate a blured version of the media, to show while the original is loading diff --git a/ui/src/components/photoGallery/MediaGallery.test.tsx b/ui/src/components/photoGallery/MediaGallery.test.tsx index 529b025..3260ee0 100644 --- a/ui/src/components/photoGallery/MediaGallery.test.tsx +++ b/ui/src/components/photoGallery/MediaGallery.test.tsx @@ -17,6 +17,7 @@ test('photo gallery with media', () => { media: [ { id: '165', + title: 'photo_165.jpg', type: MediaType.Photo, thumbnail: { url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg', @@ -32,6 +33,7 @@ test('photo gallery with media', () => { }, { id: '122', + title: 'photo_122.jpg', type: MediaType.Photo, thumbnail: null, highRes: null, @@ -42,6 +44,7 @@ test('photo gallery with media', () => { }, { id: '98', + title: 'photo_98.jpg', type: MediaType.Video, thumbnail: null, highRes: null, @@ -94,6 +97,7 @@ describe('photo gallery presenting', () => { media: [ { id: '165', + title: 'photo_165.jpg', type: MediaType.Photo, thumbnail: { url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg', diff --git a/ui/src/components/photoGallery/MediaGallery.tsx b/ui/src/components/photoGallery/MediaGallery.tsx index f9cc83a..ce7f291 100644 --- a/ui/src/components/photoGallery/MediaGallery.tsx +++ b/ui/src/components/photoGallery/MediaGallery.tsx @@ -14,6 +14,7 @@ import { import MediaSidebar from '../sidebar/MediaSidebar/MediaSidebar' import { SidebarContext } from '../sidebar/Sidebar' import { gql } from '@apollo/client' +import { mediaCaption } from '../../helpers/mediaCaption' const Gallery = styled.div` display: flex; @@ -21,7 +22,7 @@ const Gallery = styled.div` align-items: center; min-height: 200px; position: relative; - margin: -4px; + margin: -2px; @media (max-width: 1000px) { /* Compensate for tab bar on mobile */ @@ -37,6 +38,7 @@ export const PhotoFiller = styled.div` export const MEDIA_GALLERY_FRAGMENT = gql` fragment MediaGalleryFields on Media { id + title type blurhash thumbnail { @@ -72,11 +74,17 @@ const MediaGallery = ({ mediaState, dispatchMedia }: MediaGalleryProps) => { mediaElements = media.map((media, index) => { const active = activeIndex == index + const caption = mediaCaption(media.title) + const captionText = caption + ? `${caption.date}${caption.city ? ` ${caption.city}` : ''}` + : null + return ( { dispatchMedia({ type: 'selectImage', diff --git a/ui/src/components/photoGallery/MediaThumbnail.tsx b/ui/src/components/photoGallery/MediaThumbnail.tsx index 7034965..2311ad7 100644 --- a/ui/src/components/photoGallery/MediaThumbnail.tsx +++ b/ui/src/components/photoGallery/MediaThumbnail.tsx @@ -9,7 +9,7 @@ const MediaContainer = styled.div` flex-grow: 1; flex-basis: 0; height: 200px; - margin: 4px 2px; + margin: 2px; background-color: #eee; position: relative; overflow: hidden; @@ -136,9 +136,24 @@ const VideoThumbnailIcon = styled(VideoThumbnailIconSVG)` top: calc(50% - 22px); ` +const Caption = styled.div` + position: absolute; + right: 6px; + bottom: 4px; + color: #ffd400; + font-size: 12px; + font-weight: 600; + line-height: 1.2; + white-space: nowrap; + pointer-events: none; + text-shadow: -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, + 1px 1px 0 #fff, 0 0 2px rgba(0, 0, 0, 0.35); +` + type MediaThumbnailProps = { media: MediaGalleryFields active: boolean + caption?: string | null selectImage(): void clickPresent(): void clickFavorite(): void @@ -147,6 +162,7 @@ type MediaThumbnailProps = { export const MediaThumbnail = ({ media, active, + caption, selectImage, clickPresent, clickFavorite, @@ -181,7 +197,7 @@ export const MediaThumbnail = ({ key={media.id} style={{ cursor: 'pointer', - minWidth: `clamp(124px, ${minWidth}px, 100% - 8px)`, + minWidth: `clamp(124px, ${minWidth}px, 100% - 4px)`, }} onClick={() => { clickPresent() @@ -206,6 +222,7 @@ export const MediaThumbnail = ({ /> {heartIcon} + {caption && {caption}} ) } @@ -214,7 +231,7 @@ export const MediaPlaceholder = styled.div` flex-grow: 1; height: 200px; width: 300px; - margin: 4px 2px; + margin: 2px; background-color: #eee; position: relative; ` diff --git a/ui/src/components/photoGallery/__generated__/MediaGalleryFields.ts b/ui/src/components/photoGallery/__generated__/MediaGalleryFields.ts index 03b7c74..34b42ac 100644 --- a/ui/src/components/photoGallery/__generated__/MediaGalleryFields.ts +++ b/ui/src/components/photoGallery/__generated__/MediaGalleryFields.ts @@ -44,6 +44,7 @@ export interface MediaGalleryFields_videoWeb { export interface MediaGalleryFields { __typename: "Media"; id: string; + title: string; type: MediaType; /** * A short string that can be used to generate a blured version of the media, to show while the original is loading diff --git a/ui/src/components/photoGallery/photoGalleryReducer.test.ts b/ui/src/components/photoGallery/photoGalleryReducer.test.ts index d7f857d..d977801 100644 --- a/ui/src/components/photoGallery/photoGalleryReducer.test.ts +++ b/ui/src/components/photoGallery/photoGalleryReducer.test.ts @@ -9,6 +9,7 @@ describe('photo gallery reducer', () => { { __typename: 'Media', id: '1', + title: 'photo_1.jpg', highRes: null, thumbnail: null, blurhash: null, @@ -17,6 +18,7 @@ describe('photo gallery reducer', () => { { __typename: 'Media', id: '2', + title: 'photo_2.jpg', highRes: null, thumbnail: null, blurhash: null, @@ -25,6 +27,7 @@ describe('photo gallery reducer', () => { { __typename: 'Media', id: '3', + title: 'photo_3.jpg', highRes: null, thumbnail: null, blurhash: null, diff --git a/ui/src/components/photoGallery/presentView/PresentMedia.test.tsx b/ui/src/components/photoGallery/presentView/PresentMedia.test.tsx index 09ffeb4..dd995a6 100644 --- a/ui/src/components/photoGallery/presentView/PresentMedia.test.tsx +++ b/ui/src/components/photoGallery/presentView/PresentMedia.test.tsx @@ -9,6 +9,7 @@ test('render present image', () => { const media: MediaGalleryFields = { __typename: 'Media', id: '123', + title: 'sample_image.jpg', type: MediaType.Photo, highRes: null, blurhash: null, @@ -37,6 +38,7 @@ test('render present video', () => { const media: MediaGalleryFields = { __typename: 'Media', id: '123', + title: 'sample_video.mp4', type: MediaType.Video, highRes: null, blurhash: null, diff --git a/ui/src/helpers/mediaCaption.test.ts b/ui/src/helpers/mediaCaption.test.ts new file mode 100644 index 0000000..1dd7076 --- /dev/null +++ b/ui/src/helpers/mediaCaption.test.ts @@ -0,0 +1,37 @@ +import { mediaCaption } from './mediaCaption' + +describe('mediaCaption', () => { + test('parses date and city from a compact filename', () => { + expect(mediaCaption('20251115@DG000075.jpg')).toEqual({ + date: '2025/11/15', + city: '东莞', + }) + }) + + test('maps Guangzhou', () => { + expect(mediaCaption('20261115@GZ000120.jpg')).toEqual({ + date: '2026/11/15', + city: '广州', + }) + }) + + test('parses a chinese filename', () => { + expect(mediaCaption('2026年11月15日在广州.jpg')).toEqual({ + date: '2026/11/15', + city: '广州', + }) + }) + + test('infers city for early TD photos by date', () => { + expect(mediaCaption('20220806@TD000001.jpg')?.city).toBe('珠海') + expect(mediaCaption('20230910@TD000020.jpg')?.city).toBe('拉萨') + expect(mediaCaption('20240616@TD000054.jpg')?.city).toBe('广州') + expect(mediaCaption('20260913@TD000115.jpg')?.city).toBe('北京') + }) + + test('returns null when no date can be parsed', () => { + expect(mediaCaption('random.jpg')).toBeNull() + expect(mediaCaption(null)).toBeNull() + expect(mediaCaption(undefined)).toBeNull() + }) +}) \ No newline at end of file diff --git a/ui/src/helpers/mediaCaption.ts b/ui/src/helpers/mediaCaption.ts new file mode 100644 index 0000000..66cce3f --- /dev/null +++ b/ui/src/helpers/mediaCaption.ts @@ -0,0 +1,125 @@ +export type MediaCaption = { + date: string + city: string | null +} + +// City abbreviations used in media filenames, e.g. `20251115@GZ000001.jpg`. +// Mirrors the abbreviation table in `photos/_plan.py`. +const CITY_BY_CODE: Record = { + GZ: '广州', + FS: '佛山', + DG: '东莞', + SZ: '深圳', + ZS: '中山', + ZH: '珠海', + JM: '江门', + QY: '清远', + ZQ: '肇庆', + SG: '韶关', + ST: '汕头', + ZJ: '湛江', + YC: '盐城', + NJ: '南京', + SH: '上海', + HZ: '杭州', + NB: '宁波', + CD: '成都', + CQ: '重庆', + LS: '拉萨', + CZ: '长治', + TY: '太原', + BJ: '北京', + TJ: '天津', + XA: '西安', + WH: '武汉', + CS: '长沙', + KM: '昆明', + XM: '厦门', + FZ: '福州', + QD: '青岛', + JN: '济南', + ZZ: '郑州', + WLMQ: '乌鲁木齐', + XN: '西宁', + LZ: '兰州', + GY: '贵阳', + NN: '南宁', + HK: '海口', + SY: '三亚', + DL: '大连', + HEB: '哈尔滨', + CC: '长春', + HHHT: '呼和浩特', +} + +const CITY_NAMES = Array.from(new Set(Object.values(CITY_BY_CODE))) + +function pad(value: number): string { + return value < 10 ? `0${value}` : `${value}` +} + +function parseDate( + title: string +): [year: number, month: number, day: number] | null { + const compact = title.match(/^(\d{4})(\d{2})(\d{2})/) + if (compact) { + return [Number(compact[1]), Number(compact[2]), Number(compact[3])] + } + + const chinese = title.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/) + if (chinese) { + return [Number(chinese[1]), Number(chinese[2]), Number(chinese[3])] + } + + const separated = title.match(/(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})/) + if (separated) { + return [Number(separated[1]), Number(separated[2]), Number(separated[3])] + } + + return null +} + +// Early photos could not be assigned a city when they were renamed and were all +// tagged as `TD`. Infer their city from the shooting date, matching the +// inference done by `photos/_plan.py`. +function cityForTd(year: number, month: number, day: number): string { + const ymd = year * 10000 + month * 100 + day + if (ymd <= 20221231) return '珠海' + if (ymd <= 20240113) return '拉萨' + if (ymd <= 20260501) return '广州' + return '北京' +} + +function parseCity( + title: string, + [year, month, day]: [number, number, number] +): string | null { + // Some original filenames contain the city name directly. + const namedCity = CITY_NAMES.find(name => title.includes(name)) + if (namedCity) return namedCity + + const codeMatch = title.match(/[@@]([A-Za-z]{2,5})/) + if (!codeMatch) return null + + const code = codeMatch[1].toUpperCase() + if (code === 'TD') return cityForTd(year, month, day) + + return CITY_BY_CODE[code] ?? null +} + +/** + * Build the caption shown on a media card from its filename, e.g. + * `20251115@GZ000075.jpg` becomes `{ date: '2025/11/15', city: '广州' }`. + */ +export function mediaCaption(title?: string | null): MediaCaption | null { + if (!title) return null + + const date = parseDate(title) + if (!date) return null + + const [year, month, day] = date + return { + date: `${year}/${pad(month)}/${pad(day)}`, + city: parseCity(title, date), + } +} \ No newline at end of file diff --git a/ui/src/hooks/useOrderingParams.ts b/ui/src/hooks/useOrderingParams.ts index 9f85636..4d30967 100644 --- a/ui/src/hooks/useOrderingParams.ts +++ b/ui/src/hooks/useOrderingParams.ts @@ -14,15 +14,16 @@ export type SetOrderingFn = (args: { function useOrderingParams( { getParam, setParams }: UrlParams, - defaultOrderBy = 'date_shot' + defaultOrderBy = 'date_shot', + defaultOrderDirection: OrderDirection = OrderDirection.ASC ) { const rawOrderBy = getParam('orderBy', defaultOrderBy) const orderBy = rawOrderBy === null || rawOrderBy === '' ? defaultOrderBy : rawOrderBy - const rawOrderDir = getParam('orderDirection', OrderDirection.ASC) + const rawOrderDir = getParam('orderDirection', defaultOrderDirection) const orderDirection = Object.values(OrderDirection).includes(rawOrderDir as OrderDirection) - ? (rawOrderDir as OrderDirection) : OrderDirection.ASC + ? (rawOrderDir as OrderDirection) : defaultOrderDirection const setOrdering: SetOrderingFn = useCallback( ({ orderBy, orderDirection }) => {