|
| 1 | +import React, { type ReactNode } from 'react'; |
| 2 | +import { ThemeClassNames } from '@docusaurus/theme-common'; |
| 3 | +import Link from '@docusaurus/Link'; |
| 4 | +import styles from './styles.module.scss'; |
| 5 | +import siteConfig from '@generated/docusaurus.config'; |
| 6 | +import type { Props } from '@theme/EditThisPage'; |
| 7 | +import Icon from '@mdi/react'; |
| 8 | +import { mdiGithub, mdiInfinity, mdiMicrosoftVisualStudioCode } from '@mdi/js'; |
| 9 | +import clsx from 'clsx'; |
| 10 | +import { observer } from 'mobx-react-lite'; |
| 11 | +import { useStore } from '@tdev-hooks/useStore'; |
| 12 | +import { useLocation } from '@docusaurus/router'; |
| 13 | +import type { EditThisPageOption, ShowEditThisPage } from '@tdev/siteConfig/siteConfig'; |
| 14 | +const { organizationName, projectName, customFields } = siteConfig; |
| 15 | +const { showEditThisPage, showEditThisPageOptions, editThisPageCmsUrl } = customFields as { |
| 16 | + showEditThisPage: ShowEditThisPage; |
| 17 | + showEditThisPageOptions: EditThisPageOption[]; |
| 18 | + editThisPageCmsUrl: string; |
| 19 | +}; |
| 20 | +const DisplayBadgeFor = new Set<EditThisPageOption>( |
| 21 | + showEditThisPageOptions.length === 0 ? ['github', 'github-dev', 'cms'] : showEditThisPageOptions |
| 22 | +); |
| 23 | +const GH_EDIT_URL = `https://github.com/${organizationName}/${projectName}/edit/main/`; |
| 24 | +const GH_DEV_EDIT_URL = `https://github.dev/${organizationName}/${projectName}/blob/main/`; |
| 25 | +const CMS_EDIT_URL = `${editThisPageCmsUrl}${organizationName}/${projectName}/`; |
| 26 | + |
| 27 | +const EditThisPage = observer(({ editUrl }: Props): ReactNode => { |
| 28 | + const userStore = useStore('userStore'); |
| 29 | + const location = useLocation(); |
| 30 | + const search = new URLSearchParams(location.search); |
| 31 | + if (!editUrl || search.has('edit')) { |
| 32 | + return; |
| 33 | + } |
| 34 | + const isLoggedIn = !!userStore.current; |
| 35 | + switch (showEditThisPage) { |
| 36 | + case 'always': |
| 37 | + break; |
| 38 | + case 'never': |
| 39 | + return null; |
| 40 | + case 'loggedIn': |
| 41 | + if (!isLoggedIn) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + break; |
| 45 | + case 'teachers': |
| 46 | + if (!userStore.current?.hasElevatedAccess) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + break; |
| 50 | + case 'admins': |
| 51 | + if (!userStore.current?.isAdmin) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + break; |
| 55 | + default: |
| 56 | + console.warn(`Unknown value for showEditThisPage: ${showEditThisPage}`); |
| 57 | + return null; |
| 58 | + } |
| 59 | + return ( |
| 60 | + <div className={clsx(styles.editThisPage)}> |
| 61 | + {DisplayBadgeFor.has('github') && ( |
| 62 | + <Link |
| 63 | + to={`${GH_EDIT_URL}${editUrl}`} |
| 64 | + className={clsx(ThemeClassNames.common.editThisPage, styles.edit)} |
| 65 | + title="Auf GitHub bearbeiten." |
| 66 | + > |
| 67 | + <Icon path={mdiGithub} size={0.7} /> |
| 68 | + Github |
| 69 | + </Link> |
| 70 | + )} |
| 71 | + {DisplayBadgeFor.has('github-dev') && ( |
| 72 | + <Link |
| 73 | + to={`${GH_DEV_EDIT_URL}${editUrl}`} |
| 74 | + className={clsx(ThemeClassNames.common.editThisPage, styles.edit)} |
| 75 | + title="Auf GitHub.dev mit Web-VSCode bearbeiten." |
| 76 | + > |
| 77 | + <Icon path={mdiMicrosoftVisualStudioCode} size={0.7} /> |
| 78 | + .dev |
| 79 | + </Link> |
| 80 | + )} |
| 81 | + {DisplayBadgeFor.has('cms') && ( |
| 82 | + <Link |
| 83 | + to={`${CMS_EDIT_URL}${editUrl}`} |
| 84 | + className={clsx( |
| 85 | + ThemeClassNames.common.editThisPage, |
| 86 | + styles.edit, |
| 87 | + !isLoggedIn && styles.noUser |
| 88 | + )} |
| 89 | + title={ |
| 90 | + isLoggedIn |
| 91 | + ? 'Im tdev-CMS bearbeiten (Vorschau).' |
| 92 | + : 'Im tdev-CMS bearbeiten (Vorschau, Anmeldung erforderlich).' |
| 93 | + } |
| 94 | + > |
| 95 | + <Icon path={mdiInfinity} size={0.7} className={clsx(styles.cms)} /> |
| 96 | + cms |
| 97 | + </Link> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + ); |
| 101 | +}); |
| 102 | + |
| 103 | +export default EditThisPage; |
0 commit comments