Skip to content

Commit c9d56f5

Browse files
authored
MMI-3381 CHES outage (#2531)
1 parent 2476711 commit c9d56f5

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

app/editor/src/components/layout/DefaultLayout.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { UnauthenticatedHome } from 'features/home';
22
import { UserInfo } from 'features/login';
33
import { Menu } from 'features/navbar';
44
import React from 'react';
5-
import { Link, Outlet, useSearchParams } from 'react-router-dom';
5+
import { Link, Outlet, useLocation, useSearchParams } from 'react-router-dom';
66
import { toast } from 'react-toastify';
77
import { IWorkOrderToast, useApiHub, useToastError } from 'store/hooks';
88
import {
@@ -50,10 +50,12 @@ const DefaultLayout: React.FC<ILayoutProps> = ({
5050
useToastError();
5151
const [searchParams] = useSearchParams({ showNav: 'true' });
5252
const { toggle: toggleSystemMessage, isShowing: showSystemMessage } = useModal();
53+
const location = useLocation();
5354

5455
const [toastIds, setToastIds] = React.useState<IWorkOrderToast[]>([]);
5556
const showNav = initShowNav ?? searchParams.get('showNav') === 'true';
5657
const [systemMessage, setSystemMessage] = React.useState<ISystemMessageModel>();
58+
const isReportInstanceView = /^\/report\/instances\/.*\/view$/.test(location.pathname);
5759

5860
React.useEffect(() => {
5961
keycloak.instance.onTokenExpired = () => {
@@ -148,13 +150,22 @@ const DefaultLayout: React.FC<ILayoutProps> = ({
148150
</LayoutErrorBoundary>
149151
</div>
150152
</Show>
151-
<Show visible={!keycloak.authenticated}>
153+
<Show visible={!keycloak.authenticated && !isReportInstanceView}>
152154
<div className="main-window">
153155
<main style={{ backgroundColor: '#f2f2f2' }}>
154156
<UnauthenticatedHome />
155157
</main>
156158
</div>
157159
</Show>
160+
<Show visible={!keycloak.authenticated && isReportInstanceView}>
161+
<div className="main-window">
162+
<LayoutErrorBoundary>
163+
<main>
164+
<Outlet />
165+
</main>
166+
</LayoutErrorBoundary>
167+
</div>
168+
</Show>
158169
<Modal
159170
headerText={systemMessage?.name ?? 'System Message'}
160171
body={systemMessage?.message}

app/editor/src/features/reports/ReportInstancePreview.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { FaPaperPlane } from 'react-icons/fa6';
33
import { useParams } from 'react-router-dom';
4-
import { useApp, useReportInstances, useReports } from 'store/hooks';
4+
import { useApp, useReportInstances, useReports, useSettings } from 'store/hooks';
55
import { useUsers } from 'store/hooks/admin';
66
import {
77
Button,
@@ -24,11 +24,14 @@ const ReportInstancePreview: React.FC = () => {
2424
const { id } = useParams();
2525
const instanceId = parseInt(id ?? '');
2626
const [{ userInfo }] = useApp();
27+
const { editorUrl, subscriberUrl } = useSettings();
2728

2829
const [isLoading, setIsLoading] = React.useState(true);
2930
const [view, setView] = React.useState<IReportResultModel | undefined>();
3031
const [report, setReport] = React.useState<IReportModel>();
3132

33+
console.error('ReportInstancePreview ');
34+
3235
const handlePreviewReport = React.useCallback(
3336
async (instanceId: number) => {
3437
try {
@@ -67,14 +70,21 @@ const ReportInstancePreview: React.FC = () => {
6770
(v) => v,
6871
);
6972

70-
const htmlBlob = new Blob([email.body], { type: 'text/html' });
71-
const textBlob = new Blob([email.body], { type: 'text/plain' });
73+
// Replace the URL so that it points to the external site.
74+
let fixed_body = email.body;
75+
if (editorUrl && subscriberUrl) {
76+
const urlReplaceRegex = new RegExp(editorUrl, 'gi');
77+
fixed_body = email.body.replace(urlReplaceRegex, subscriberUrl);
78+
}
79+
80+
const htmlBlob = new Blob([fixed_body], { type: 'text/html' });
81+
const textBlob = new Blob([fixed_body], { type: 'text/plain' });
7282
const clip = new ClipboardItem({ 'text/html': htmlBlob, 'text/plain': textBlob });
7383
navigator.clipboard.write([clip]);
7484
const bcc = subscribers.length ? `bcc=${emails.join('; ')}` : '';
7585
window.location.href = `mailto:${to}?${bcc}&subject=${email.subject}&body=Click Paste - Keep Source Formatting`;
7686
},
77-
[getDistributionListById],
87+
[editorUrl, getDistributionListById, subscriberUrl],
7888
);
7989

8090
React.useEffect(() => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useKeycloak } from '@react-keycloak/web';
2+
import React from 'react';
3+
import { useNavigate, useParams } from 'react-router-dom';
4+
5+
import ReportInstancePreview from './ReportInstancePreview';
6+
7+
/**
8+
* Temporary redirect component for report/instances view
9+
* Redirects unauthenticated users to external URL
10+
* Authenticated users are redirected to the normal view
11+
*/
12+
export const ReportInstancesRedirect: React.FC = () => {
13+
const { keycloak } = useKeycloak();
14+
const { id } = useParams<{ id: string }>();
15+
const navigate = useNavigate();
16+
17+
React.useEffect(() => {
18+
if (!keycloak?.authenticated) {
19+
// Redirect unauthenticated users to external URL
20+
// TODO: Replace EXTERNAL_URL_HERE with your actual external URL
21+
const externalUrl = `https://mmi.gov.bc.ca/report/instances/${id}/view`;
22+
window.location.href = externalUrl;
23+
} else {
24+
// Redirect authenticated users to the normal view
25+
navigate(`/report/instances/${id}/view`, { replace: true });
26+
}
27+
}, [keycloak?.authenticated, id, navigate]);
28+
29+
return <ReportInstancePreview />; // This component just handles the redirect
30+
};
31+
32+
export default ReportInstancesRedirect;

app/editor/src/features/reports/test.html

Whitespace-only changes.

app/editor/src/features/router/AppRouter.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AdminRouter } from 'features/admin';
2+
import ReportInstancesRedirect from 'features/reports/ReportInstancesRedirect';
23
import React, { lazy, Suspense } from 'react';
34
import { Navigate, Route, Routes, useNavigate } from 'react-router-dom';
45
import { useApp } from 'store/hooks';
@@ -40,8 +41,10 @@ export const AppRouter: React.FC<IAppRouter> = ({ name }) => {
4041
React.useEffect(() => {
4142
// There is a race condition, when keycloak is ready state related to user claims will not be.
4243
// Additionally, when the user is not authenticated keycloak also is not initialized (which makes no sense).
43-
if (!authenticated && !window.location.pathname.startsWith('/login'))
44+
const isReportInstanceView = /^\/report\/instances\/.*\/view$/.test(window.location.pathname);
45+
if (!authenticated && !window.location.pathname.startsWith('/login') && !isReportInstanceView) {
4446
navigate(`/login?redirectTo=${window.location.pathname}`);
47+
}
4548
}, [authenticated, navigate]);
4649

4750
return (
@@ -125,6 +128,9 @@ export const AppRouter: React.FC<IAppRouter> = ({ name }) => {
125128
<Route path="clips" element={<RequestClip />} />
126129
<Route path="transcriptions" element={<TranscriptionList />} />
127130

131+
{/* Temporary redirect for unauthenticated users accessing report instances */}
132+
<Route path="report/instances/:id/view" element={<ReportInstancesRedirect />} />
133+
128134
<Route
129135
path="report/instances/:id/view"
130136
element={

0 commit comments

Comments
 (0)