Skip to content

Commit d728cdd

Browse files
author
Victoria Ivanova
committed
move clientIdMapStore logics from Report.tsx to store/index.ts
1 parent c02fade commit d728cdd

File tree

3 files changed

+52
-44
lines changed

3 files changed

+52
-44
lines changed

frontend/src/pages/Report/Report.tsx

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { useEffect } from "react";
22

33
import { formatDistanceToNow } from "date-fns";
44
import { ru } from "date-fns/locale";
5-
import { useUnit, useStoreMap } from "effector-react";
5+
import { useUnit } from "effector-react";
66
import { useParams, useNavigate } from "react-router-dom";
77

88
import { useReportPageSocket } from "@/hooks/useReportPageSocket";
99
import { useSocketEvent } from "@/hooks/useSocketEvent";
10-
import { $allBugsStore } from "@/store";
10+
import { $combinedBugsStore } from "@/store";
1111
import { createLocalBugEvent } from "@/store/localBugs";
1212
import {
1313
$initialReportStore,
@@ -18,7 +18,6 @@ import {
1818
saveTitleEvent,
1919
updateReportPathIdEvent,
2020
} from "@/store/report";
21-
import { BugClientEntity } from "@/types/bug";
2221
import { SocketEvent } from "@/webSocketApi/models";
2322

2423
import Bug from "./components/Bug/Bug";
@@ -29,40 +28,7 @@ const ReportPage = () => {
2928
const initialReport = useUnit($initialReportStore);
3029
const title = useUnit($titleStore);
3130
const creatorUserName = useUnit($creatorUserNameStore);
32-
33-
const allBugs = useStoreMap({
34-
store: $allBugsStore,
35-
keys: [reportId],
36-
fn: ([bugsData, localBugs], [currentReportId]) => {
37-
if (!currentReportId) return [];
38-
39-
const { bugs, reportBugs } = bugsData;
40-
const bugIds = reportBugs[Number(currentReportId)] || [];
41-
42-
// 1) Собираем мапу bug.id → clientId для любого локального багa
43-
const clientIdMap: Record<number, number> = {};
44-
localBugs.forEach((bug) => {
45-
// у вас в store и локальный, и промоушн лежат, нам нужна обоих запись
46-
clientIdMap[bug.id] = bug.clientId;
47-
});
48-
49-
// 2) Берём бекендовые баги и подставляем правильный clientId
50-
const bugsFromStore = bugIds
51-
.map((id: number) => bugs[id])
52-
.filter(Boolean)
53-
.map((bug) => ({
54-
...bug,
55-
clientId: clientIdMap[bug.id] ?? bug.id, // ← вот здесь ключ сохраняется прежним
56-
}));
57-
58-
// 3) К этим добавляем только ещё непромоушненные локальные
59-
const pendingLocals = localBugs.filter(
60-
(bug) => bug.reportId === Number(currentReportId) && bug.isLocalOnly
61-
);
62-
63-
return [...bugsFromStore, ...pendingLocals];
64-
},
65-
});
31+
const allBugs = useUnit($combinedBugsStore);
6632

6733
useReportPageSocket();
6834
useSocketEvent(SocketEvent.ReportPatch, (patch) =>
@@ -110,7 +76,7 @@ const ReportPage = () => {
11076
пользователем <strong>{creatorUserName || "Загрузка..."}</strong>
11177
</div>
11278
<div className="flex flex-col gap-2">
113-
{allBugs.map((bug: BugClientEntity) => (
79+
{allBugs.map((bug) => (
11480
<Bug key={bug.clientId} bug={bug} />
11581
))}
11682
<button

frontend/src/store/bugs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const $bugsStore = createStore<Record<number, BugClientEntity>>({})
125125
.reset(clearBugsEvent);
126126

127127
// Список id багов для каждого репорта
128-
export const $reportBugsStore = createStore<Record<number, number[]>>({})
128+
export const $reportBugIdsStore = createStore<Record<number, number[]>>({})
129129
.on(setBugsEvent, (state, { reportId, bugs }) => ({
130130
...state,
131131
[reportId]: bugs.map((bug) => bug.id),
@@ -146,8 +146,8 @@ export const $reportBugsStore = createStore<Record<number, number[]>>({})
146146
// Combined store из всех багов и id багов для каждого репорта
147147
export const $bugsData = combine(
148148
$bugsStore,
149-
$reportBugsStore,
150-
(bugs, reportBugs) => ({ bugs, reportBugs })
149+
$reportBugIdsStore,
150+
(bugs, reportBugIds) => ({ bugs, reportBugIds })
151151
);
152152

153153
/** Сэмплы */

frontend/src/store/index.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { sample, combine } from "effector";
1+
import { sample, combine, createStore } from "effector";
22

33
import { $bugsData } from "./bugs";
4-
import { $localBugsStore, clearLocalBugEvent } from "./localBugs";
4+
import {
5+
$localBugsStore,
6+
clearLocalBugEvent,
7+
promoteLocalBugToBackendEvent,
8+
} from "./localBugs";
59
import {
610
$creatorUserIdStore,
711
$pastResponsibleUserIdStore,
@@ -16,7 +20,45 @@ import { $user } from "./user";
1620
import { setBugsEvent } from "./commonEvents";
1721

1822
const $src = combine({ user: $user, reportPath: $reportPathStore });
19-
export const $allBugsStore = combine($bugsData, $localBugsStore);
23+
24+
// Мапа для сопоставления id c с бэка с clientId для промотированных багов
25+
// Нужна, чтобы запоминать clientId бага, который был только на фронтенде, но появился на бэке
26+
// clientId используем на ui как react key, чтобы не перерисовывать полностью компонент Bug
27+
export const $clientIdMapStore = createStore<Record<number, number>>({})
28+
.on(promoteLocalBugToBackendEvent, (state, { tempClientId, backendBug }) => ({
29+
...state,
30+
[backendBug.id]: tempClientId,
31+
}))
32+
.reset(clearLocalBugEvent);
33+
34+
// Стор для багов с бэка и локальных
35+
export const $combinedBugsStore = combine(
36+
$bugsData,
37+
$localBugsStore,
38+
$clientIdMapStore,
39+
(bugsData, localBugs, clientIdMap) => {
40+
const { bugs, reportBugIds } = bugsData;
41+
42+
const allBugIds = Object.values(reportBugIds).flat();
43+
44+
// Обрабатываем backend баги
45+
const bugsFromStore = allBugIds.map((id: number) => {
46+
const bug = bugs[id];
47+
48+
return bug.clientId
49+
? bug
50+
: {
51+
...bug,
52+
clientId: clientIdMap[bug.id] || bug.id,
53+
};
54+
});
55+
56+
// Добавляем только локальные фронтовые баги
57+
const pendingLocals = localBugs.filter((bug) => bug.isLocalOnly);
58+
59+
return bugsFromStore.concat(pendingLocals);
60+
}
61+
);
2062

2163
// заполнение сторов при открытии страницы создания репорта
2264
sample({

0 commit comments

Comments
 (0)