Skip to content

Commit 427a18a

Browse files
committed
feat(Admin > Edit Album): Clipboard for reference
1 parent b30d189 commit 427a18a

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/components/AdminAlbum/Fields.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useEffect, useRef, useState } from 'react'
99
import useSWR from 'swr'
1010
import xml2js from 'xml2js'
1111

12+
import { transformReference } from '../../models/album'
1213
import type { Gallery, IndexedKeywords, ItemReferenceSource, RawXmlAlbum, RawXmlItem } from '../../types/common'
1314
import ComboBox from '../ComboBox'
1415
import { type XmlItemState } from './AdminAlbumClient'
@@ -352,6 +353,21 @@ export default function Fields(
352353
title="Reference name/ID (ref.name)"
353354
sx={{ flex: 1, minWidth: 0 }}
354355
/>
356+
<IconButton
357+
size="sm"
358+
variant="outlined"
359+
onClick={() => {
360+
const reference = transformReference(editedItem?.ref)
361+
if (reference) {
362+
navigator.clipboard.writeText(reference[0])
363+
}
364+
}}
365+
disabled={!editedItem?.ref?.source || !editedItem?.ref?.name}
366+
title="Copy reference URL to clipboard"
367+
sx={{ minWidth: 'auto', px: 1 }}
368+
>
369+
📋
370+
</IconButton>
355371
</Stack>
356372
<Button onClick={generateXml} variant="solid" color="primary">
357373
Generate XML

src/models/__tests__/album.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('Album library', () => {
193193
source: 'youtube',
194194
},
195195
}
196-
const actual = transformReference(mock)
196+
const actual = transformReference(mock.ref)
197197
if (actual === null) {
198198
expect(actual).not.toBeNull()
199199
} else {
@@ -209,7 +209,7 @@ describe('Album library', () => {
209209
source: 'wikipedia',
210210
},
211211
}
212-
const actual = transformReference(mock)
212+
const actual = transformReference(mock.ref)
213213
if (actual === null) {
214214
expect(actual).not.toBeNull()
215215
} else {

src/models/album.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export const transformPersons = (photoSearchValues: XmlItem['search'], definedPe
108108
return null
109109
}
110110

111-
export const transformReference = (item: XmlItem): [string, string] | null => {
111+
export const transformReference = (ref: XmlItem['ref']): [string, string] | null => {
112+
if (!ref) {
113+
return null
114+
}
112115
const baseUrl = (source: ItemReferenceSource) => {
113116
switch (source) {
114117
case 'facebook':
@@ -125,13 +128,10 @@ export const transformReference = (item: XmlItem): [string, string] | null => {
125128
return null // Return null instead of throwing for unknown sources
126129
}
127130
}
128-
if (!('ref' in item) || !item.ref) {
129-
return null
130-
}
131-
if ('name' in item.ref && 'source' in item.ref) {
132-
const url = baseUrl(item.ref.source)
131+
if ('name' in ref && 'source' in ref) {
132+
const url = baseUrl(ref.source)
133133
if (!url) return null // Return null if source is invalid
134-
return [url + item.ref.name, item.ref.name]
134+
return [url + ref.name, ref.name]
135135
}
136136
return null
137137
}
@@ -225,7 +225,7 @@ const transformJsonSchema = (dirty: XmlAlbum, persons: Person[]): Album => {
225225
photoPath,
226226
mediaPath: (item.type === 'video' && videoPaths) ? videoPaths[0] : photoPath,
227227
videoPaths,
228-
reference: transformReference(item),
228+
reference: transformReference(item?.ref),
229229
}
230230

231231
return removeUndefinedFields(out)

0 commit comments

Comments
 (0)