Skip to content

Commit

Permalink
Merge pull request #2766 from daostack/staging
Browse files Browse the repository at this point in the history
Instant reorder
  • Loading branch information
pvm-code authored Nov 18, 2024
2 parents ef91494 + a66d8a5 commit 6371b77
Show file tree
Hide file tree
Showing 39 changed files with 932 additions and 330 deletions.
19 changes: 16 additions & 3 deletions src/pages/Auth/store/saga.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
subscribeToNotification,
} from "@/pages/OldCommon/store/api";
import { UserService } from "@/services";
import { store } from "@/shared/appConfig";
import { persistor, store } from "@/shared/appConfig";
import { Awaited } from "@/shared/interfaces";
import { FirebaseCredentials } from "@/shared/interfaces/FirebaseCredentials";
import { EventTypeState, NotificationItem } from "@/shared/models/Notification";
Expand Down Expand Up @@ -48,6 +48,7 @@ import firebase from "../../../shared/utils/firebase";
import { UserCreationDto } from "../interface";
import * as actions from "./actions";
import { createdUserApi, deleteUserApi, getUserData } from "./api";
import { resetOptimisticState } from "@/store/states/optimistic/actions";

const getAuthProviderFromProviderData = (
providerData?: firebase.User["providerData"],
Expand Down Expand Up @@ -533,15 +534,27 @@ function* confirmVerificationCodeSaga({
}

function* logOut() {

yield put(resetOptimisticState());
// Wait for persistor.purge() to complete
yield call([persistor, persistor.purge]);
yield call([persistor, persistor.flush]);

// Now clear localStorage
localStorage.clear();
firebase.auth().signOut();

// Sign out from Firebase
yield call([firebase.auth(), firebase.auth().signOut]);

// Notify React Native WebView if applicable
if (window.ReactNativeWebView) {
window?.ReactNativeWebView?.postMessage(WebviewActions.logout);
window.ReactNativeWebView.postMessage(WebviewActions.logout);
}

// Reset global data and navigate to home
resetGlobalData(true);
history.push(ROUTE_PATHS.HOME);

yield true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,116 +25,116 @@ export const useCommonTransactionsChartDataSet =
orderedCommonTransactions: TransactionData[],
commonCreatedAt?: Time,
) => {
const uniqueTransactionsMonths = new Set();

const groupedByMonthPayInsSummaries: { [key: string]: number } = {};
const groupedByMonthPayOutsSummaries: { [key: string]: number } = {};

orderedCommonTransactions
.filter(
(transaction) =>
getMonthsDifference(
new Date(transaction.createdAt.seconds * 1000),
new Date(),
) <= TRANSACTIONS_PERIOD_MONTHS_AMOUNT,
)
.map((transaction) => ({
...transaction,
amount: transaction.amount / 100,
}))
.reverse()
.map((transaction) => {
const transactionMonthNotation =
BRIEF_MONTH_NAMES[
new Date(transaction.createdAt.seconds * 1000).getMonth()
];

uniqueTransactionsMonths.add(transactionMonthNotation);

if (
groupedByMonthPayInsSummaries[transactionMonthNotation] ===
undefined
)
groupedByMonthPayInsSummaries[transactionMonthNotation] = 0;

if (
groupedByMonthPayOutsSummaries[transactionMonthNotation] ===
undefined
)
groupedByMonthPayOutsSummaries[transactionMonthNotation] = 0;

if (transaction.type === TransactionType.PayIn) {
groupedByMonthPayInsSummaries[transactionMonthNotation] +=
transaction.amount;
} else if (transaction.type === TransactionType.PayOut) {
groupedByMonthPayOutsSummaries[transactionMonthNotation] +=
transaction.amount;
}

return transaction;
});

const chartMonthLabelsList = Array.from(
uniqueTransactionsMonths,
) as string[];

/*
FIXME: tempo decision to prevent common's crashing (some common-records have createdAt set in null),
should be reverted after full merging of the Governance & clearing the DB from legacy stuff
*/
if (commonCreatedAt) {
const commonCreatedAtMonthNotation =
BRIEF_MONTH_NAMES[
new Date(commonCreatedAt.seconds * 1000).getMonth()
];

if (
!chartMonthLabelsList.includes(commonCreatedAtMonthNotation) &&
getMonthsDifference(
new Date(commonCreatedAt.seconds * 1000),
new Date(),
) <= TRANSACTIONS_PERIOD_MONTHS_AMOUNT
) {
chartMonthLabelsList.unshift(commonCreatedAtMonthNotation);

groupedByMonthPayInsSummaries[commonCreatedAtMonthNotation] = 0;
groupedByMonthPayOutsSummaries[commonCreatedAtMonthNotation] = 0;
}
}

const payInsChartData = chartMonthLabelsList.map(
(monthLabel) => groupedByMonthPayInsSummaries[monthLabel],
);
const payOutsChartData = chartMonthLabelsList.map(
(monthLabel) => groupedByMonthPayOutsSummaries[monthLabel],
);
const balanceChartData = payInsChartData.reduce(
(
accum: { currentBalance: number; balances: number[] },
payInsMonthSum,
index,
) => {
let newBalance = accum.currentBalance;

newBalance += payInsMonthSum;
newBalance -= payOutsChartData[index];

return {
currentBalance: newBalance,
balances: [...accum.balances, newBalance],
};
},
{
currentBalance: 0,
balances: [],
},
).balances;
// const uniqueTransactionsMonths = new Set();

// const groupedByMonthPayInsSummaries: { [key: string]: number } = {};
// const groupedByMonthPayOutsSummaries: { [key: string]: number } = {};

// orderedCommonTransactions
// .filter(
// (transaction) =>
// getMonthsDifference(
// new Date(transaction.createdAt.seconds * 1000),
// new Date(),
// ) <= TRANSACTIONS_PERIOD_MONTHS_AMOUNT,
// )
// .map((transaction) => ({
// ...transaction,
// amount: transaction.amount / 100,
// }))
// .reverse()
// .map((transaction) => {
// const transactionMonthNotation =
// BRIEF_MONTH_NAMES[
// new Date(transaction.createdAt.seconds * 1000).getMonth()
// ];

// uniqueTransactionsMonths.add(transactionMonthNotation);

// if (
// groupedByMonthPayInsSummaries[transactionMonthNotation] ===
// undefined
// )
// groupedByMonthPayInsSummaries[transactionMonthNotation] = 0;

// if (
// groupedByMonthPayOutsSummaries[transactionMonthNotation] ===
// undefined
// )
// groupedByMonthPayOutsSummaries[transactionMonthNotation] = 0;

// if (transaction.type === TransactionType.PayIn) {
// groupedByMonthPayInsSummaries[transactionMonthNotation] +=
// transaction.amount;
// } else if (transaction.type === TransactionType.PayOut) {
// groupedByMonthPayOutsSummaries[transactionMonthNotation] +=
// transaction.amount;
// }

// return transaction;
// });

// const chartMonthLabelsList = Array.from(
// uniqueTransactionsMonths,
// ) as string[];

// /*
// FIXME: tempo decision to prevent common's crashing (some common-records have createdAt set in null),
// should be reverted after full merging of the Governance & clearing the DB from legacy stuff
// */
// if (commonCreatedAt) {
// const commonCreatedAtMonthNotation =
// BRIEF_MONTH_NAMES[
// new Date(commonCreatedAt.seconds * 1000).getMonth()
// ];

// if (
// !chartMonthLabelsList.includes(commonCreatedAtMonthNotation) &&
// getMonthsDifference(
// new Date(commonCreatedAt.seconds * 1000),
// new Date(),
// ) <= TRANSACTIONS_PERIOD_MONTHS_AMOUNT
// ) {
// chartMonthLabelsList.unshift(commonCreatedAtMonthNotation);

// groupedByMonthPayInsSummaries[commonCreatedAtMonthNotation] = 0;
// groupedByMonthPayOutsSummaries[commonCreatedAtMonthNotation] = 0;
// }
// }

// const payInsChartData = chartMonthLabelsList.map(
// (monthLabel) => groupedByMonthPayInsSummaries[monthLabel],
// );
// const payOutsChartData = chartMonthLabelsList.map(
// (monthLabel) => groupedByMonthPayOutsSummaries[monthLabel],
// );
// const balanceChartData = payInsChartData.reduce(
// (
// accum: { currentBalance: number; balances: number[] },
// payInsMonthSum,
// index,
// ) => {
// let newBalance = accum.currentBalance;

// newBalance += payInsMonthSum;
// newBalance -= payOutsChartData[index];

// return {
// currentBalance: newBalance,
// balances: [...accum.balances, newBalance],
// };
// },
// {
// currentBalance: 0,
// balances: [],
// },
// ).balances;

return {
chartMonthLabelsList,
payInsChartData,
payOutsChartData,
balanceChartData,
chartMonthLabelsList: [],
payInsChartData: [],
payOutsChartData: [],
balanceChartData: [],
};
},
[],
Expand Down
49 changes: 45 additions & 4 deletions src/pages/common/components/ChatComponent/ChatComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ import {
selectOptimisticFeedItems,
commonActions,
selectOptimisticDiscussionMessages,
inboxActions,
optimisticActions,
selectInstantDiscussionMessagesOrder,
} from "@/store/states";
import { ChatContentContext, ChatContentData } from "../CommonContent/context";
import {
Expand All @@ -85,8 +88,10 @@ import {
} from "./utils";
import styles from "./ChatComponent.module.scss";
import { BaseTextEditorHandles } from "@/shared/ui-kit/TextEditor/BaseTextEditor";
import { useFeedItemContext } from "../FeedItem";

const BASE_CHAT_INPUT_HEIGHT = 48;
const BASE_ORDER_INTERVAL = 1000;

interface ChatComponentInterface {
commonId: string;
Expand Down Expand Up @@ -254,6 +259,20 @@ export default function ChatComponent({
parseStringToTextEditorValue(),
);

const {
setIsInputFocused
} = useFeedItemContext();

useEffect(() => {
const isEmpty = checkIsTextEditorValueEmpty(message);
if(!isEmpty || message.length > 1) {
setIsInputFocused?.(true);
} else {
setIsInputFocused?.(false);
}

},[message, setIsInputFocused])

const emojiCount = useMemo(
() => countTextEditorEmojiElements(message),
[message],
Expand All @@ -275,6 +294,9 @@ export default function ChatComponent({
const optimisticDiscussionMessages = useSelector(
selectOptimisticDiscussionMessages,
);
const instantDiscussionMessagesOrder = useSelector(selectInstantDiscussionMessagesOrder);

const currentChatOrder = instantDiscussionMessagesOrder.get(discussionId)?.order || 1;

const isOptimisticChat = optimisticFeedItems.has(discussionId);

Expand All @@ -295,7 +317,7 @@ export default function ChatComponent({
});

dispatch(
commonActions.clearOptimisticDiscussionMessages(
optimisticActions.clearOptimisticDiscussionMessages(
optimisticMessageDiscussionId,
),
);
Expand Down Expand Up @@ -414,8 +436,8 @@ export default function ChatComponent({
setMessages([]);
}
},
1500,
[newMessages, discussionId, dispatch],
1500 + BASE_ORDER_INTERVAL * currentChatOrder,
[newMessages, discussionId, dispatch, currentChatOrder],
);

/**
Expand Down Expand Up @@ -573,7 +595,7 @@ export default function ChatComponent({
}

if (isOptimisticChat) {
dispatch(commonActions.setOptimisticDiscussionMessages(payload));
dispatch(optimisticActions.setOptimisticDiscussionMessages(payload));
} else {
setMessages((prev) => {
if (isFilesMessageWithoutTextAndImages) {
Expand All @@ -582,6 +604,7 @@ export default function ChatComponent({

return [...prev, ...filePreviewPayload, payload];
});
dispatch(optimisticActions.setInstantDiscussionMessagesOrder({discussionId}));
}

if (isChatChannel) {
Expand All @@ -606,6 +629,23 @@ export default function ChatComponent({
if (currentFilesPreview) {
dispatch(chatActions.clearFilesPreview());
}

const payloadUpdateFeedItem = {
feedItemId,
lastMessage: {
messageId: pendingMessageId,
ownerId: userId as string,
userName: getUserName(user),
ownerType: DiscussionMessageOwnerType.User,
content: JSON.stringify(message),
}
};

dispatch(commonActions.setFeedItemUpdatedAt(payloadUpdateFeedItem));
dispatch(inboxActions.setInboxItemUpdatedAt(payloadUpdateFeedItem));
document
.getElementById("feedLayoutWrapper")
?.scrollIntoView({ behavior: "smooth" });
focusOnChat();
}
},
Expand All @@ -620,6 +660,7 @@ export default function ChatComponent({
isChatChannel,
linkPreviewData,
isOptimisticChat,
feedItemId,
],
);

Expand Down
Loading

0 comments on commit 6371b77

Please sign in to comment.