PageGrantAlert.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React from 'react';
  2. import { isPopulated } from '@growi/core';
  3. import { useTranslation } from 'react-i18next';
  4. import { useSWRxCurrentPage } from '~/stores/page';
  5. export const PageGrantAlert = (): JSX.Element => {
  6. const { t } = useTranslation();
  7. const { data: pageData } = useSWRxCurrentPage();
  8. if (pageData == null || pageData.grant == null || pageData.grant === 1) {
  9. return <></>;
  10. }
  11. const populatedGrantedGroups = () => {
  12. return pageData.grantedGroups.filter(group => isPopulated(group.item));
  13. };
  14. const renderAlertContent = () => {
  15. const getGrantLabel = () => {
  16. if (pageData.grant === 2) {
  17. return (
  18. <>
  19. <span className="material-symbols-outlined me-1">link</span><strong>{t('Anyone with the link')}</strong>
  20. </>
  21. );
  22. }
  23. if (pageData.grant === 4) {
  24. return (
  25. <>
  26. <span className="material-symbols-outlined me-1">lock</span><strong>{t('Only me')}</strong>
  27. </>
  28. );
  29. }
  30. if (pageData.grant === 5) {
  31. return (
  32. <>
  33. <span className="material-symbols-outlined me-1">account_tree</span>
  34. <strong>{
  35. populatedGrantedGroups().map(g => g.item.name).join(', ')
  36. }
  37. </strong>
  38. </>
  39. );
  40. }
  41. };
  42. return (
  43. <>
  44. {getGrantLabel()} ({t('Browsing of this page is restricted')})
  45. </>
  46. );
  47. };
  48. return (
  49. <p className="alert alert-primary py-3 px-4">
  50. {renderAlertContent()}
  51. </p>
  52. );
  53. };