-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathTabber.tsx
127 lines (107 loc) · 3.89 KB
/
Tabber.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable @typescript-eslint/no-unnecessary-condition -- to avoid lint error that will be remove soon on a changhe of how the data will be dealed */
import { ChangeEvent, ReactNode, useEffect, useState } from 'react';
import { Account } from '~services/api/accounts/accounts.types';
import { PostMode } from '~services/api/social-media/social-media.types';
import { AccountPost, useAccountStore } from '~stores/useAccountStore';
import { useSocialMediaStore } from '~stores/useSocialMediaStore/useSocialMediaStore';
import { accountsToTabs } from './utils';
import MainComposerBase from '~components/MainComposer/components/MainComposerBase/MainComposerBase';
import Preview from '~components/Preview/Preview';
import scss from '~components/Tabber/Tabber.module.scss';
import PostModes from './PostModes/PostModes';
import Tabs from './Tabs/Tabs';
import { Tab, TabId, Tabs as TabsType } from './Tabber.types';
const makeId = (account: AccountPost): `${string}-${string}` =>
`${account.id}-${account.socialMediaId}`;
function Tabber(): ReactNode {
const { accounts } = useAccountStore();
const { socialMedias } = useSocialMediaStore();
const [currentTab, setCurrentTab] = useState<TabId>(
makeId(accounts[0] || {})
);
const [tabs, setTabs] = useState<TabsType>({});
useEffect(() => {
if (accounts.length > 0) {
const initialTabId = accounts.length > 0 ? makeId(accounts[0]) : '';
setCurrentTab(initialTabId as TabId);
setTabs(accountsToTabs(accounts, socialMedias));
} else {
setTabs({});
setCurrentTab('' as unknown as TabId);
}
}, [accounts, socialMedias]);
const getCurrentPostMode = (): PostMode | undefined => {
if (!currentTab) return;
const [, socialMediaId] = currentTab.split('-');
const socialMedia = socialMedias.get(socialMediaId);
if (!socialMedia) return;
const postModeOnView = tabs[currentTab]?.postModeOnView;
return socialMedia.postModes.find(
(postMode: PostMode) => postMode.id === postModeOnView
);
};
const currentPostMode = getCurrentPostMode();
const handleContentChange = (e: ChangeEvent<HTMLTextAreaElement>): void => {
if (!currentTab || !tabs[currentTab]) return;
const tab = { ...tabs[currentTab] };
const postId = tab.postModeOnView;
if (tab.posts[postId]) {
tab.posts[postId].text = e.target.value;
}
setTabs({
...tabs,
[currentTab]: tab,
});
};
const changeCurrentTab = (tab: Tab): void => {
setCurrentTab(tab.id);
};
const changePostMode = (postMode: PostMode): void => {
if (!currentTab || !tabs[currentTab]) return;
const tab = { ...tabs[currentTab] };
tab.postModeOnView = postMode.id;
if (!tab.posts[postMode.id]) {
tab.posts[postMode.id] = { text: '' };
}
setTabs({
...tabs,
[currentTab]: tab,
});
};
if (!currentTab || !tabs[currentTab]) {
return <div>No tabs available</div>;
}
return (
<div>
<Tabs
currentTab={tabs[currentTab]}
onChangeTab={changeCurrentTab}
tabs={tabs}
/>
<div className={scss.gridContainer}>
<div className={scss.postModesContainer}>
<PostModes
currentPostModeId={tabs[currentTab].postModeOnView}
currentTab={tabs[currentTab].account as Account}
onChangePostMode={changePostMode}
/>
<MainComposerBase
accountId={tabs[currentTab].account.id.toString()}
onChange={handleContentChange}
postMode={currentPostMode ?? undefined}
value={
tabs[currentTab].posts[tabs[currentTab].postModeOnView]?.text ??
''
}
/>
</div>
<div className={scss.previewContainer}>
<Preview>
{tabs[currentTab].posts[tabs[currentTab].postModeOnView]?.text}
</Preview>
</div>
</div>
</div>
);
}
export default Tabber;