Adds unread pins as described in #25
This commit is contained in:
1 parent
ba197aee54
commit
a18e3d534c
4 files changed
+98
-12
No files matched your search
@@ -5,6 +5,7 @@
|
||||
- Codebase cleanup ([#22](https://github.com/7w1/sable/pull/22))
|
||||
- Fix mono font ([#18](https://github.com/7w1/sable/pull/18))
|
||||
- Merge final commits from ([cinnyapp#2599](https://github.com/cinnyapp/cinny/pull/2599))
|
||||
- Pin counter tracks unread pins
|
||||
|
||||
## 1.1.7
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { MouseEventHandler, forwardRef, useState } from 'react';
|
||||
import React, { MouseEventHandler, forwardRef, useEffect, useState } from 'react';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import {
|
||||
Box,
|
||||
@@ -47,7 +47,6 @@ import { roomToUnreadAtom } from '$state/room/roomToUnread';
|
||||
import { copyToClipboard } from '$appUtils/dom';
|
||||
import { LeaveRoomPrompt } from '$components/leave-room-prompt';
|
||||
import { useRoomAvatar, useRoomName, useRoomTopic } from '$hooks/useRoomMeta';
|
||||
import { mDirectAtom } from '$state/mDirectList';
|
||||
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
|
||||
import { stopPropagation } from '$appUtils/keyboard';
|
||||
import { getMatrixToRoom } from '$plugins/matrix-to';
|
||||
@@ -71,6 +70,17 @@ import { InviteUserPrompt } from '$components/invite-user-prompt';
|
||||
import { useCallState } from '$pages/client/call/CallProvider';
|
||||
import { ContainerColor } from '$styles/ContainerColor.css';
|
||||
import { useRoomWidgets } from '$hooks/useRoomWidgets';
|
||||
import { AccountDataEvent } from '$types/matrix/accountData';
|
||||
|
||||
async function getPinsHash(pinnedIds: string[]): Promise<string> {
|
||||
const sorted = [...pinnedIds].sort().join(',');
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(sorted);
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
return hashHex.slice(0, 10);
|
||||
}
|
||||
|
||||
type RoomMenuProps = {
|
||||
room: Room;
|
||||
@@ -268,7 +278,6 @@ export function RoomViewHeader() {
|
||||
const direct = useIsDirectRoom();
|
||||
|
||||
const { isChatOpen, toggleChat } = useCallState();
|
||||
const pinnedEvents = useRoomPinnedEvents(room);
|
||||
const encryptionEvent = useStateEvent(room, StateEvent.RoomEncryption);
|
||||
const encryptedRoom = !!encryptionEvent;
|
||||
const avatarMxc = useRoomAvatar(room, direct);
|
||||
@@ -282,6 +291,42 @@ export function RoomViewHeader() {
|
||||
const [widgetDrawer, setWidgetDrawer] = useSetting(settingsAtom, 'isWidgetDrawer');
|
||||
const widgets = useRoomWidgets(room);
|
||||
|
||||
const pinnedIds = useRoomPinnedEvents(room);
|
||||
const pinMarker = room.getAccountData(AccountDataEvent.SablePinStatus)?.getContent();
|
||||
const [unreadPinsCount, setUnreadPinsCount] = useState(0);
|
||||
|
||||
const [currentHash, setCurrentHash] = useState<string>('');
|
||||
|
||||
useEffect(() => {
|
||||
getPinsHash(pinnedIds).then(setCurrentHash);
|
||||
}, [pinnedIds]);
|
||||
|
||||
useEffect(() => {
|
||||
const checkUnreads = async () => {
|
||||
if (!pinnedIds.length) {
|
||||
setUnreadPinsCount(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentHash = await getPinsHash(pinnedIds);
|
||||
|
||||
if (pinMarker?.hash === currentHash) {
|
||||
setUnreadPinsCount(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const lastSeenIndex = pinnedIds.indexOf(pinMarker?.last_seen_id);
|
||||
if (lastSeenIndex !== -1) {
|
||||
const newPins = pinnedIds.slice(lastSeenIndex + 1);
|
||||
setUnreadPinsCount(newPins.length);
|
||||
} else {
|
||||
const countDiff = pinnedIds.length - (pinMarker?.count ?? 0);
|
||||
setUnreadPinsCount(countDiff > 0 ? countDiff : 0);
|
||||
}
|
||||
};
|
||||
checkUnreads();
|
||||
}, [pinnedIds, pinMarker]);
|
||||
|
||||
const handleSearchClick = () => {
|
||||
const searchParams: _SearchPathSearchParams = {
|
||||
rooms: room.roomId,
|
||||
@@ -296,10 +341,18 @@ export function RoomViewHeader() {
|
||||
setMenuAnchor(evt.currentTarget.getBoundingClientRect());
|
||||
};
|
||||
|
||||
const handleOpenPinMenu: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
||||
const handleOpenPinMenu: MouseEventHandler<HTMLButtonElement> = async (evt) => {
|
||||
setPinMenuAnchor(evt.currentTarget.getBoundingClientRect());
|
||||
|
||||
const currentHash = await getPinsHash(pinnedIds);
|
||||
mx.setRoomAccountData(room.roomId, AccountDataEvent.SablePinStatus, {
|
||||
hash: currentHash,
|
||||
count: pinnedIds.length,
|
||||
last_seen_id: pinnedIds[pinnedIds.length - 1],
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<PageHeader
|
||||
className={ContainerColor({ variant: 'Surface' })}
|
||||
@@ -411,7 +464,7 @@ export function RoomViewHeader() {
|
||||
ref={triggerRef}
|
||||
aria-pressed={!!pinMenuAnchor}
|
||||
>
|
||||
{pinnedEvents.length > 0 && (
|
||||
{unreadPinsCount > 0 && (
|
||||
<Badge
|
||||
style={{
|
||||
position: 'absolute',
|
||||
@@ -424,7 +477,7 @@ export function RoomViewHeader() {
|
||||
radii="Pill"
|
||||
>
|
||||
<Text as="span" size="L400">
|
||||
{pinnedEvents.length}
|
||||
{unreadPinsCount}
|
||||
</Text>
|
||||
</Badge>
|
||||
)}
|
||||
@@ -447,7 +500,11 @@ export function RoomViewHeader() {
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<RoomPinMenu room={room} requestClose={() => setPinMenuAnchor(undefined)} />
|
||||
<RoomPinMenu
|
||||
room={room}
|
||||
requestClose={() => setPinMenuAnchor(undefined)}
|
||||
currentHash={currentHash}
|
||||
/>
|
||||
</FocusTrap>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -88,6 +88,7 @@ import {
|
||||
} from '$hooks/useMemberPowerTag';
|
||||
import { useRoomCreatorsTag } from '$hooks/useRoomCreatorsTag';
|
||||
import { nicknamesAtom } from '$state/nicknames';
|
||||
import { AccountDataEvent } from '$types/matrix/accountData';
|
||||
|
||||
type PinnedMessageProps = {
|
||||
room: Room;
|
||||
@@ -100,6 +101,7 @@ type PinnedMessageProps = {
|
||||
legacyUsernameColor: boolean;
|
||||
hour24Clock: boolean;
|
||||
dateFormatString: string;
|
||||
isNew: boolean;
|
||||
};
|
||||
function PinnedMessage({
|
||||
room,
|
||||
@@ -112,6 +114,7 @@ function PinnedMessage({
|
||||
legacyUsernameColor,
|
||||
hour24Clock,
|
||||
dateFormatString,
|
||||
isNew,
|
||||
}: PinnedMessageProps) {
|
||||
const pinnedEvent = useRoomEvent(room, eventId);
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
@@ -144,13 +147,19 @@ function PinnedMessage({
|
||||
|
||||
const renderOptions = () => (
|
||||
<Box shrink="No" gap="200" alignItems="Center">
|
||||
<Chip data-event-id={eventId} onClick={handleOpenClick} variant="Secondary" radii="Pill">
|
||||
<Chip
|
||||
data-event-id={eventId}
|
||||
onClick={handleOpenClick}
|
||||
// Maybe change colors later. design not finalized
|
||||
//variant={isNew ? "Primary" : "Secondary"}
|
||||
radii="Pill"
|
||||
>
|
||||
<Text size="T200">Jump</Text>
|
||||
</Chip>
|
||||
{canPinEvent && (
|
||||
<IconButton
|
||||
data-event-id={eventId}
|
||||
variant="Secondary"
|
||||
//variant={isNew ? "Primary" : "Secondary"}
|
||||
size="300"
|
||||
radii="Pill"
|
||||
onClick={unpinState.status === AsyncStatus.Loading ? undefined : handleUnpinClick}
|
||||
@@ -247,9 +256,10 @@ function PinnedMessage({
|
||||
type RoomPinMenuProps = {
|
||||
room: Room;
|
||||
requestClose: () => void;
|
||||
currentHash: string;
|
||||
};
|
||||
export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
({ room, requestClose }, ref) => {
|
||||
({ room, requestClose, currentHash }, ref) => {
|
||||
const mx = useMatrixClient();
|
||||
const userId = mx.getUserId()!;
|
||||
const nicknames = useAtomValue(nicknamesAtom);
|
||||
@@ -285,6 +295,15 @@ export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
const { navigateRoom } = useRoomNavigate();
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const pinMarker = useMemo(() =>
|
||||
room.getAccountData(AccountDataEvent.SablePinStatus)?.getContent(),
|
||||
[room]);
|
||||
const lastSeenIndex = useMemo(() => {
|
||||
if (!pinMarker?.last_seen_id) return -1;
|
||||
return pinnedEvents.indexOf(pinMarker.last_seen_id);
|
||||
}, [pinnedEvents, pinMarker]);
|
||||
const hasNewContent = pinMarker?.hash !== currentHash;
|
||||
|
||||
const virtualizer = useVirtualizer({
|
||||
count: sortedPinnedEvent.length,
|
||||
getScrollElement: () => scrollRef.current,
|
||||
@@ -493,6 +512,9 @@ export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
const eventId = sortedPinnedEvent[vItem.index];
|
||||
if (!eventId) return null;
|
||||
|
||||
const originalIndex = pinnedEvents.indexOf(eventId);
|
||||
const isNew = hasNewContent && (lastSeenIndex === -1 || originalIndex > lastSeenIndex);
|
||||
|
||||
return (
|
||||
<VirtualTile
|
||||
virtualItem={vItem}
|
||||
@@ -501,8 +523,12 @@ export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
key={vItem.index}
|
||||
>
|
||||
<SequenceCard
|
||||
style={{ padding: config.space.S400, borderRadius: config.radii.R300 }}
|
||||
variant="SurfaceVariant"
|
||||
style={{
|
||||
padding: config.space.S400,
|
||||
borderRadius: config.radii.R300,
|
||||
border: isNew ? `${config.borderWidth.B700} solid ${color.Secondary.ContainerActive}` : undefined,
|
||||
}}
|
||||
variant={"Background"}
|
||||
direction="Column"
|
||||
>
|
||||
<PinnedMessage
|
||||
@@ -515,6 +541,7 @@ export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
accessibleTagColors={accessibleTagColors}
|
||||
legacyUsernameColor={legacyUsernameColor || direct}
|
||||
hour24Clock={hour24Clock}
|
||||
isNew={isNew}
|
||||
dateFormatString={dateFormatString}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
@@ -20,6 +20,7 @@ export enum AccountDataEvent {
|
||||
|
||||
// Sable account data
|
||||
SableNicknames = 'moe.sable.app.nicknames',
|
||||
SablePinStatus = 'moe.sable.app.pins_read_marker',
|
||||
}
|
||||
|
||||
export type MDirectContent = Record<string, string[]>;
|
||||
|
||||
Reference in new issue
Block a user