diff --git a/src/app/features/albums/AlbumView.tsx b/src/app/features/albums/AlbumView.tsx index f90138d6..f1a7f01a 100644 --- a/src/app/features/albums/AlbumView.tsx +++ b/src/app/features/albums/AlbumView.tsx @@ -14,7 +14,14 @@ import { markAsRead } from '../../utils/notifications'; import { getBlobSafeMimeType } from '../../utils/mimeTypes'; import { timeDayMonYear } from '../../utils/time'; import { getMemberDisplayName } from '../../utils/room'; -import { Album, AlbumFile, TimelineEvent, lastEvent, looseFiles, looseNote } from './buildAlbums'; +import { + Album, + AlbumFile, + TimelineEvent, + lastEvent, + looseFiles, + looseNoteFor, +} from './buildAlbums'; import { folderOf, listFolder, pathLabel } from './albumFolders'; import { FileReadBy } from './FileReadBy'; import { ShareSet, useShareEnabled } from './ShareSet'; @@ -112,8 +119,10 @@ function useLooseFiles(room: Room): AlbumFile[] { useEffect(() => { const on = () => bump((n) => n + 1); room.on(RoomEvent.Timeline, on); + room.on(RoomEvent.Redaction, on); return () => { room.off(RoomEvent.Timeline, on); + room.off(RoomEvent.Redaction, on); }; }, [room]); const events: TimelineEvent[] = room @@ -143,6 +152,12 @@ export function AlbumView({ room, onTimeline }: AlbumViewProps) { const encrypted = room.hasEncryptionStateEvent(); const canShare = useShareEnabled(room); const loose = useLooseFiles(room); + const looseNote = looseNoteFor(loose, { + open: !!open, + encrypted, + loading, + error: !!error, + }); let body: React.ReactNode; if (loading) body = Loading albums…; @@ -232,9 +247,9 @@ export function AlbumView({ room, onTimeline }: AlbumViewProps) {
{body} - {!open && !encrypted && !loading && !error && loose.length > 0 && ( + {looseNote && ( - {looseNote(loose)} + {looseNote} )} {/* Stays mounted while an album is open, so a running send keeps its progress. */} diff --git a/src/app/features/albums/buildAlbums.ts b/src/app/features/albums/buildAlbums.ts index 4a09500b..5d3662e1 100644 --- a/src/app/features/albums/buildAlbums.ts +++ b/src/app/features/albums/buildAlbums.ts @@ -175,3 +175,10 @@ export const looseNote = (files: AlbumFile[]): string => { files.length === 1 ? 'It is' : 'They are' } in the Timeline.`; }; + +/** Where the album view stands when it decides whether to show the loose-files note. */ +export type ViewState = { open: boolean; encrypted: boolean; loading: boolean; error: boolean }; + +/** The note for the list view only: never inside an open album, an encrypted zone, or a load that is not finished. */ +export const looseNoteFor = (files: AlbumFile[], view: ViewState): string => + view.open || view.encrypted || view.loading || view.error ? '' : looseNote(files); diff --git a/src/app/features/albums/publisherWire.test.ts b/src/app/features/albums/publisherWire.test.ts index a63ab29c..83bddf3a 100644 --- a/src/app/features/albums/publisherWire.test.ts +++ b/src/app/features/albums/publisherWire.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { buildAlbums, looseFiles, looseNote, TimelineEvent } from './buildAlbums'; +import { buildAlbums, looseFiles, looseNote, looseNoteFor, TimelineEvent } from './buildAlbums'; // The Commons publisher as it posted before gamecult-ops #121: loose files (no album thread), a // flat `org.gamecult.publish.replaces` key, no info.sha256. The sender must see that, not a @@ -62,6 +62,33 @@ describe('loose files from a poster that does not thread', () => { }); }); +describe('where the loose-files note shows', () => { + const loose = looseFiles([file('$a', 'notes.md')]); + const list = { open: false, encrypted: false, loading: false, error: false }; + + it('on the album list, and only there', () => { + expect(looseNoteFor(loose, list)).toContain('notes.md'); + expect(looseNoteFor(loose, { ...list, open: true })).toBe(''); + expect(looseNoteFor(loose, { ...list, encrypted: true })).toBe(''); + expect(looseNoteFor(loose, { ...list, loading: true })).toBe(''); + expect(looseNoteFor(loose, { ...list, error: true })).toBe(''); + }); + + it('says nothing when there are no loose files', () => { + expect(looseNoteFor([], list)).toBe(''); + }); + + it('names m.notice and m.sticker posts as not files', () => { + const notice = { ...text('$n', 'hi'), content: { msgtype: 'm.notice', body: 'hi' } }; + expect(looseFiles([notice])).toEqual([]); + }); + + it('a file with no name reads as unnamed', () => { + const bare: TimelineEvent = { ...file('$b', ''), content: { msgtype: 'm.file', body: '' } }; + expect(looseNote(looseFiles([bare]))).toContain('unnamed'); + }); +}); + describe('the old wire shape inside an album', () => { const root = text('$root', 'handoff/eureka'); const albumOf = (replies: TimelineEvent[]) => buildAlbums([{ root, replies }], 'gamecult.org')[0];