Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaricMourgues authored and Labels Bot committed May 30, 2022
1 parent 361ffa9 commit 74e850c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 41 deletions.
19 changes: 12 additions & 7 deletions twake/backend/node/src/services/messages/services/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ export class ThreadMessagesService implements MessageThreadMessagesServiceAPI {
try {
if (options.files) message = await this.completeMessageFiles(message, options.files || []);
} catch (err) {
logger.warn("Error while completing message files", err);
console.log(err);
logger.warn("Error while completing message files");
}

//Mobile retro compatibility
Expand Down Expand Up @@ -675,17 +676,22 @@ export class ThreadMessagesService implements MessageThreadMessagesServiceAPI {
const previousMessageFiles = message.files;
message.files = [];
for (const file of files) {
const existing = existingMsgFiles.filter(e => sameFile(e.metadata, file.metadata))[0];
let existing = existingMsgFiles.filter(e => sameFile(e.metadata, file.metadata))[0];
const entity = existing || new MessageFile();
entity.message_id = message.id;
entity.id = file.id || undefined;
entity.company_id = file.company_id;

//If it is defined it should exists
if (
entity.id &&
!(await this.msgFilesRepository.findOne({ message_id: message.id, id: entity.id }))
) {
let messageFileExistOnDb = false;
try {
messageFileExistOnDb = !!(await this.msgFilesRepository.findOne({
message_id: message.id,
id: entity.id,
}));
} catch (e) {}
if (entity.id && !messageFileExistOnDb) {
existing = null;
entity.id = undefined;
}

Expand Down Expand Up @@ -719,7 +725,6 @@ export class ThreadMessagesService implements MessageThreadMessagesServiceAPI {

if (!existing || !_.isEqual(existing.metadata, entity.metadata)) {
didChange = true;

await this.msgFilesRepository.save(entity);
}

Expand Down
24 changes: 13 additions & 11 deletions twake/frontend/src/app/components/twacode/twacode.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useRef } from 'react';

import PseudoMarkdownCompiler from 'app/features/global/services/pseudo-markdown-compiler-service';
import Compile from './compile';
Expand All @@ -15,12 +15,13 @@ type Props = {
onAction?: (type: string, id: string, context: any, value: any, evt: any) => void;
};

let loadingInteractionTimeout: any = 0;

export default React.memo((props: Props) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let container: any = null;
let passives: any = {};
let loadingInteraction_timeout: any = 0;
let saved_stringified: string = '';
let savedStringified = useRef('');

const [loadingInteraction, setLoadingInteraction] = useState(false);

Expand All @@ -30,9 +31,9 @@ export default React.memo((props: Props) => {
if (type === 'interactive_action') {
if (props.onAction) {
setLoadingInteraction(true);
clearTimeout(loadingInteraction_timeout);
loadingInteraction_timeout = setTimeout(() => {
saved_stringified = '';
clearTimeout(loadingInteractionTimeout);
loadingInteractionTimeout = setTimeout(() => {
savedStringified.current = '';
setLoadingInteraction(false);
}, 5000);
props.onAction(type, id, context, JSON.parse(JSON.stringify(passives)), evt);
Expand All @@ -51,17 +52,18 @@ export default React.memo((props: Props) => {

useEffect(() => {
var stringified = JSON.stringify([props.content]);
if (stringified !== saved_stringified) {
clearTimeout(loadingInteraction_timeout);
if (stringified !== savedStringified.current) {
clearTimeout(loadingInteractionTimeout);
// eslint-disable-next-line react-hooks/exhaustive-deps
saved_stringified = stringified;
savedStringified.current = stringified;
setLoadingInteraction(false);
console.log('called setLoadingInteraction');
}
return () => {
//Called when element is unmounted
clearTimeout(loadingInteraction_timeout);
clearTimeout(loadingInteractionTimeout);
};
}, [props.content]);
}, [JSON.stringify(props.content)]);

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Api from '../../global/framework/api-service';
import { ChannelType } from 'app/features/channels/types/channel';
import { TwakeService } from '../../global/framework/registry-decorator-service';
import { delayRequest } from 'app/features/global/utils/managedSearchRequest';
import { removeBadgesNow } from 'app/features/users/hooks/use-notifications';

const PREFIX = '/internal/services/channels/v1/companies';

Expand Down Expand Up @@ -41,6 +42,9 @@ class ChannelAPIClientService {
{ status = true, requireFocus = false, now = false },
): Promise<void> {
if (requireFocus && !document.hasFocus()) return;

if (status) removeBadgesNow('channel', channelId);

delayRequest(
'reach-end-read-channel-' + channelId,
() =>
Expand Down
18 changes: 18 additions & 0 deletions twake/frontend/src/app/features/users/hooks/use-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import RouterService from '../../router/services/router-service';
import { useCallback } from 'react';
import { pushDesktopNotification } from '../services/push-desktop-notification';

export let removeBadgesNow = (type: 'channel' | 'workspace' | 'company', id: string) => {};

export const useNotifications = () => {
const [badges, setBadges] = useRecoilState(NotificationsBadgesState);
const companyId = useRouterCompany();
Expand All @@ -42,6 +44,22 @@ export const useNotifications = () => {
[setBadges, badges],
);

const removeObjectBadges = useRecoilCallback(
({ snapshot }) =>
(type: 'channel' | 'workspace' | 'company', id: string) => {
const badges = snapshot.getLoadable(NotificationsBadgesState).getValue();
const list = badges.filter(
b =>
(type === 'channel' && b.channel_id !== id) ||
(type === 'workspace' && b.workspace_id !== id) ||
(type === 'company' && b.company_id !== id),
);
setBadges(list);
},
[setBadges, badges],
);
removeBadgesNow = removeObjectBadges;

const setCompanyBadges = useRecoilCallback(
({ snapshot }) =>
(newBadges: NotificationType[], companyId: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ export default (props: Props) => {
companyId,
};

const hasEphemeral = !(
!lastEphemeral ||
!(lastEphemeral.thread_id === props.threadId || (!props.threadId && !lastEphemeral.thread_id))
);

useEffect(() => {
if (lastEphemeral) {
if (lastEphemeral && hasEphemeral) {
props.onHasEphemeralMessage();
} else {
props.onNotEphemeralMessage();
}
}, [lastEphemeral]);
}, [lastEphemeral, hasEphemeral]);

if (!lastEphemeral || (props.threadId && lastEphemeral.thread_id !== props.threadId)) {
if (!hasEphemeral) {
return <div />;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useContext, useState } from 'react';
import React, { Suspense, useContext, useEffect, useState } from 'react';
import 'moment-timezone';
import classNames from 'classnames';
import Reactions from './Reactions';
Expand All @@ -25,17 +25,16 @@ type Props = {
threadHeader?: string;
};

let loadingInteractionTimeout: any = 0;

export default (props: Props) => {
const [active, setActive] = useState(false);
const [loadingAction, setLoadingAction] = useState(false);
const [didMouseOver, setDidMouseOver] = useState(false);
let loading_interaction_timeout: any = 0;

const context = useContext(MessageContext);
let { message } = useMessage(context);

const companyId = context.companyId;

const onInteractiveMessageAction = (action_id: string, context: any, passives: any, evt: any) => {
var app_id = message.application_id;
var type = 'interactive_message_action';
Expand All @@ -51,14 +50,18 @@ export default (props: Props) => {
const onAction = (type: string, id: string, context: any, passives: any, evt: any) => {
if (type === 'interactive_action') {
setLoadingAction(true);
clearTimeout(loading_interaction_timeout);
loading_interaction_timeout = setTimeout(() => {
clearTimeout(loadingInteractionTimeout);
loadingInteractionTimeout = setTimeout(() => {
setLoadingAction(false);
}, 5000);
onInteractiveMessageAction(id, context, passives, evt);
}
};

useEffect(() => {
setLoadingAction(false);
}, [JSON.stringify(message.blocks)]);

const deleted = message.subtype === 'deleted';

const location = `message-${message.id}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default ({ channelId, companyId, workspaceId, threadId }: Props) => {
style={virtuosoLoading ? { opacity: 0 } : {}}
onScroll={(e: any) => {
const scrollBottom = e.target.scrollHeight - e.target.scrollTop - e.target.clientHeight;
const closeToBottom = scrollBottom < 100;
const closeToBottom = scrollBottom < 500;
if (closeToBottom !== atBottom) setAtBottom(closeToBottom);
cancelHighlight();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,12 @@ const FullMenu = (props: PropsType): JSX.Element => {
: 'scenes.app.channelsbar.unread_sign',
),
onClick: () => {
badges.length > 0
? ChannelAPIClient.read(
props.channel.company_id || '',
props.channel.workspace_id || '',
props.channel.id || '',
{ status: true, now: true },
)
: ChannelAPIClient.read(
props.channel.company_id || '',
props.channel.workspace_id || '',
props.channel.id || '',
{ status: false, now: true },
);
ChannelAPIClient.read(
props.channel.company_id || '',
props.channel.workspace_id || '',
props.channel.id || '',
{ status: badges.length > 0, now: true },
);
},
},
{
Expand Down

0 comments on commit 74e850c

Please sign in to comment.