mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
fix(types): align account-data and message edit payload typing
Adopt matrix event-map augmentation and Element-style typed boundaries for account data, and build message edit content with explicit replacement event types. Also make read receipt and fully_read marker sends independent with explicit failure handling.
This commit is contained in:
10 files changed
+117
-49
No files matched your search
+1
-1
@@ -14,7 +14,7 @@
|
||||
"check:eslint": "eslint src/*",
|
||||
"check:prettier": "prettier --check .",
|
||||
"fix:prettier": "prettier --write .",
|
||||
"typecheck": "tsc --noEmit"
|
||||
"typecheck": "tsc"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Ajay Bura",
|
||||
|
||||
@@ -26,27 +26,30 @@ import { useTextAreaCodeEditor } from '../hooks/useTextAreaCodeEditor';
|
||||
|
||||
const EDITOR_INTENT_SPACE_COUNT = 2;
|
||||
|
||||
export type AccountDataSubmitCallback = (type: string, content: object) => Promise<void>;
|
||||
export type AccountDataSubmitCallback<T extends string = string> = (
|
||||
type: T,
|
||||
content: object
|
||||
) => Promise<void>;
|
||||
|
||||
type AccountDataInfo = {
|
||||
type: string;
|
||||
type AccountDataInfo<T extends string = string> = {
|
||||
type: T;
|
||||
content: object;
|
||||
};
|
||||
|
||||
type AccountDataEditProps = {
|
||||
type: string;
|
||||
type AccountDataEditProps<T extends string = string> = {
|
||||
type: T | '';
|
||||
defaultContent: string;
|
||||
submitChange: AccountDataSubmitCallback;
|
||||
submitChange: AccountDataSubmitCallback<T>;
|
||||
onCancel: () => void;
|
||||
onSave: (info: AccountDataInfo) => void;
|
||||
onSave: (info: AccountDataInfo<T>) => void;
|
||||
};
|
||||
function AccountDataEdit({
|
||||
function AccountDataEdit<T extends string = string>({
|
||||
type,
|
||||
defaultContent,
|
||||
submitChange,
|
||||
onCancel,
|
||||
onSave,
|
||||
}: AccountDataEditProps) {
|
||||
}: AccountDataEditProps<T>) {
|
||||
const alive = useAlive();
|
||||
|
||||
const textAreaRef = useRef<HTMLTextAreaElement>(null);
|
||||
@@ -57,7 +60,7 @@ function AccountDataEdit({
|
||||
EDITOR_INTENT_SPACE_COUNT
|
||||
);
|
||||
|
||||
const [submitState, submit] = useAsyncCallback<void, MatrixError, [string, object]>(submitChange);
|
||||
const [submitState, submit] = useAsyncCallback<void, MatrixError, [T, object]>(submitChange);
|
||||
const submitting = submitState.status === AsyncStatus.Loading;
|
||||
|
||||
const handleSubmit: FormEventHandler<HTMLFormElement> = (evt) => {
|
||||
@@ -89,10 +92,11 @@ function AccountDataEdit({
|
||||
return;
|
||||
}
|
||||
|
||||
submit(typeStr, parsedContent).then(() => {
|
||||
const eventType = typeStr as T;
|
||||
submit(eventType, parsedContent).then(() => {
|
||||
if (alive()) {
|
||||
onSave({
|
||||
type: typeStr,
|
||||
type: eventType,
|
||||
content: parsedContent,
|
||||
});
|
||||
}
|
||||
@@ -192,12 +196,16 @@ function AccountDataEdit({
|
||||
);
|
||||
}
|
||||
|
||||
type AccountDataViewProps = {
|
||||
type: string;
|
||||
type AccountDataViewProps<T extends string = string> = {
|
||||
type: T;
|
||||
defaultContent: string;
|
||||
onEdit: () => void;
|
||||
};
|
||||
function AccountDataView({ type, defaultContent, onEdit }: AccountDataViewProps) {
|
||||
function AccountDataView<T extends string = string>({
|
||||
type,
|
||||
defaultContent,
|
||||
onEdit,
|
||||
}: AccountDataViewProps<T>) {
|
||||
return (
|
||||
<Box
|
||||
direction="Column"
|
||||
@@ -241,21 +249,21 @@ function AccountDataView({ type, defaultContent, onEdit }: AccountDataViewProps)
|
||||
);
|
||||
}
|
||||
|
||||
export type AccountDataEditorProps = {
|
||||
type?: string;
|
||||
export type AccountDataEditorProps<T extends string = string> = {
|
||||
type?: T;
|
||||
content?: object;
|
||||
submitChange: AccountDataSubmitCallback;
|
||||
submitChange: AccountDataSubmitCallback<T>;
|
||||
requestClose: () => void;
|
||||
};
|
||||
|
||||
export function AccountDataEditor({
|
||||
export function AccountDataEditor<T extends string = string>({
|
||||
type,
|
||||
content,
|
||||
submitChange,
|
||||
requestClose,
|
||||
}: AccountDataEditorProps) {
|
||||
const [data, setData] = useState<AccountDataInfo>({
|
||||
type: type ?? '',
|
||||
}: AccountDataEditorProps<T>) {
|
||||
const [data, setData] = useState<AccountDataInfo<T>>({
|
||||
type: (type ?? '') as T,
|
||||
content: content ?? {},
|
||||
});
|
||||
|
||||
@@ -269,7 +277,7 @@ export function AccountDataEditor({
|
||||
setEdit(false);
|
||||
}, [type, requestClose]);
|
||||
|
||||
const handleSave = useCallback((info: AccountDataInfo) => {
|
||||
const handleSave = useCallback((info: AccountDataInfo<T>) => {
|
||||
setData(info);
|
||||
setEdit(false);
|
||||
}, []);
|
||||
|
||||
@@ -21,7 +21,16 @@ import {
|
||||
} from 'folds';
|
||||
import { Editor, Transforms } from 'slate';
|
||||
import { ReactEditor } from 'slate-react';
|
||||
import { IContent, IMentions, MatrixEvent, RelationType, Room } from '$types/matrix-sdk';
|
||||
import {
|
||||
IContent,
|
||||
IMentions,
|
||||
MatrixEvent,
|
||||
ReplacementEvent,
|
||||
RelationType,
|
||||
Room,
|
||||
RoomMessageEventContent,
|
||||
RoomMessageTextEventContent,
|
||||
} from '$types/matrix-sdk';
|
||||
import { isKeyHotkey } from 'is-hotkey';
|
||||
import {
|
||||
AUTOCOMPLETE_PREFIXES,
|
||||
@@ -80,7 +89,8 @@ export const MessageEditor = as<'div', MessageEditorProps>(
|
||||
string | undefined,
|
||||
IMentions | undefined
|
||||
] => {
|
||||
const evtId = mEvent.getId()!;
|
||||
const evtId = mEvent.getId();
|
||||
if (!evtId) return [undefined, undefined, undefined];
|
||||
const evtTimeline = room.getTimelineForEvent(evtId);
|
||||
const editedEvent =
|
||||
evtTimeline && getEditedEvent(evtId, mEvent, evtTimeline.getTimelineSet());
|
||||
@@ -111,6 +121,9 @@ export const MessageEditor = as<'div', MessageEditorProps>(
|
||||
const [prevBody, prevCustomHtml, prevMentions] = getPrevBodyAndFormattedBody();
|
||||
|
||||
if (plainText === '') return undefined;
|
||||
const eventId = mEvent.getId();
|
||||
if (!eventId) return undefined;
|
||||
|
||||
if (prevBody) {
|
||||
if (prevCustomHtml && trimReplyFromFormattedBody(prevCustomHtml) === customHtml) {
|
||||
return undefined;
|
||||
@@ -124,11 +137,20 @@ export const MessageEditor = as<'div', MessageEditorProps>(
|
||||
}
|
||||
}
|
||||
|
||||
const newContent: IContent = {
|
||||
msgtype: mEvent.getContent().msgtype,
|
||||
const msgtype = mEvent.getContent().msgtype as RoomMessageTextEventContent['msgtype'];
|
||||
|
||||
const newContent: RoomMessageTextEventContent = {
|
||||
msgtype,
|
||||
body: plainText,
|
||||
};
|
||||
|
||||
const contentBody: RoomMessageTextEventContent &
|
||||
Omit<ReplacementEvent<RoomMessageEventContent>, 'm.relates_to'> = {
|
||||
msgtype,
|
||||
body: `* ${plainText}`,
|
||||
'm.new_content': newContent,
|
||||
};
|
||||
|
||||
const mentionData = getMentions(mx, roomId, editor);
|
||||
|
||||
prevMentions?.user_ids?.forEach((prevMentionId) => {
|
||||
@@ -137,18 +159,19 @@ export const MessageEditor = as<'div', MessageEditorProps>(
|
||||
|
||||
const mMentions = getMentionContent(Array.from(mentionData.users), mentionData.room);
|
||||
newContent['m.mentions'] = mMentions;
|
||||
contentBody['m.mentions'] = mMentions;
|
||||
|
||||
if (!customHtmlEqualsPlainText(customHtml, plainText)) {
|
||||
newContent.format = 'org.matrix.custom.html';
|
||||
newContent.formatted_body = customHtml;
|
||||
contentBody.format = 'org.matrix.custom.html';
|
||||
contentBody.formatted_body = `* ${customHtml}`;
|
||||
}
|
||||
|
||||
const content: IContent = {
|
||||
...newContent,
|
||||
body: `* ${plainText}`,
|
||||
'm.new_content': newContent,
|
||||
const content: RoomMessageEventContent = {
|
||||
...contentBody,
|
||||
'm.relates_to': {
|
||||
event_id: mEvent.getId(),
|
||||
event_id: eventId,
|
||||
rel_type: RelationType.Replace,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import { Box, Text, Icon, Icons, Button, MenuItem } from 'folds';
|
||||
import { AccountDataEvents } from '$types/matrix-sdk';
|
||||
import { SequenceCard } from '../../../components/sequence-card';
|
||||
import { SequenceCardStyle } from '../styles.css';
|
||||
import { SettingTile } from '../../../components/setting-tile';
|
||||
@@ -10,18 +11,22 @@ import { CutoutCard } from '../../../components/cutout-card';
|
||||
type AccountDataProps = {
|
||||
expand: boolean;
|
||||
onExpandToggle: (expand: boolean) => void;
|
||||
onSelect: (type: string | null) => void;
|
||||
onSelect: (type: Extract<keyof AccountDataEvents, string> | null) => void;
|
||||
};
|
||||
export function AccountData({ expand, onExpandToggle, onSelect }: AccountDataProps) {
|
||||
const mx = useMatrixClient();
|
||||
const [accountDataTypes, setAccountDataKeys] = useState(() =>
|
||||
Array.from(mx.store.accountData.keys())
|
||||
const [accountDataTypes, setAccountDataKeys] = useState<
|
||||
Array<Extract<keyof AccountDataEvents, string>>
|
||||
>(
|
||||
() => Array.from(mx.store.accountData.keys()) as Array<Extract<keyof AccountDataEvents, string>>
|
||||
);
|
||||
|
||||
useAccountDataCallback(
|
||||
mx,
|
||||
useCallback(() => {
|
||||
setAccountDataKeys(Array.from(mx.store.accountData.keys()));
|
||||
setAccountDataKeys(
|
||||
Array.from(mx.store.accountData.keys()) as Array<Extract<keyof AccountDataEvents, string>>
|
||||
);
|
||||
}, [mx])
|
||||
);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { SettingTile } from '../../../components/setting-tile';
|
||||
import { useSetting } from '../../../state/hooks/settings';
|
||||
import { settingsAtom } from '../../../state/settings';
|
||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||
import { AccountDataEvents } from '$types/matrix-sdk';
|
||||
import {
|
||||
AccountDataEditor,
|
||||
AccountDataSubmitCallback,
|
||||
@@ -14,6 +15,8 @@ import {
|
||||
import { copyToClipboard } from '../../../utils/dom';
|
||||
import { AccountData } from './AccountData';
|
||||
|
||||
type AccountDataEventType = Extract<keyof AccountDataEvents, string>;
|
||||
|
||||
type DeveloperToolsProps = {
|
||||
requestClose: () => void;
|
||||
};
|
||||
@@ -21,9 +24,9 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) {
|
||||
const mx = useMatrixClient();
|
||||
const [developerTools, setDeveloperTools] = useSetting(settingsAtom, 'developerTools');
|
||||
const [expand, setExpend] = useState(false);
|
||||
const [accountDataType, setAccountDataType] = useState<string | null>();
|
||||
const [accountDataType, setAccountDataType] = useState<AccountDataEventType | null>();
|
||||
|
||||
const submitAccountData: AccountDataSubmitCallback = useCallback(
|
||||
const submitAccountData: AccountDataSubmitCallback<AccountDataEventType> = useCallback(
|
||||
async (type, content) => {
|
||||
await mx.setAccountData(type, content);
|
||||
},
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { AccountDataEvents } from '$types/matrix-sdk';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
import { useAccountDataCallback } from './useAccountDataCallback';
|
||||
|
||||
export function useAccountData(eventType: string) {
|
||||
const mx = useMatrixClient();
|
||||
const [event, setEvent] = useState(() => mx.getAccountData(eventType));
|
||||
const [event, setEvent] = useState(() => mx.getAccountData(eventType as keyof AccountDataEvents));
|
||||
|
||||
useAccountDataCallback(
|
||||
mx,
|
||||
|
||||
@@ -22,12 +22,19 @@ export async function markAsRead(mx: MatrixClient, roomId: string, privateReceip
|
||||
const latestEventId = latestEvent.getId();
|
||||
if (!latestEventId) return;
|
||||
|
||||
// Set both the read receipt AND the fully_read marker
|
||||
// The fully_read marker is what persists your read position across sessions
|
||||
await mx.setRoomReadMarkers(
|
||||
roomId,
|
||||
latestEventId, // m.fully_read marker
|
||||
latestEvent, // m.read receipt event
|
||||
privateReceipt ? { receiptType: ReceiptType.ReadPrivate } : undefined
|
||||
);
|
||||
// Match Element Web behavior: send receipt and fully_read marker separately.
|
||||
// Keep them independent so a failure in one does not block the other.
|
||||
const receiptType = privateReceipt ? ReceiptType.ReadPrivate : undefined;
|
||||
|
||||
const [receiptResult, markerResult] = await Promise.allSettled([
|
||||
mx.sendReadReceipt(latestEvent, receiptType),
|
||||
mx.setRoomReadMarkers(roomId, latestEventId),
|
||||
]);
|
||||
|
||||
if (receiptResult.status === 'rejected') {
|
||||
console.error('Failed to send read receipt', receiptResult.reason);
|
||||
}
|
||||
if (markerResult.status === 'rejected') {
|
||||
console.error('Failed to send fully_read marker', markerResult.reason);
|
||||
}
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import type { PackContent } from '../app/plugins/custom-emoji/types';
|
||||
import type { EmoteRoomsContent } from '../app/plugins/custom-emoji/types';
|
||||
import type { IRecentEmojiContent } from '../app/plugins/recent-emoji';
|
||||
import type { InCinnySpacesContent } from '../app/hooks/useSidebarItems';
|
||||
|
||||
declare module 'matrix-js-sdk/lib/@types/event' {
|
||||
interface StateEvents {
|
||||
'im.ponies.room_emotes': PackContent;
|
||||
'in.cinny.room.power_level_tags': Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface AccountDataEvents {
|
||||
'in.cinny.spaces': InCinnySpacesContent;
|
||||
'io.element.recent_emoji': IRecentEmojiContent;
|
||||
'im.ponies.user_emotes': PackContent;
|
||||
'im.ponies.emote_rooms': EmoteRoomsContent;
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ export * from 'matrix-js-sdk/lib/crypto-api/index';
|
||||
export * from 'matrix-js-sdk/lib/@types/common';
|
||||
export * from 'matrix-js-sdk/lib/@types/uia';
|
||||
export * from 'matrix-js-sdk/lib/@types/event';
|
||||
export * from 'matrix-js-sdk/lib/@types/events';
|
||||
export * from 'matrix-js-sdk/lib/@types/PushRules';
|
||||
export * from 'matrix-js-sdk/lib/@types/partials';
|
||||
export * from 'matrix-js-sdk/lib/@types/requests';
|
||||
|
||||
+3
-1
@@ -11,7 +11,9 @@
|
||||
"paths": {
|
||||
"$types/*": ["src/types/*"]
|
||||
},
|
||||
"moduleResolution": "Node",
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"noEmit": true,
|
||||
"resolveJsonModule": true,
|
||||
"outDir": "dist",
|
||||
"skipLibCheck": true,
|
||||
|
||||
Reference in new issue
Block a user