|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { useLayoutEffect, useState } from 'react'; |
| 21 | +import { Toast, Button } from 'react-bootstrap'; |
| 22 | +import { useTranslation } from 'react-i18next'; |
| 23 | + |
| 24 | +import ReactDOM from 'react-dom/client'; |
| 25 | + |
| 26 | +import { EXTERNAL_CONTENT_DISPLAY_MODE } from '@/common/constants'; |
| 27 | +import { Storage } from '@/utils'; |
| 28 | + |
| 29 | +const toastPortal = document.createElement('div'); |
| 30 | +toastPortal.style.position = 'fixed'; |
| 31 | +toastPortal.style.top = '90px'; |
| 32 | +toastPortal.style.left = '50%'; |
| 33 | +toastPortal.style.transform = 'translate(-50%, 0)'; |
| 34 | +toastPortal.style.maxWidth = '100%'; |
| 35 | +toastPortal.style.zIndex = '1001'; |
| 36 | + |
| 37 | +const setPortalPosition = () => { |
| 38 | + const header = document.querySelector('#header'); |
| 39 | + if (header) { |
| 40 | + toastPortal.style.top = `${header.getBoundingClientRect().top + 90}px`; |
| 41 | + } |
| 42 | +}; |
| 43 | +const startHandlePortalPosition = () => { |
| 44 | + setPortalPosition(); |
| 45 | + window.addEventListener('scroll', setPortalPosition); |
| 46 | +}; |
| 47 | + |
| 48 | +const stopHandlePortalPosition = () => { |
| 49 | + setPortalPosition(); |
| 50 | + window.removeEventListener('scroll', setPortalPosition); |
| 51 | +}; |
| 52 | + |
| 53 | +const root = ReactDOM.createRoot(toastPortal); |
| 54 | + |
| 55 | +const useExternalToast = () => { |
| 56 | + const [show, setShow] = useState(false); |
| 57 | + const { t } = useTranslation('translation', { keyPrefix: 'messages' }); |
| 58 | + |
| 59 | + const onClose = () => { |
| 60 | + const parent = document.querySelector('.page-wrap'); |
| 61 | + if (parent?.contains(toastPortal)) { |
| 62 | + parent.removeChild(toastPortal); |
| 63 | + } |
| 64 | + stopHandlePortalPosition(); |
| 65 | + setShow(false); |
| 66 | + }; |
| 67 | + |
| 68 | + const onShow = () => { |
| 69 | + startHandlePortalPosition(); |
| 70 | + setShow(true); |
| 71 | + }; |
| 72 | + |
| 73 | + const showExternalResourceMode = (mode) => { |
| 74 | + if (mode === 'always') { |
| 75 | + Storage.set(EXTERNAL_CONTENT_DISPLAY_MODE, 'always'); |
| 76 | + } else { |
| 77 | + Storage.remove(EXTERNAL_CONTENT_DISPLAY_MODE); |
| 78 | + } |
| 79 | + const img = document.querySelectorAll('img'); |
| 80 | + img.forEach((i) => { |
| 81 | + if (!i.src && i.dataset.src) { |
| 82 | + i.src = i.dataset.src; |
| 83 | + i.removeAttribute('data-src'); |
| 84 | + i.classList.remove('broken'); |
| 85 | + } |
| 86 | + }); |
| 87 | + onClose(); |
| 88 | + }; |
| 89 | + |
| 90 | + useLayoutEffect(() => { |
| 91 | + const parent = document.querySelector('.page-wrap'); |
| 92 | + parent?.appendChild(toastPortal); |
| 93 | + |
| 94 | + root.render( |
| 95 | + <div className="d-flex justify-content-center"> |
| 96 | + <Toast |
| 97 | + className="align-items-center border-0" |
| 98 | + bg="warning" |
| 99 | + show={show} |
| 100 | + onClose={onClose}> |
| 101 | + <div className="d-flex"> |
| 102 | + <Toast.Body> |
| 103 | + {t('external_content_warning')} |
| 104 | + <div className="d-flex align-items-center"> |
| 105 | + <Button |
| 106 | + variant="link" |
| 107 | + onClick={() => showExternalResourceMode('below')} |
| 108 | + className="btn-no-border small link-dark p-0 fw-bold"> |
| 109 | + {t('display_below', { keyPrefix: 'btns' })} |
| 110 | + </Button> |
| 111 | + <span className="mx-1">{t('or', { keyPrefix: 'btns' })}</span> |
| 112 | + <Button |
| 113 | + variant="link" |
| 114 | + onClick={() => showExternalResourceMode('always')} |
| 115 | + className="btn-no-border small link-dark p-0 fw-bold"> |
| 116 | + {t('always_display', { keyPrefix: 'btns' })} |
| 117 | + </Button> |
| 118 | + </div> |
| 119 | + </Toast.Body> |
| 120 | + <button |
| 121 | + className="btn-close me-2 m-auto" |
| 122 | + onClick={onClose} |
| 123 | + data-bs-dismiss="toast" |
| 124 | + aria-label="Close" |
| 125 | + /> |
| 126 | + </div> |
| 127 | + </Toast> |
| 128 | + </div>, |
| 129 | + ); |
| 130 | + }, [show]); |
| 131 | + |
| 132 | + return { |
| 133 | + onShow, |
| 134 | + }; |
| 135 | +}; |
| 136 | + |
| 137 | +export default useExternalToast; |
0 commit comments