File size: 4,133 Bytes
ec4551b | 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 | 'use client';
import { ReactNode, useCallback } from 'react';
import { FetchWrapperComponent } from '@gitroom/helpers/utils/custom.fetch';
import { deleteDialog } from '@gitroom/react/helpers/delete.dialog';
import { useReturnUrl } from '@gitroom/frontend/app/(app)/auth/return.url.component';
import { useVariables } from '@gitroom/react/helpers/variable.context';
export default function LayoutContext(params: { children: ReactNode }) {
if (params?.children) {
// eslint-disable-next-line react/no-children-prop
return <LayoutContextInner children={params.children} />;
}
return <></>;
}
export function setCookie(cname: string, cvalue: string, exdays: number) {
if (typeof document === 'undefined') {
return;
}
const d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
const expires = 'expires=' + d.toUTCString();
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
function LayoutContextInner(params: { children: ReactNode }) {
const returnUrl = useReturnUrl();
const { backendUrl, isGeneral, isSecured } = useVariables();
const afterRequest = useCallback(
async (url: string, options: RequestInit, response: Response) => {
if (
typeof window !== 'undefined' &&
(window.location.href.includes('/p/') ||
window.location.pathname.startsWith('/provider/'))
) {
return true;
}
const headerAuth =
response?.headers?.get('auth') || response?.headers?.get('Auth');
const showOrg =
response?.headers?.get('showorg') || response?.headers?.get('Showorg');
const impersonate =
response?.headers?.get('impersonate') ||
response?.headers?.get('Impersonate');
const logout =
response?.headers?.get('logout') || response?.headers?.get('Logout');
if (headerAuth) {
setCookie('auth', headerAuth, 365);
}
if (showOrg) {
setCookie('showorg', showOrg, 365);
}
if (impersonate) {
setCookie('impersonate', impersonate, 365);
}
if (logout && !isSecured) {
setCookie('auth', '', -10);
setCookie('showorg', '', -10);
setCookie('impersonate', '', -10);
window.location.href = '/';
return true;
}
const reloadOrOnboarding =
response?.headers?.get('reload') ||
response?.headers?.get('onboarding');
if (reloadOrOnboarding) {
const getAndClear = returnUrl.getAndClear();
if (getAndClear) {
window.location.href = getAndClear;
return true;
}
}
if (response?.headers?.get('onboarding')) {
window.location.href = isGeneral
? '/launches?onboarding=true'
: '/analytics?onboarding=true';
return true;
}
if (response?.headers?.get('reload')) {
window.location.reload();
return true;
}
if (response.status === 401 || response?.headers?.get('logout')) {
if (!isSecured) {
setCookie('auth', '', -10);
setCookie('showorg', '', -10);
setCookie('impersonate', '', -10);
}
window.location.href = '/';
}
if (response.status === 406) {
if (
await deleteDialog(
'You are currently on trial, in order to use the feature you must finish the trial',
'Finish the trial, charge me now',
'Trial',
)
) {
window.open('/billing?finishTrial=true', '_blank');
return false;
}
return false;
}
if (response.status === 402) {
if (
await deleteDialog(
(
await response.json()
).message,
'Move to billing',
'Payment Required'
)
) {
window.open('/billing', '_blank');
return false;
}
return true;
}
return true;
},
[]
);
return (
<FetchWrapperComponent baseUrl={backendUrl} afterRequest={afterRequest}>
{params?.children || <></>}
</FetchWrapperComponent>
);
}
|