分享页: 照片按时间倒序、统一间距、卡片右下角显示日期与城市

This commit is contained in:
gallery
2026-11-15 20:33:21 +08:00
parent 8c376575f5
commit 6bec039595
12 changed files with 213 additions and 8 deletions
+1
View File
@@ -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
+6 -1
View File
@@ -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<shareAlbumQuery>(
SHARE_ALBUM_QUERY,
@@ -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
@@ -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',
@@ -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 (
<MediaThumbnail
key={media.id}
media={media}
active={active}
caption={captionText}
selectImage={() => {
dispatchMedia({
type: 'selectImage',
@@ -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}
</PhotoOverlay>
{caption && <Caption>{caption}</Caption>}
</MediaContainer>
)
}
@@ -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;
`
@@ -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
@@ -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,
@@ -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,
+37
View File
@@ -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()
})
})
+125
View File
@@ -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<string, string> = {
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),
}
}
+4 -3
View File
@@ -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 }) => {