Skip to content

Commit

Permalink
Fixed document err, message (#3611)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat authored Apr 5, 2023
1 parent ce85ac7 commit c184e9d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/app-mobile/src/screens/Unlocked/Chat/ChatDetailScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function ChatDetailScreen({
const { roomType, roomId, remoteUserId, remoteUsername } = route.params;
const user = useUser();
const avatarUrl = useAvatarUrl();
const existingChatRef = useRef<any>();

const [isLoadingEarlier, setIsLoadingEarlier] = useState(false);
const { chats } = useChatsWithMetadata({
Expand All @@ -81,6 +82,17 @@ export function ChatDetailScreen({
const [messages, setMessages] = useState([]);

useEffect(() => {
if (
existingChatRef.current &&
JSON.stringify(chats) === JSON.stringify(existingChatRef.current)
) {
return;
}
if (chats && chats.length) {
SignalingManager.getInstance().debouncedUpdateLastRead(
chats[chats.length - 1]
);
}
const _messages = chats
.map((x) => {
return {
Expand All @@ -104,10 +116,10 @@ export function ChatDetailScreen({
})
.reverse();

existingChatRef.current = chats;
setMessages(_messages);
}, []);
}, [chats]);

// TODO(kirat) not sure this is doing anything
const onSend = useCallback(
async (messages = []) => {
const [message] = messages;
Expand Down Expand Up @@ -150,10 +162,6 @@ export function ChatDetailScreen({
room: roomId.toString(),
},
});

setMessages((previousMessages) => {
GiftedChat.append(previousMessages, messages);
});
},
[chats.length, roomId, roomType, user.uuid, remoteUserId, remoteUsername]
);
Expand Down
66 changes: 66 additions & 0 deletions packages/db/src/api/LocalImageManager.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { getImage, putImage } from "../db/images";

export class LocalImageManager {
static instance: LocalImageManager;
queue: { image: string; timestamp: number; fullImage?: boolean }[] = [];

private constructor() {
this.process();
}

async process() {
this.queue = this.queue
.filter((x) => (new Date().getTime() - x.timestamp) / 1000 > 3600)
.sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1));
const nextEl = this.queue.pop();
if (nextEl) {
await this.storeImageInLocalStorage(nextEl.image, nextEl.fullImage);
}
await this.sleep(15);
this.process();
}

async sleep(timer) {
await new Promise((resolve) => setTimeout(resolve, timer * 1000));
}

bulkAddToQueue(elements: { image: string }[]) {
elements.forEach((el) => this.addToQueue(el));
}

async addToQueue({ image }: { image: string }) {
if (this.queue.find((x) => x.image === image)) {
return;
}

try {
const parsedEl = await getImage("images", `image-${image}`);
if (parsedEl) {
this.queue.push({
image,
timestamp: parsedEl.timestamp,
fullImage: parsedEl.fullImage || false,
});
} else {
this.queue.push({ image, timestamp: 0 });
}
} catch (e) {
this.queue.push({ image, timestamp: 0 });
}
}

static getInstance() {
if (!this.instance) {
this.instance = new LocalImageManager();
}
return this.instance;
}

storeImageInLocalStorage(
url: string,
fullImage?: boolean,
overridenUrl?: string
) {
return new Promise((resolve, reject) => {});
}
}

1 comment on commit c184e9d

@vercel
Copy link

@vercel vercel bot commented on c184e9d Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.