Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an empty pinned message banner when loading #28633

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/components/views/rooms/PinnedMessageBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface PinnedMessageBannerProps {
export function PinnedMessageBanner({ room, permalinkCreator }: PinnedMessageBannerProps): JSX.Element | null {
const pinnedEventIds = usePinnedEvents(room);
const pinnedEvents = useSortedFetchedPinnedEvents(room, pinnedEventIds);
const eventCount = pinnedEvents.length;
const eventCount = pinnedEvents?.length || 0;
const isSinglePinnedEvent = eventCount === 1;

const [currentEventIndex, setCurrentEventIndex] = useState(eventCount - 1);
Expand All @@ -55,8 +55,19 @@ export function PinnedMessageBanner({ room, permalinkCreator }: PinnedMessageBan
setCurrentEventIndex(() => eventCount - 1);
}, [eventCount]);

const pinnedEvent = pinnedEvents[currentEventIndex];
if (!pinnedEvent) return null;
const pinnedEvent = pinnedEvents?.[currentEventIndex];

// We are fetching the pinned messages
if (!pinnedEvents)
return (
<div
className="mx_PinnedMessageBanner"
aria-label={_t("room|pinned_message_banner|description")}
data-testid="pinned-message-banner"
/>
);
// No pinned messages, we don't display the banner
else if (!pinnedEvent) return null;

const shouldUseMessageEvent = pinnedEvent.isRedacted() || pinnedEvent.isDecryptionFailure();

Expand Down
6 changes: 4 additions & 2 deletions src/hooks/usePinnedEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ async function fetchPinnedEvent(room: Room, pinnedEventId: string, cli: MatrixCl

/**
* Fetch the pinned events
* Returns null if the pinned events are not fetched yet
* @param room
* @param pinnedEventIds
*/
Expand All @@ -189,13 +190,14 @@ export function useFetchedPinnedEvents(room: Room, pinnedEventIds: string[]): Ar
/**
* Fetch the pinned events and sort them by from the oldest to the newest
* The order is determined by the event timestamp
* Returns null if the pinned events are not fetched yet
* @param room
* @param pinnedEventIds
*/
export function useSortedFetchedPinnedEvents(room: Room, pinnedEventIds: string[]): Array<MatrixEvent | null> {
export function useSortedFetchedPinnedEvents(room: Room, pinnedEventIds: string[]): Array<MatrixEvent | null> | null {
const pinnedEvents = useFetchedPinnedEvents(room, pinnedEventIds);
return useMemo(() => {
if (!pinnedEvents) return [];
if (!pinnedEvents) return null;

return pinnedEvents.sort((a, b) => {
if (!a) return -1;
Expand Down
Loading