分享页: 照片按时间倒序、统一间距、卡片右下角显示日期与城市
This commit is contained in:
@@ -71,6 +71,7 @@ export interface albumQuery_album_media_videoWeb {
|
|||||||
export interface albumQuery_album_media {
|
export interface albumQuery_album_media {
|
||||||
__typename: "Media";
|
__typename: "Media";
|
||||||
id: string;
|
id: string;
|
||||||
|
title: string;
|
||||||
type: MediaType;
|
type: MediaType;
|
||||||
/**
|
/**
|
||||||
* A short string that can be used to generate a blured version of the media, to show while the original is loading
|
* A short string that can be used to generate a blured version of the media, to show while the original is loading
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { gql, useQuery } from '@apollo/client'
|
|||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import useURLParameters from '../../hooks/useURLParameters'
|
import useURLParameters from '../../hooks/useURLParameters'
|
||||||
import useOrderingParams from '../../hooks/useOrderingParams'
|
import useOrderingParams from '../../hooks/useOrderingParams'
|
||||||
|
import { OrderDirection } from '../../__generated__/globalTypes'
|
||||||
import { shareAlbumQuery } from './__generated__/shareAlbumQuery'
|
import { shareAlbumQuery } from './__generated__/shareAlbumQuery'
|
||||||
import useScrollPagination from '../../hooks/useScrollPagination'
|
import useScrollPagination from '../../hooks/useScrollPagination'
|
||||||
import PaginateLoader from '../../components/PaginateLoader'
|
import PaginateLoader from '../../components/PaginateLoader'
|
||||||
@@ -103,7 +104,11 @@ const AlbumSharePage = ({ albumID, token, password }: AlbumSharePageProps) => {
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
const urlParams = useURLParameters()
|
const urlParams = useURLParameters()
|
||||||
const orderParams = useOrderingParams(urlParams)
|
const orderParams = useOrderingParams(
|
||||||
|
urlParams,
|
||||||
|
'date_shot',
|
||||||
|
OrderDirection.DESC
|
||||||
|
)
|
||||||
|
|
||||||
const { data, error, loading, fetchMore } = useQuery<shareAlbumQuery>(
|
const { data, error, loading, fetchMore } = useQuery<shareAlbumQuery>(
|
||||||
SHARE_ALBUM_QUERY,
|
SHARE_ALBUM_QUERY,
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export interface AlbumGalleryFields_media_videoWeb {
|
|||||||
export interface AlbumGalleryFields_media {
|
export interface AlbumGalleryFields_media {
|
||||||
__typename: "Media";
|
__typename: "Media";
|
||||||
id: string;
|
id: string;
|
||||||
|
title: string;
|
||||||
type: MediaType;
|
type: MediaType;
|
||||||
/**
|
/**
|
||||||
* A short string that can be used to generate a blured version of the media, to show while the original is loading
|
* 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: [
|
media: [
|
||||||
{
|
{
|
||||||
id: '165',
|
id: '165',
|
||||||
|
title: 'photo_165.jpg',
|
||||||
type: MediaType.Photo,
|
type: MediaType.Photo,
|
||||||
thumbnail: {
|
thumbnail: {
|
||||||
url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg',
|
url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg',
|
||||||
@@ -32,6 +33,7 @@ test('photo gallery with media', () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '122',
|
id: '122',
|
||||||
|
title: 'photo_122.jpg',
|
||||||
type: MediaType.Photo,
|
type: MediaType.Photo,
|
||||||
thumbnail: null,
|
thumbnail: null,
|
||||||
highRes: null,
|
highRes: null,
|
||||||
@@ -42,6 +44,7 @@ test('photo gallery with media', () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '98',
|
id: '98',
|
||||||
|
title: 'photo_98.jpg',
|
||||||
type: MediaType.Video,
|
type: MediaType.Video,
|
||||||
thumbnail: null,
|
thumbnail: null,
|
||||||
highRes: null,
|
highRes: null,
|
||||||
@@ -94,6 +97,7 @@ describe('photo gallery presenting', () => {
|
|||||||
media: [
|
media: [
|
||||||
{
|
{
|
||||||
id: '165',
|
id: '165',
|
||||||
|
title: 'photo_165.jpg',
|
||||||
type: MediaType.Photo,
|
type: MediaType.Photo,
|
||||||
thumbnail: {
|
thumbnail: {
|
||||||
url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg',
|
url: '/photo/thumbnail_3666760020_jpg_x76GG5pS.jpg',
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
import MediaSidebar from '../sidebar/MediaSidebar/MediaSidebar'
|
import MediaSidebar from '../sidebar/MediaSidebar/MediaSidebar'
|
||||||
import { SidebarContext } from '../sidebar/Sidebar'
|
import { SidebarContext } from '../sidebar/Sidebar'
|
||||||
import { gql } from '@apollo/client'
|
import { gql } from '@apollo/client'
|
||||||
|
import { mediaCaption } from '../../helpers/mediaCaption'
|
||||||
|
|
||||||
const Gallery = styled.div`
|
const Gallery = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -21,7 +22,7 @@ const Gallery = styled.div`
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
min-height: 200px;
|
min-height: 200px;
|
||||||
position: relative;
|
position: relative;
|
||||||
margin: -4px;
|
margin: -2px;
|
||||||
|
|
||||||
@media (max-width: 1000px) {
|
@media (max-width: 1000px) {
|
||||||
/* Compensate for tab bar on mobile */
|
/* Compensate for tab bar on mobile */
|
||||||
@@ -37,6 +38,7 @@ export const PhotoFiller = styled.div`
|
|||||||
export const MEDIA_GALLERY_FRAGMENT = gql`
|
export const MEDIA_GALLERY_FRAGMENT = gql`
|
||||||
fragment MediaGalleryFields on Media {
|
fragment MediaGalleryFields on Media {
|
||||||
id
|
id
|
||||||
|
title
|
||||||
type
|
type
|
||||||
blurhash
|
blurhash
|
||||||
thumbnail {
|
thumbnail {
|
||||||
@@ -72,11 +74,17 @@ const MediaGallery = ({ mediaState, dispatchMedia }: MediaGalleryProps) => {
|
|||||||
mediaElements = media.map((media, index) => {
|
mediaElements = media.map((media, index) => {
|
||||||
const active = activeIndex == index
|
const active = activeIndex == index
|
||||||
|
|
||||||
|
const caption = mediaCaption(media.title)
|
||||||
|
const captionText = caption
|
||||||
|
? `${caption.date}${caption.city ? ` ${caption.city}` : ''}`
|
||||||
|
: null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MediaThumbnail
|
<MediaThumbnail
|
||||||
key={media.id}
|
key={media.id}
|
||||||
media={media}
|
media={media}
|
||||||
active={active}
|
active={active}
|
||||||
|
caption={captionText}
|
||||||
selectImage={() => {
|
selectImage={() => {
|
||||||
dispatchMedia({
|
dispatchMedia({
|
||||||
type: 'selectImage',
|
type: 'selectImage',
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ const MediaContainer = styled.div`
|
|||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
flex-basis: 0;
|
flex-basis: 0;
|
||||||
height: 200px;
|
height: 200px;
|
||||||
margin: 4px 2px;
|
margin: 2px;
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
position: relative;
|
position: relative;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
@@ -136,9 +136,24 @@ const VideoThumbnailIcon = styled(VideoThumbnailIconSVG)`
|
|||||||
top: calc(50% - 22px);
|
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 = {
|
type MediaThumbnailProps = {
|
||||||
media: MediaGalleryFields
|
media: MediaGalleryFields
|
||||||
active: boolean
|
active: boolean
|
||||||
|
caption?: string | null
|
||||||
selectImage(): void
|
selectImage(): void
|
||||||
clickPresent(): void
|
clickPresent(): void
|
||||||
clickFavorite(): void
|
clickFavorite(): void
|
||||||
@@ -147,6 +162,7 @@ type MediaThumbnailProps = {
|
|||||||
export const MediaThumbnail = ({
|
export const MediaThumbnail = ({
|
||||||
media,
|
media,
|
||||||
active,
|
active,
|
||||||
|
caption,
|
||||||
selectImage,
|
selectImage,
|
||||||
clickPresent,
|
clickPresent,
|
||||||
clickFavorite,
|
clickFavorite,
|
||||||
@@ -181,7 +197,7 @@ export const MediaThumbnail = ({
|
|||||||
key={media.id}
|
key={media.id}
|
||||||
style={{
|
style={{
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
minWidth: `clamp(124px, ${minWidth}px, 100% - 8px)`,
|
minWidth: `clamp(124px, ${minWidth}px, 100% - 4px)`,
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clickPresent()
|
clickPresent()
|
||||||
@@ -206,6 +222,7 @@ export const MediaThumbnail = ({
|
|||||||
/>
|
/>
|
||||||
{heartIcon}
|
{heartIcon}
|
||||||
</PhotoOverlay>
|
</PhotoOverlay>
|
||||||
|
{caption && <Caption>{caption}</Caption>}
|
||||||
</MediaContainer>
|
</MediaContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -214,7 +231,7 @@ export const MediaPlaceholder = styled.div`
|
|||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
height: 200px;
|
height: 200px;
|
||||||
width: 300px;
|
width: 300px;
|
||||||
margin: 4px 2px;
|
margin: 2px;
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
position: relative;
|
position: relative;
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ export interface MediaGalleryFields_videoWeb {
|
|||||||
export interface MediaGalleryFields {
|
export interface MediaGalleryFields {
|
||||||
__typename: "Media";
|
__typename: "Media";
|
||||||
id: string;
|
id: string;
|
||||||
|
title: string;
|
||||||
type: MediaType;
|
type: MediaType;
|
||||||
/**
|
/**
|
||||||
* A short string that can be used to generate a blured version of the media, to show while the original is loading
|
* 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',
|
__typename: 'Media',
|
||||||
id: '1',
|
id: '1',
|
||||||
|
title: 'photo_1.jpg',
|
||||||
highRes: null,
|
highRes: null,
|
||||||
thumbnail: null,
|
thumbnail: null,
|
||||||
blurhash: null,
|
blurhash: null,
|
||||||
@@ -17,6 +18,7 @@ describe('photo gallery reducer', () => {
|
|||||||
{
|
{
|
||||||
__typename: 'Media',
|
__typename: 'Media',
|
||||||
id: '2',
|
id: '2',
|
||||||
|
title: 'photo_2.jpg',
|
||||||
highRes: null,
|
highRes: null,
|
||||||
thumbnail: null,
|
thumbnail: null,
|
||||||
blurhash: null,
|
blurhash: null,
|
||||||
@@ -25,6 +27,7 @@ describe('photo gallery reducer', () => {
|
|||||||
{
|
{
|
||||||
__typename: 'Media',
|
__typename: 'Media',
|
||||||
id: '3',
|
id: '3',
|
||||||
|
title: 'photo_3.jpg',
|
||||||
highRes: null,
|
highRes: null,
|
||||||
thumbnail: null,
|
thumbnail: null,
|
||||||
blurhash: null,
|
blurhash: null,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ test('render present image', () => {
|
|||||||
const media: MediaGalleryFields = {
|
const media: MediaGalleryFields = {
|
||||||
__typename: 'Media',
|
__typename: 'Media',
|
||||||
id: '123',
|
id: '123',
|
||||||
|
title: 'sample_image.jpg',
|
||||||
type: MediaType.Photo,
|
type: MediaType.Photo,
|
||||||
highRes: null,
|
highRes: null,
|
||||||
blurhash: null,
|
blurhash: null,
|
||||||
@@ -37,6 +38,7 @@ test('render present video', () => {
|
|||||||
const media: MediaGalleryFields = {
|
const media: MediaGalleryFields = {
|
||||||
__typename: 'Media',
|
__typename: 'Media',
|
||||||
id: '123',
|
id: '123',
|
||||||
|
title: 'sample_video.mp4',
|
||||||
type: MediaType.Video,
|
type: MediaType.Video,
|
||||||
highRes: null,
|
highRes: null,
|
||||||
blurhash: null,
|
blurhash: null,
|
||||||
|
|||||||
@@ -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()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,15 +14,16 @@ export type SetOrderingFn = (args: {
|
|||||||
|
|
||||||
function useOrderingParams(
|
function useOrderingParams(
|
||||||
{ getParam, setParams }: UrlParams,
|
{ getParam, setParams }: UrlParams,
|
||||||
defaultOrderBy = 'date_shot'
|
defaultOrderBy = 'date_shot',
|
||||||
|
defaultOrderDirection: OrderDirection = OrderDirection.ASC
|
||||||
) {
|
) {
|
||||||
const rawOrderBy = getParam('orderBy', defaultOrderBy)
|
const rawOrderBy = getParam('orderBy', defaultOrderBy)
|
||||||
const orderBy = rawOrderBy === null || rawOrderBy === '' ? defaultOrderBy : rawOrderBy
|
const orderBy = rawOrderBy === null || rawOrderBy === '' ? defaultOrderBy : rawOrderBy
|
||||||
|
|
||||||
const rawOrderDir = getParam('orderDirection', OrderDirection.ASC)
|
const rawOrderDir = getParam('orderDirection', defaultOrderDirection)
|
||||||
const orderDirection =
|
const orderDirection =
|
||||||
Object.values(OrderDirection).includes(rawOrderDir as OrderDirection)
|
Object.values(OrderDirection).includes(rawOrderDir as OrderDirection)
|
||||||
? (rawOrderDir as OrderDirection) : OrderDirection.ASC
|
? (rawOrderDir as OrderDirection) : defaultOrderDirection
|
||||||
|
|
||||||
const setOrdering: SetOrderingFn = useCallback(
|
const setOrdering: SetOrderingFn = useCallback(
|
||||||
({ orderBy, orderDirection }) => {
|
({ orderBy, orderDirection }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user