-
Notifications
You must be signed in to change notification settings - Fork 125
/
SmBreadcrumbs.jsx
156 lines (143 loc) · 4.32 KB
/
SmBreadcrumbs.jsx
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { useEffect, useMemo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useHistory, useLocation } from 'react-router-dom';
import { setBreadcrumbs } from '../../actions/breadcrumbs';
import * as Constants from '../../util/constants';
import { retrieveFolder } from '../../actions/folders';
import { navigateToFolderByFolderId } from '../../util/helpers';
const SmBreadcrumbs = () => {
const dispatch = useDispatch();
const location = useLocation();
const history = useHistory();
const threadDetails = useSelector(state => state.sm.threadDetails);
const messageDetails = threadDetails.messages?.length
? threadDetails.messages[0]
: null;
const activeFolder = useSelector(state => state.sm.folders.folder);
const crumbs = useSelector(state => state.sm.breadcrumbs.list);
const [isMobile, setIsMobile] = useState(false);
function checkScreenSize() {
if (window.innerWidth <= 768 && setIsMobile !== false) {
setIsMobile(true);
} else {
setIsMobile(false);
}
}
const [locationBasePath, locationChildPath] = useMemo(
() => {
const pathElements = location.pathname.split('/');
if (pathElements[0] === '') pathElements.shift();
return pathElements;
},
[location],
);
useEffect(
() => {
checkScreenSize();
},
[isMobile],
);
useEffect(
() => {
if (
`/${locationBasePath}/` === Constants.Paths.FOLDERS &&
parseInt(locationChildPath, 10) < 1
) {
navigateToFolderByFolderId(locationChildPath, history);
}
},
[locationBasePath, locationChildPath, history],
);
window.addEventListener('resize', checkScreenSize);
useEffect(
() => {
if (!locationBasePath) {
dispatch(setBreadcrumbs({}, location));
return;
}
const path = `/${locationBasePath}/`;
if (
[
Constants.Paths.INBOX,
Constants.Paths.SENT,
Constants.Paths.DELETED,
Constants.Paths.DRAFTS,
].includes(path) ||
(path === Constants.Paths.FOLDERS && !locationChildPath)
) {
dispatch(setBreadcrumbs(Constants.Breadcrumbs.MESSAGES, location));
} else if (path === Constants.Paths.FOLDERS && locationChildPath) {
dispatch(setBreadcrumbs(Constants.Breadcrumbs.FOLDERS, location));
} else if (path === Constants.Paths.COMPOSE) {
dispatch(setBreadcrumbs(Constants.Breadcrumbs.INBOX, location));
} else if (
path ===
(Constants.Paths.MESSAGE_THREAD ||
Constants.Paths.REPLY ||
Constants.Paths.COMPOSE) &&
activeFolder
) {
dispatch(
setBreadcrumbs(
{
path: `${Constants.Paths.FOLDERS}${activeFolder.folderId}`,
label: `Back to ${
activeFolder.folderId < 1
? activeFolder.name.toLowerCase()
: activeFolder.name
}`,
},
location,
),
);
}
},
[
activeFolder,
dispatch,
location,
locationBasePath,
locationChildPath,
messageDetails?.subject,
],
);
useEffect(
() => {
if (messageDetails && !activeFolder) {
dispatch(retrieveFolder(messageDetails?.threadFolderId));
}
},
[messageDetails, activeFolder, dispatch],
);
const breadcrumbSize = () => {
if (isMobile) {
return Constants.BreadcrumbViews.MOBILE_VIEW;
}
return Constants.BreadcrumbViews.DESKTOP_VIEW;
};
return (
<div
className={`breadcrumbs vads-1-row ${
!crumbs?.label ? 'breadcrumbs--hidden' : ''
}`}
>
<va-breadcrumbs
uswds="false"
label="Breadcrumb"
home-veterans-affairs={false}
>
{crumbs && (
<ul className={breadcrumbSize()}>
<li>
<span className="breadcrumb-angle vads-u-padding-right--1">
{'\u2039'}{' '}
</span>
<Link to={crumbs.path?.toLowerCase()}>{crumbs.label}</Link>
</li>
</ul>
)}
</va-breadcrumbs>
</div>
);
};
export default SmBreadcrumbs;