refactor: remove `rootUrlChange` from meteor (#35945)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>ci/docker-build-logs
parent
3a1b59237f
commit
e665a7e24e
@ -0,0 +1,98 @@ |
||||
import { useEndpoint, useRole, useSetModal, useSetting, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; |
||||
import { useMutation } from '@tanstack/react-query'; |
||||
import { useEffect, useReducer } from 'react'; |
||||
import { useTranslation } from 'react-i18next'; |
||||
|
||||
import FingerprintChangeModal from '../components/FingerprintChangeModal'; |
||||
import FingerprintChangeModalConfirmation from '../components/FingerprintChangeModalConfirmation'; |
||||
|
||||
const reducer = ( |
||||
state: { openModal: boolean; openConfirmation: boolean; newWorkspace?: boolean }, |
||||
action: { type: 'openModal' | 'openConfirmation' | 'closeModal'; newWorkspace?: boolean }, |
||||
) => { |
||||
switch (action.type) { |
||||
case 'openModal': |
||||
return { openModal: true, openConfirmation: false, newWorkspace: undefined }; |
||||
case 'openConfirmation': |
||||
return { openModal: false, openConfirmation: true, newWorkspace: action.newWorkspace }; |
||||
case 'closeModal': |
||||
return { openModal: false, openConfirmation: false, newWorkspace: undefined }; |
||||
default: |
||||
return state; |
||||
} |
||||
}; |
||||
|
||||
export const useFingerprintChange = () => { |
||||
const { t } = useTranslation(); |
||||
const dispatchToastMessage = useToastMessageDispatch(); |
||||
const isAdmin = useRole('admin'); |
||||
const setModal = useSetModal(); |
||||
const deploymentFingerPrintVerified = useSetting('Deployment_FingerPrint_Verified', true); |
||||
const fingerprintEndpoint = useEndpoint('POST', '/v1/fingerprint'); |
||||
|
||||
const [{ openConfirmation, openModal, newWorkspace }, dispatch] = useReducer(reducer, { |
||||
openModal: false, |
||||
openConfirmation: false, |
||||
newWorkspace: undefined, |
||||
}); |
||||
|
||||
const { mutate: fingerPrintMutation } = useMutation({ |
||||
mutationKey: ['settings', 'Deployment_FingerPrint_Verified'], |
||||
mutationFn: async (setDeploymentAs: 'new-workspace' | 'updated-configuration') => { |
||||
const result = await fingerprintEndpoint({ setDeploymentAs }); |
||||
return { |
||||
...result, |
||||
setDeploymentAs, |
||||
}; |
||||
}, |
||||
onSuccess: ({ setDeploymentAs }) => { |
||||
if (setDeploymentAs === 'new-workspace') { |
||||
return dispatchToastMessage({ type: 'success', message: t('New_workspace_confirmed') }); |
||||
} |
||||
return dispatchToastMessage({ type: 'success', message: t('Configuration_update_confirmed') }); |
||||
}, |
||||
}); |
||||
|
||||
useEffect(() => { |
||||
if (!isAdmin) { |
||||
return; |
||||
} |
||||
if (deploymentFingerPrintVerified === null || deploymentFingerPrintVerified === true) { |
||||
return; |
||||
} |
||||
dispatch({ type: 'openModal' }); |
||||
|
||||
return () => { |
||||
dispatch({ type: 'closeModal' }); |
||||
}; |
||||
}, [deploymentFingerPrintVerified, isAdmin]); |
||||
|
||||
useEffect(() => { |
||||
if (!openModal && !openConfirmation) { |
||||
setModal(null); |
||||
} |
||||
if (openModal) { |
||||
setModal( |
||||
<FingerprintChangeModal |
||||
onConfirm={() => dispatch({ type: 'openConfirmation', newWorkspace: true })} |
||||
onCancel={() => dispatch({ type: 'openConfirmation', newWorkspace: false })} |
||||
onClose={() => dispatch({ type: 'closeModal' })} |
||||
/>, |
||||
); |
||||
} |
||||
|
||||
if (openConfirmation && newWorkspace !== undefined) { |
||||
setModal( |
||||
<FingerprintChangeModalConfirmation |
||||
onConfirm={() => { |
||||
fingerPrintMutation(newWorkspace ? 'new-workspace' : 'updated-configuration'); |
||||
dispatch({ type: 'closeModal' }); |
||||
}} |
||||
onCancel={() => dispatch({ type: 'openModal' })} |
||||
onClose={() => dispatch({ type: 'closeModal' })} |
||||
newWorkspace={newWorkspace} |
||||
/>, |
||||
); |
||||
} |
||||
}, [fingerPrintMutation, setModal, openConfirmation, openModal, newWorkspace]); |
||||
}; |
||||
@ -0,0 +1,61 @@ |
||||
import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; |
||||
import { useRole, useSetModal, useSetting, useSettingSetValue, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; |
||||
import { useMutation } from '@tanstack/react-query'; |
||||
import { useEffect } from 'react'; |
||||
import { useTranslation } from 'react-i18next'; |
||||
|
||||
import UrlChangeModal from '../components/UrlChangeModal'; |
||||
|
||||
export const useRootUrlChange = () => { |
||||
const { t } = useTranslation(); |
||||
const dispatchToastMessage = useToastMessageDispatch(); |
||||
const isAdmin = useRole('admin'); |
||||
const setModal = useSetModal(); |
||||
const closeModal = useEffectEvent(() => setModal(null)); |
||||
|
||||
const currentUrl = location.origin + window.__meteor_runtime_config__.ROOT_URL_PATH_PREFIX; |
||||
const siteUrl = useSetting('Site_Url', ''); |
||||
const documentDomain = useSetting('Document_Domain', ''); |
||||
const setSiteUrl = useSettingSetValue('Site_Url'); |
||||
|
||||
const { |
||||
mutate: siteUrlMutation, |
||||
isPending, |
||||
isSuccess, |
||||
} = useMutation({ |
||||
mutationKey: ['settings', 'Site_Url'], |
||||
mutationFn: async (url: string) => { |
||||
await setSiteUrl(url); |
||||
return { url }; |
||||
}, |
||||
onSuccess: ({ url }) => dispatchToastMessage({ type: 'success', message: t('Saved_new_url_site_is__url__', { url }) }), |
||||
onError: () => dispatchToastMessage({ type: 'error', message: t('Something_went_wrong') }), |
||||
}); |
||||
|
||||
useEffect(() => { |
||||
if (!isAdmin) { |
||||
return; |
||||
} |
||||
if (!siteUrl) { |
||||
return; |
||||
} |
||||
if (isPending || isSuccess) { |
||||
return; |
||||
} |
||||
if (window.__meteor_runtime_config__.ROOT_URL.replace(/\/$/, '') === currentUrl) { |
||||
return; |
||||
} |
||||
const onConfirm = () => { |
||||
closeModal(); |
||||
siteUrlMutation(currentUrl); |
||||
}; |
||||
|
||||
setModal(<UrlChangeModal onClose={closeModal} onConfirm={onConfirm} siteUrl={siteUrl} currentUrl={currentUrl} />); |
||||
|
||||
if (documentDomain) { |
||||
window.document.domain = documentDomain; |
||||
} |
||||
|
||||
return closeModal; |
||||
}, [currentUrl, documentDomain, siteUrlMutation, siteUrl, isAdmin, isPending, isSuccess, setModal, closeModal]); |
||||
}; |
||||
@ -0,0 +1,16 @@ |
||||
import { useSetModal } from '@rocket.chat/ui-contexts'; |
||||
import { useEffect } from 'react'; |
||||
|
||||
import { useRequire2faSetup } from '../views/hooks/useRequire2faSetup'; |
||||
import TwoFactorRequiredModal from '../views/root/MainLayout/TwoFactorRequiredModal'; |
||||
|
||||
export const useTwoFactorAuthSetupCheck = () => { |
||||
const setModal = useSetModal(); |
||||
const require2faSetup = useRequire2faSetup(); |
||||
|
||||
useEffect(() => { |
||||
if (require2faSetup) { |
||||
setModal(<TwoFactorRequiredModal />); |
||||
} |
||||
}, [setModal, require2faSetup]); |
||||
}; |
||||
@ -1,131 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { Tracker } from 'meteor/tracker'; |
||||
|
||||
import { hasRole } from '../../app/authorization/client'; |
||||
import { Roles } from '../../app/models/client'; |
||||
import { settings } from '../../app/settings/client'; |
||||
import { sdk } from '../../app/utils/client/lib/SDKClient'; |
||||
import { t } from '../../app/utils/lib/i18n'; |
||||
import FingerprintChangeModal from '../components/FingerprintChangeModal'; |
||||
import FingerprintChangeModalConfirmation from '../components/FingerprintChangeModalConfirmation'; |
||||
import UrlChangeModal from '../components/UrlChangeModal'; |
||||
import { imperativeModal } from '../lib/imperativeModal'; |
||||
import { dispatchToastMessage } from '../lib/toast'; |
||||
import { isSyncReady } from '../lib/userData'; |
||||
|
||||
Meteor.startup(() => { |
||||
Tracker.autorun((c) => { |
||||
const userId = Meteor.userId(); |
||||
if (!userId) { |
||||
return; |
||||
} |
||||
|
||||
if (!Roles.ready.get() || !isSyncReady.get()) { |
||||
return; |
||||
} |
||||
|
||||
if (hasRole(userId, 'admin') === false) { |
||||
return c.stop(); |
||||
} |
||||
|
||||
const siteUrl = settings.get('Site_Url'); |
||||
if (!siteUrl) { |
||||
return; |
||||
} |
||||
|
||||
const currentUrl = location.origin + window.__meteor_runtime_config__.ROOT_URL_PATH_PREFIX; |
||||
if (window.__meteor_runtime_config__.ROOT_URL.replace(/\/$/, '') !== currentUrl) { |
||||
const confirm = (): void => { |
||||
imperativeModal.close(); |
||||
void sdk.call('saveSetting', 'Site_Url', currentUrl).then(() => { |
||||
dispatchToastMessage({ type: 'success', message: t('Saved') }); |
||||
}); |
||||
}; |
||||
imperativeModal.open({ |
||||
component: UrlChangeModal, |
||||
props: { |
||||
onConfirm: confirm, |
||||
siteUrl, |
||||
currentUrl, |
||||
onClose: imperativeModal.close, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
const documentDomain = settings.get('Document_Domain'); |
||||
if (documentDomain) { |
||||
window.document.domain = documentDomain; |
||||
} |
||||
|
||||
return c.stop(); |
||||
}); |
||||
}); |
||||
|
||||
Meteor.startup(() => { |
||||
Tracker.autorun((c) => { |
||||
const userId = Meteor.userId(); |
||||
if (!userId) { |
||||
return; |
||||
} |
||||
|
||||
if (!Roles.ready.get() || !isSyncReady.get()) { |
||||
return; |
||||
} |
||||
|
||||
if (hasRole(userId, 'admin') === false) { |
||||
return c.stop(); |
||||
} |
||||
|
||||
const deploymentFingerPrintVerified = settings.get('Deployment_FingerPrint_Verified'); |
||||
if (deploymentFingerPrintVerified == null || deploymentFingerPrintVerified === true) { |
||||
return; |
||||
} |
||||
|
||||
const updateWorkspace = (): void => { |
||||
imperativeModal.close(); |
||||
void sdk.rest.post('/v1/fingerprint', { setDeploymentAs: 'updated-configuration' }).then(() => { |
||||
dispatchToastMessage({ type: 'success', message: t('Configuration_update_confirmed') }); |
||||
}); |
||||
}; |
||||
|
||||
const setNewWorkspace = (): void => { |
||||
imperativeModal.close(); |
||||
void sdk.rest.post('/v1/fingerprint', { setDeploymentAs: 'new-workspace' }).then(() => { |
||||
dispatchToastMessage({ type: 'success', message: t('New_workspace_confirmed') }); |
||||
}); |
||||
}; |
||||
|
||||
const openModal = (): void => { |
||||
imperativeModal.open({ |
||||
component: FingerprintChangeModal, |
||||
props: { |
||||
onConfirm: () => { |
||||
imperativeModal.open({ |
||||
component: FingerprintChangeModalConfirmation, |
||||
props: { |
||||
onConfirm: setNewWorkspace, |
||||
onCancel: openModal, |
||||
newWorkspace: true, |
||||
}, |
||||
}); |
||||
}, |
||||
onCancel: () => { |
||||
imperativeModal.open({ |
||||
component: FingerprintChangeModalConfirmation, |
||||
props: { |
||||
onConfirm: updateWorkspace, |
||||
onCancel: openModal, |
||||
newWorkspace: false, |
||||
}, |
||||
}); |
||||
}, |
||||
onClose: imperativeModal.close, |
||||
}, |
||||
}); |
||||
}; |
||||
|
||||
openModal(); |
||||
|
||||
return c.stop(); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue