diff --git a/package.json b/package.json index cd149dbc7..d996d6ee9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/components/AccountDataEditor.tsx b/src/app/components/AccountDataEditor.tsx index 84d46163b..9798d5855 100644 --- a/src/app/components/AccountDataEditor.tsx +++ b/src/app/components/AccountDataEditor.tsx @@ -26,27 +26,30 @@ import { useTextAreaCodeEditor } from '../hooks/useTextAreaCodeEditor'; const EDITOR_INTENT_SPACE_COUNT = 2; -export type AccountDataSubmitCallback = (type: string, content: object) => Promise; +export type AccountDataSubmitCallback = ( + type: T, + content: object +) => Promise; -type AccountDataInfo = { - type: string; +type AccountDataInfo = { + type: T; content: object; }; -type AccountDataEditProps = { - type: string; +type AccountDataEditProps = { + type: T | ''; defaultContent: string; - submitChange: AccountDataSubmitCallback; + submitChange: AccountDataSubmitCallback; onCancel: () => void; - onSave: (info: AccountDataInfo) => void; + onSave: (info: AccountDataInfo) => void; }; -function AccountDataEdit({ +function AccountDataEdit({ type, defaultContent, submitChange, onCancel, onSave, -}: AccountDataEditProps) { +}: AccountDataEditProps) { const alive = useAlive(); const textAreaRef = useRef(null); @@ -57,7 +60,7 @@ function AccountDataEdit({ EDITOR_INTENT_SPACE_COUNT ); - const [submitState, submit] = useAsyncCallback(submitChange); + const [submitState, submit] = useAsyncCallback(submitChange); const submitting = submitState.status === AsyncStatus.Loading; const handleSubmit: FormEventHandler = (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 = { + type: T; defaultContent: string; onEdit: () => void; }; -function AccountDataView({ type, defaultContent, onEdit }: AccountDataViewProps) { +function AccountDataView({ + type, + defaultContent, + onEdit, +}: AccountDataViewProps) { return ( = { + type?: T; content?: object; - submitChange: AccountDataSubmitCallback; + submitChange: AccountDataSubmitCallback; requestClose: () => void; }; -export function AccountDataEditor({ +export function AccountDataEditor({ type, content, submitChange, requestClose, -}: AccountDataEditorProps) { - const [data, setData] = useState({ - type: type ?? '', +}: AccountDataEditorProps) { + const [data, setData] = useState>({ + 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) => { setData(info); setEdit(false); }, []); diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 94ec7aeb4..0cee57c99 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -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, '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, }, }; diff --git a/src/app/features/settings/developer-tools/AccountData.tsx b/src/app/features/settings/developer-tools/AccountData.tsx index 8bccb62ec..edb8a0a1b 100644 --- a/src/app/features/settings/developer-tools/AccountData.tsx +++ b/src/app/features/settings/developer-tools/AccountData.tsx @@ -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 | 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> + >( + () => Array.from(mx.store.accountData.keys()) as Array> ); useAccountDataCallback( mx, useCallback(() => { - setAccountDataKeys(Array.from(mx.store.accountData.keys())); + setAccountDataKeys( + Array.from(mx.store.accountData.keys()) as Array> + ); }, [mx]) ); diff --git a/src/app/features/settings/developer-tools/DevelopTools.tsx b/src/app/features/settings/developer-tools/DevelopTools.tsx index a3f04567b..e9fd7e56f 100644 --- a/src/app/features/settings/developer-tools/DevelopTools.tsx +++ b/src/app/features/settings/developer-tools/DevelopTools.tsx @@ -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; + 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(); + const [accountDataType, setAccountDataType] = useState(); - const submitAccountData: AccountDataSubmitCallback = useCallback( + const submitAccountData: AccountDataSubmitCallback = useCallback( async (type, content) => { await mx.setAccountData(type, content); }, diff --git a/src/app/hooks/useAccountData.ts b/src/app/hooks/useAccountData.ts index 30e47b475..11d3d5b42 100644 --- a/src/app/hooks/useAccountData.ts +++ b/src/app/hooks/useAccountData.ts @@ -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, diff --git a/src/app/utils/notifications.ts b/src/app/utils/notifications.ts index c43e830d9..61002a087 100644 --- a/src/app/utils/notifications.ts +++ b/src/app/utils/notifications.ts @@ -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); + } } diff --git a/src/types/matrix-sdk-events.d.ts b/src/types/matrix-sdk-events.d.ts new file mode 100644 index 000000000..4da19b45b --- /dev/null +++ b/src/types/matrix-sdk-events.d.ts @@ -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; + } + + interface AccountDataEvents { + 'in.cinny.spaces': InCinnySpacesContent; + 'io.element.recent_emoji': IRecentEmojiContent; + 'im.ponies.user_emotes': PackContent; + 'im.ponies.emote_rooms': EmoteRoomsContent; + } +} diff --git a/src/types/matrix-sdk.ts b/src/types/matrix-sdk.ts index 3d1ec6fb2..3097f2197 100644 --- a/src/types/matrix-sdk.ts +++ b/src/types/matrix-sdk.ts @@ -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'; diff --git a/tsconfig.json b/tsconfig.json index 1a5842a01..c461119b3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,9 @@ "paths": { "$types/*": ["src/types/*"] }, - "moduleResolution": "Node", + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "noEmit": true, "resolveJsonModule": true, "outDir": "dist", "skipLibCheck": true,