2026-06-19 11:21:29 -04:00
|
|
|
import React, { ChangeEvent, useCallback, useEffect, useState } from 'react';
|
2026-06-04 12:07:12 -04:00
|
|
|
import {
|
2026-06-19 11:21:29 -04:00
|
|
|
Avatar,
|
2026-06-04 12:07:12 -04:00
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Header,
|
|
|
|
|
Icon,
|
|
|
|
|
IconButton,
|
|
|
|
|
Icons,
|
|
|
|
|
Input,
|
|
|
|
|
Scroll,
|
|
|
|
|
Text,
|
|
|
|
|
color,
|
|
|
|
|
config,
|
|
|
|
|
} from 'folds';
|
2026-06-19 11:21:29 -04:00
|
|
|
import classNames from 'classnames';
|
2026-06-04 10:26:08 -04:00
|
|
|
import { useBookmarks, Bookmark } from '../../hooks/useBookmarks';
|
|
|
|
|
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
|
|
|
|
import { useRoomNavigate } from '../../hooks/useRoomNavigate';
|
2026-06-19 11:21:29 -04:00
|
|
|
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
|
|
|
|
import { RoomAvatar } from '../../components/room-avatar';
|
|
|
|
|
import { getRoomAvatarUrl } from '../../utils/room';
|
|
|
|
|
import { nameInitials } from '../../utils/common';
|
|
|
|
|
import { ContainerColor } from '../../styles/ContainerColor.css';
|
|
|
|
|
import { stopPropagation } from '../../utils/keyboard';
|
|
|
|
|
import * as css from './BookmarksPanel.css';
|
2026-06-04 10:26:08 -04:00
|
|
|
|
|
|
|
|
function formatTimeAgo(ts: number): string {
|
|
|
|
|
const diff = Date.now() - ts;
|
|
|
|
|
const minutes = Math.floor(diff / 60_000);
|
|
|
|
|
if (minutes < 1) return 'just now';
|
|
|
|
|
if (minutes < 60) return `${minutes}m ago`;
|
|
|
|
|
const hours = Math.floor(minutes / 60);
|
|
|
|
|
if (hours < 24) return `${hours}h ago`;
|
|
|
|
|
const days = Math.floor(hours / 24);
|
|
|
|
|
if (days === 1) return 'yesterday';
|
|
|
|
|
if (days < 7) return `${days}d ago`;
|
|
|
|
|
return new Date(ts).toLocaleDateString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BookmarkItemProps = {
|
|
|
|
|
bookmark: Bookmark;
|
|
|
|
|
onJump: (roomId: string, eventId: string) => void;
|
|
|
|
|
onRemove: (eventId: string) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function BookmarkItem({ bookmark, onJump, onRemove }: BookmarkItemProps) {
|
|
|
|
|
const mx = useMatrixClient();
|
2026-06-19 11:21:29 -04:00
|
|
|
const useAuthentication = useMediaAuthentication();
|
|
|
|
|
const room = mx.getRoom(bookmark.roomId) ?? undefined;
|
2026-06-04 10:26:08 -04:00
|
|
|
const displayRoomName = room?.name ?? bookmark.roomName;
|
2026-06-19 11:21:29 -04:00
|
|
|
const avatarUrl = room
|
|
|
|
|
? (getRoomAvatarUrl(mx, room, 96, useAuthentication) ?? undefined)
|
|
|
|
|
: undefined;
|
2026-06-04 10:26:08 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
direction="Column"
|
2026-06-04 12:07:12 -04:00
|
|
|
gap="200"
|
2026-06-04 10:26:08 -04:00
|
|
|
style={{
|
2026-06-19 11:21:29 -04:00
|
|
|
padding: config.space.S300,
|
|
|
|
|
borderRadius: config.radii.R300,
|
|
|
|
|
background: color.SurfaceVariant.Container,
|
2026-06-04 10:26:08 -04:00
|
|
|
}}
|
|
|
|
|
>
|
2026-06-19 11:21:29 -04:00
|
|
|
{/* Room identity + remove */}
|
|
|
|
|
<Box alignItems="Center" gap="200">
|
|
|
|
|
<Avatar size="200" radii="300">
|
|
|
|
|
<RoomAvatar
|
|
|
|
|
roomId={bookmark.roomId}
|
|
|
|
|
src={avatarUrl}
|
|
|
|
|
alt={displayRoomName}
|
|
|
|
|
renderFallback={() => <Text size="H6">{nameInitials(displayRoomName)}</Text>}
|
|
|
|
|
/>
|
|
|
|
|
</Avatar>
|
|
|
|
|
<Box grow="Yes" direction="Column" style={{ minWidth: 0 }}>
|
|
|
|
|
<Text size="T200" truncate style={{ fontWeight: config.fontWeight.W600 }}>
|
|
|
|
|
{displayRoomName}
|
|
|
|
|
</Text>
|
|
|
|
|
<Text size="T200" priority="300">
|
|
|
|
|
{formatTimeAgo(bookmark.savedAt)}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
<IconButton
|
|
|
|
|
size="300"
|
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
|
fill="None"
|
|
|
|
|
radii="300"
|
|
|
|
|
onClick={() => onRemove(bookmark.eventId)}
|
|
|
|
|
aria-label="Remove saved message"
|
2026-06-04 12:07:12 -04:00
|
|
|
>
|
2026-06-19 11:21:29 -04:00
|
|
|
<Icon size="100" src={Icons.Delete} />
|
|
|
|
|
</IconButton>
|
2026-06-04 12:07:12 -04:00
|
|
|
</Box>
|
|
|
|
|
|
2026-06-19 11:21:29 -04:00
|
|
|
{/* Message preview — clicking jumps to the message */}
|
|
|
|
|
<Button
|
|
|
|
|
variant="Secondary"
|
|
|
|
|
fill="Soft"
|
|
|
|
|
size="300"
|
|
|
|
|
radii="300"
|
|
|
|
|
onClick={() => onJump(bookmark.roomId, bookmark.eventId)}
|
|
|
|
|
aria-label="Jump to saved message"
|
|
|
|
|
style={{ justifyContent: 'flex-start', height: 'unset', padding: config.space.S200 }}
|
2026-06-04 10:26:08 -04:00
|
|
|
>
|
2026-06-19 11:21:29 -04:00
|
|
|
<Text className={css.BookmarkPreview} size="T200" priority="400">
|
2026-06-04 12:07:12 -04:00
|
|
|
{bookmark.previewText || '(no preview)'}
|
|
|
|
|
</Text>
|
2026-06-19 11:21:29 -04:00
|
|
|
</Button>
|
2026-06-04 10:26:08 -04:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BookmarksPanelProps = {
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-19 11:21:29 -04:00
|
|
|
export function BookmarksPanel({ onClose }: BookmarksPanelProps) {
|
2026-06-04 10:26:08 -04:00
|
|
|
const { bookmarks, removeBookmark } = useBookmarks();
|
|
|
|
|
const { navigateRoom } = useRoomNavigate();
|
|
|
|
|
const [filter, setFilter] = useState('');
|
|
|
|
|
|
2026-06-19 11:21:29 -04:00
|
|
|
// Escape closes the panel (parity with the app's other overlays/drawers).
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleKeyDown = (evt: KeyboardEvent) => {
|
|
|
|
|
if (evt.key === 'Escape') {
|
|
|
|
|
stopPropagation(evt);
|
|
|
|
|
onClose();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
return () => document.removeEventListener('keydown', handleKeyDown);
|
|
|
|
|
}, [onClose]);
|
|
|
|
|
|
|
|
|
|
const handleJump = useCallback(
|
|
|
|
|
(roomId: string, eventId: string) => {
|
|
|
|
|
navigateRoom(roomId, eventId);
|
|
|
|
|
onClose();
|
|
|
|
|
},
|
|
|
|
|
[navigateRoom, onClose],
|
|
|
|
|
);
|
2026-06-04 10:26:08 -04:00
|
|
|
|
|
|
|
|
const handleFilterChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
setFilter(e.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-19 11:21:29 -04:00
|
|
|
const query = filter.trim().toLowerCase();
|
2026-06-04 10:26:08 -04:00
|
|
|
const filtered: Bookmark[] =
|
2026-06-19 11:21:29 -04:00
|
|
|
query.length === 0
|
2026-06-04 10:26:08 -04:00
|
|
|
? bookmarks
|
2026-06-19 11:21:29 -04:00
|
|
|
: bookmarks.filter(
|
|
|
|
|
(bk) =>
|
|
|
|
|
bk.previewText.toLowerCase().includes(query) ||
|
|
|
|
|
bk.roomName.toLowerCase().includes(query),
|
|
|
|
|
);
|
2026-06-04 10:26:08 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
2026-06-19 11:21:29 -04:00
|
|
|
className={classNames(css.BookmarksPanel, ContainerColor({ variant: 'Background' }))}
|
|
|
|
|
shrink="No"
|
2026-06-04 10:26:08 -04:00
|
|
|
direction="Column"
|
|
|
|
|
>
|
2026-06-19 11:21:29 -04:00
|
|
|
<Header className={css.BookmarksHeader} variant="Background" size="600">
|
2026-06-04 10:26:08 -04:00
|
|
|
<Box grow="Yes" alignItems="Center" gap="200">
|
|
|
|
|
<Icon src={Icons.Star} size="200" />
|
|
|
|
|
<Box grow="Yes">
|
2026-06-19 11:21:29 -04:00
|
|
|
<Text size="H4" truncate>
|
|
|
|
|
Saved Messages
|
|
|
|
|
</Text>
|
2026-06-04 10:26:08 -04:00
|
|
|
</Box>
|
2026-06-19 11:21:29 -04:00
|
|
|
<IconButton size="300" radii="300" aria-label="Close saved messages" onClick={onClose}>
|
|
|
|
|
<Icon src={Icons.Cross} />
|
2026-06-04 10:26:08 -04:00
|
|
|
</IconButton>
|
|
|
|
|
</Box>
|
|
|
|
|
</Header>
|
|
|
|
|
|
2026-06-04 12:07:12 -04:00
|
|
|
{/* Search */}
|
2026-06-19 11:21:29 -04:00
|
|
|
<Box className={css.BookmarksToolbar} direction="Column" gap="100">
|
2026-06-04 10:26:08 -04:00
|
|
|
<Input
|
2026-06-19 11:21:29 -04:00
|
|
|
variant="SurfaceVariant"
|
2026-06-04 12:07:12 -04:00
|
|
|
size="400"
|
2026-06-19 11:21:29 -04:00
|
|
|
radii="300"
|
2026-06-04 12:07:12 -04:00
|
|
|
placeholder="Search saved messages…"
|
2026-06-04 10:26:08 -04:00
|
|
|
value={filter}
|
|
|
|
|
onChange={handleFilterChange}
|
2026-06-19 11:21:29 -04:00
|
|
|
before={<Icon size="100" src={Icons.Search} />}
|
2026-06-04 12:07:12 -04:00
|
|
|
after={
|
|
|
|
|
filter.length > 0 ? (
|
|
|
|
|
<IconButton
|
|
|
|
|
size="300"
|
2026-06-19 11:21:29 -04:00
|
|
|
variant="SurfaceVariant"
|
|
|
|
|
fill="None"
|
2026-06-04 12:07:12 -04:00
|
|
|
radii="300"
|
|
|
|
|
aria-label="Clear search"
|
|
|
|
|
onClick={() => setFilter('')}
|
|
|
|
|
>
|
|
|
|
|
<Icon size="100" src={Icons.Cross} />
|
|
|
|
|
</IconButton>
|
|
|
|
|
) : undefined
|
|
|
|
|
}
|
2026-06-04 10:26:08 -04:00
|
|
|
/>
|
2026-06-19 11:21:29 -04:00
|
|
|
{bookmarks.length > 0 && (
|
|
|
|
|
<Text size="T200" priority="300">
|
2026-06-04 12:07:12 -04:00
|
|
|
{filtered.length === bookmarks.length
|
|
|
|
|
? `${bookmarks.length} saved message${bookmarks.length !== 1 ? 's' : ''}`
|
|
|
|
|
: `${filtered.length} of ${bookmarks.length} messages`}
|
|
|
|
|
</Text>
|
2026-06-19 11:21:29 -04:00
|
|
|
)}
|
|
|
|
|
</Box>
|
2026-06-04 12:07:12 -04:00
|
|
|
|
|
|
|
|
{/* List */}
|
2026-06-19 11:21:29 -04:00
|
|
|
<Box grow="Yes" style={{ minHeight: 0 }}>
|
|
|
|
|
<Scroll variant="Background" size="300" visibility="Hover" hideTrack>
|
|
|
|
|
{filtered.length === 0 ? (
|
|
|
|
|
<Box
|
|
|
|
|
direction="Column"
|
|
|
|
|
alignItems="Center"
|
|
|
|
|
justifyContent="Center"
|
|
|
|
|
gap="300"
|
|
|
|
|
style={{ padding: config.space.S700, textAlign: 'center' }}
|
|
|
|
|
>
|
|
|
|
|
<Icon size="600" src={Icons.Star} style={{ opacity: config.opacity.Disabled }} />
|
|
|
|
|
<Text size="T300" priority="300" align="Center">
|
|
|
|
|
{bookmarks.length === 0
|
|
|
|
|
? 'No saved messages yet. Right-click any message to save it.'
|
|
|
|
|
: 'No saved messages match your search.'}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
) : (
|
|
|
|
|
<Box className={css.BookmarksContent} direction="Column" gap="200">
|
|
|
|
|
{filtered.map((bk) => (
|
|
|
|
|
<BookmarkItem
|
|
|
|
|
key={bk.eventId}
|
|
|
|
|
bookmark={bk}
|
|
|
|
|
onJump={handleJump}
|
|
|
|
|
onRemove={removeBookmark}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Scroll>
|
|
|
|
|
</Box>
|
2026-06-04 10:26:08 -04:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|