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

chat: properly clear refs #2957

Merged
merged 2 commits into from
Oct 17, 2023
Merged
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
10 changes: 9 additions & 1 deletion ui/src/chat/ChatInput/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
useShipHasBlockedUs,
useUnblockShipMutation,
} from '@/state/chat';
import { ChatImage, ChatMemo, Cite } from '@/types/chat';
import { ChatBlock, ChatImage, ChatMemo, Cite } from '@/types/chat';
import MessageEditor, {
HandlerParams,
useMessageEditor,
Expand All @@ -26,6 +26,7 @@ import Avatar from '@/components/Avatar';
import ShipName from '@/components/ShipName';
import X16Icon from '@/components/icons/X16Icon';
import {
chatStoreLogger,
fetchChatBlocks,
useChatInfo,
useChatStore,
Expand Down Expand Up @@ -122,6 +123,7 @@ export default function ChatInput({
[targetId, dropZoneId]
);
const id = replying ? `${whom}-${replying}` : whom;
chatStoreLogger.log('InputRender', id);
const [draft, setDraft] = useLocalStorage(
createStorageKey(`chat-${id}`),
inlinesToJSON([''])
Expand Down Expand Up @@ -199,6 +201,7 @@ export default function ChatInput({
}, [mostRecentFile]);

const clearAttachments = useCallback(() => {
chatStoreLogger.log('clearAttachments', { id, uploadKey });
useChatStore.getState().setBlocks(id, []);
useFileStore.getState().getUploader(uploadKey)?.clear();
if (replyCite) {
Expand All @@ -225,6 +228,7 @@ export default function ChatInput({
const uploadType = useFileStore.getState().getUploadType(uploadKey);

if (isTargetId && uploadType === 'drag' && didDrop) {
chatStoreLogger.log('DragUpload', { id, files });
// TODO: handle existing blocks (other refs)
useChatStore.getState().setBlocks(
id,
Expand All @@ -241,6 +245,7 @@ export default function ChatInput({
}

if (uploadType !== 'drag') {
chatStoreLogger.log('Upload', { id, files });
// TODO: handle existing blocks (other refs)
useChatStore.getState().setBlocks(
id,
Expand Down Expand Up @@ -508,6 +513,7 @@ export default function ChatInput({
return;
}
setBlocks(id, [{ cite }]);
chatStoreLogger.log('AndroidPaste', { id, cite });
messageEditor.commands.deleteRange({
from: editorText.indexOf(path),
to: editorText.indexOf(path) + path.length + 1,
Expand Down Expand Up @@ -550,6 +556,7 @@ export default function ChatInput({
// @ts-expect-error type check on previous line
uploader.removeByURL(blocks[idx].image.src);
}
chatStoreLogger.log('onRemove', { id, blocks });
useChatStore.getState().setBlocks(
id,
blocks.filter((_b, k) => k !== idx)
Expand All @@ -560,6 +567,7 @@ export default function ChatInput({

// @ts-expect-error tsc is not tracking the type narrowing in the filter
const imageBlocks: ChatImage[] = chatInfo.blocks.filter((b) => 'image' in b);
// chatStoreLogger.log('ChatInputRender', id, chatInfo);

if (shipHasBlockedUs) {
return (
Expand Down
36 changes: 22 additions & 14 deletions ui/src/chat/useChatStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createDevLogger } from '@/logic/utils';
import { ChatBlock, ChatBrief, ChatBriefs } from '@/types/chat';
import produce from 'immer';
import { useCallback } from 'react';
Expand Down Expand Up @@ -45,14 +46,16 @@ export interface ChatStore {
update: (briefs: ChatBriefs) => void;
}

const emptyInfo: ChatInfo = {
const emptyInfo: () => ChatInfo = () => ({
replying: null,
blocks: [],
unread: undefined,
dialogs: {},
hovering: '',
failedToLoadContent: {},
};
});

export const chatStoreLogger = createDevLogger('ChatStore', false);

export const useChatStore = create<ChatStore>((set, get) => ({
chats: {},
Expand All @@ -61,9 +64,10 @@ export const useChatStore = create<ChatStore>((set, get) => ({
produce((draft: ChatStore) => {
Object.entries(briefs).forEach(([whom, brief]) => {
const chat = draft.chats[whom];
chatStoreLogger.log('update', whom, chat, brief, draft.chats);
if (brief.count > 0 && brief['read-id']) {
draft.chats[whom] = {
...(chat || emptyInfo),
...(chat || emptyInfo()),
unread: {
seen: false,
readTimeout: 0,
Expand All @@ -88,9 +92,10 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

chatStoreLogger.log('setBlocks', whom, blocks);
draft.chats[whom].blocks = blocks;
})
);
Expand All @@ -99,7 +104,7 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

draft.chats[whom].dialogs[writId] = dialogs;
Expand All @@ -110,7 +115,7 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

if (!draft.chats[whom].failedToLoadContent[writId]) {
Expand All @@ -126,7 +131,7 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

draft.chats[whom].hovering = hovering ? writId : '';
Expand All @@ -137,7 +142,7 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

draft.chats[whom].replying = msgId;
Expand All @@ -148,7 +153,7 @@ export const useChatStore = create<ChatStore>((set, get) => ({
set(
produce((draft: ChatStore) => {
if (!draft.chats[whom]) {
draft.chats[whom] = emptyInfo;
draft.chats[whom] = emptyInfo();
}

const chat = draft.chats[whom];
Expand All @@ -172,13 +177,14 @@ export const useChatStore = create<ChatStore>((set, get) => ({
return;
}

chatStoreLogger.log('read', whom, chat);
delete chat.unread;
})
);
},
delayedRead: (whom, cb) => {
const { chats, read } = get();
const chat = chats[whom] || emptyInfo;
const chat = chats[whom] || emptyInfo();

if (!chat.unread || chat.unread.readTimeout) {
return;
Expand All @@ -191,10 +197,12 @@ export const useChatStore = create<ChatStore>((set, get) => ({

set(
produce((draft) => {
const latest = draft.chats[whom] || emptyInfo();
chatStoreLogger.log('delayedRead', whom, chat, latest);
draft.chats[whom] = {
...chat,
...latest,
unread: {
...chat.unread,
...latest.unread,
readTimeout,
},
};
Expand All @@ -204,8 +212,8 @@ export const useChatStore = create<ChatStore>((set, get) => ({
unread: (whom, brief) => {
set(
produce((draft: ChatStore) => {
const chat = draft.chats[whom] || emptyInfo;

const chat = draft.chats[whom] || emptyInfo();
chatStoreLogger.log('unread', whom, chat, brief);
draft.chats[whom] = {
...chat,
unread: {
Expand Down
7 changes: 6 additions & 1 deletion ui/src/components/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import HardBreak from '@tiptap/extension-hard-break';
import { useIsMobile } from '@/logic/useMedia';
import ChatInputMenu from '@/chat/ChatInputMenu/ChatInputMenu';
import { refPasteRule, Shortcuts } from '@/logic/tiptap';
import { useChatBlocks, useChatStore } from '@/chat/useChatStore';
import {
chatStoreLogger,
useChatBlocks,
useChatStore,
} from '@/chat/useChatStore';
import { useCalm } from '@/state/settings';
import Mention from '@tiptap/extension-mention';
import { PASTEABLE_IMAGE_TYPES } from '@/constants';
Expand Down Expand Up @@ -70,6 +74,7 @@ export function useMessageEditor({
return;
}
setBlocks(whom, [...chatBlocks, { cite: r }]);
chatStoreLogger.log('onReference', { whom, r, chatBlocks });
},
[chatBlocks, setBlocks, whom]
);
Expand Down
3 changes: 3 additions & 0 deletions ui/src/components/Sidebar/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ vi.mock('@/logic/utils', () => ({
storageVersion: () => 0,
clearStorageMigration: () => ({}),
isTalk: () => false,
createDevLogger: () => ({
log: () => ({}),
}),
}));

describe('Sidebar', () => {
Expand Down
Loading