Skip to content

Commit

Permalink
Use XP_REQUEST_MODE constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Dec 11, 2024
1 parent 23847a1 commit 108547c
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/Common/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {ReactNode} from 'react';

import { ErrorComponent } from './ErrorComponent';
import { Warning } from './Warning';
import { XP_REQUEST_MODE } from '../constants';


export const Message = ({
Expand All @@ -18,18 +19,18 @@ export const Message = ({
}): JSX.Element | null => {

// Log, but don't render errors in live and preview mode.
if (mode === 'live' || mode === 'preview') {
if (mode === XP_REQUEST_MODE.LIVE || mode === XP_REQUEST_MODE.PREVIEW) {
console.error(children);
return null;
}

if (mode === 'inline') {
if (mode === XP_REQUEST_MODE.INLINE) {
return (
<Warning {...extraProps} children={children}/>
);
}

if (mode === 'edit') {
if (mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.ADMIN) {
return (
<ErrorComponent {...extraProps} children={children}/>
);
Expand Down
3 changes: 2 additions & 1 deletion src/Common/TryCatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {ReactNode} from 'react';

import * as React from 'react';
import { Message } from './Message';
import { XP_REQUEST_MODE } from '../constants';

export const TryCatch = ({
children,
Expand All @@ -22,7 +23,7 @@ export const TryCatch = ({
<h2>Error rendering component</h2>
<p>{e.message}</p>
{
mode === 'edit' && (
mode === XP_REQUEST_MODE.EDIT && (
<pre>{e.stack || ''}</pre>
)
}
Expand Down
9 changes: 6 additions & 3 deletions src/ComponentRegistry/BaseComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type {RenderableComponent} from '../types';
import { toStr } from '@enonic/js-utils/value/toStr';
import * as React from 'react';

import {RENDERABLE_COMPONENT_TYPE} from '../constants';
import {
RENDERABLE_COMPONENT_TYPE,
XP_REQUEST_MODE,
} from '../constants';
import {ErrorComponent} from '../Common/ErrorComponent';
import {Warning} from '../Common/Warning';
import {BaseLayout} from './BaseLayout';
Expand Down Expand Up @@ -85,7 +88,7 @@ export function BaseComponent({
html,
mode
} = component;
if (mode === 'live') {
if (mode === XP_REQUEST_MODE.LIVE) {
return null;
}
return (
Expand All @@ -97,7 +100,7 @@ export function BaseComponent({
html,
mode
} = component;
if (mode === 'live') {
if (mode === XP_REQUEST_MODE.LIVE) {
return null;
}
return (
Expand Down
3 changes: 2 additions & 1 deletion src/ComponentRegistry/BaseContentType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {

import * as React from 'react';
import { Message } from '../Common/Message';
// import { XP_REQUEST_MODE } from '../constants';

export const BaseContentType = ({
component,
Expand All @@ -22,7 +23,7 @@ export const BaseContentType = ({
// warning,
} = component;

// if (warning && (mode === 'edit' || mode === 'inline')) {
// if (warning && (mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.INLINE || mode === XP_REQUEST_MODE.ADMIN)) {
// return (
// <Message mode={mode}>{warning}</Message>
// );
Expand Down
5 changes: 3 additions & 2 deletions src/ComponentRegistry/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {

import * as React from 'react';
import { Message } from '../Common/Message';
import { XP_REQUEST_MODE } from '../constants';

export function BaseLayout({
component,
Expand All @@ -21,9 +22,9 @@ export function BaseLayout({
warning,
} = component;

const dataPortalComponentType = mode === 'edit' ? 'layout' : undefined;
const dataPortalComponentType = mode === XP_REQUEST_MODE.EDIT ? 'layout' : undefined;

if (warning && (mode === 'edit' || mode === 'inline')) {
if (warning && (mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.INLINE || mode === XP_REQUEST_MODE.ADMIN)) {
return (
<Message {...{
'data-portal-component-type': dataPortalComponentType,
Expand Down
5 changes: 3 additions & 2 deletions src/ComponentRegistry/BasePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
import * as React from 'react';
import { Message } from '../Common/Message';
import { ErrorComponent } from '../Common/ErrorComponent';
import { XP_REQUEST_MODE } from '../constants';

export function BasePage({
component,
Expand All @@ -23,7 +24,7 @@ export function BasePage({
warning,
} = component;

const dataPortalComponentType = mode === 'edit' ? 'page' : undefined;
const dataPortalComponentType = mode === XP_REQUEST_MODE.EDIT ? 'page' : undefined;

if (error && (mode === 'inline' || mode === 'preview')) { // In edit mode the error should be handeled by Content Studio.
return (
Expand All @@ -34,7 +35,7 @@ export function BasePage({
);
}

if (warning && (mode === 'inline' || mode === 'edit')) {
if (warning && (mode === XP_REQUEST_MODE.INLINE || mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.ADMIN)) {
return (
<Message {...{
children: warning,
Expand Down
5 changes: 3 additions & 2 deletions src/ComponentRegistry/BasePart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
// import { toStr } from '@enonic/js-utils/value/toStr';
import * as React from 'react';
import { Message } from '../Common/Message';
import { XP_REQUEST_MODE } from '../constants';

export function BasePart({
component,
Expand All @@ -22,9 +23,9 @@ export function BasePart({
warning,
} = component;

const dataPortalComponentType = mode === 'edit' ? 'part' : undefined;
const dataPortalComponentType = mode === XP_REQUEST_MODE.EDIT ? 'part' : undefined;

if (warning && (mode === 'edit' || mode === 'inline')) {
if (warning && (mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.INLINE || mode === XP_REQUEST_MODE.ADMIN)) {
return (
<Message {...{
children: warning,
Expand Down
5 changes: 3 additions & 2 deletions src/ComponentRegistry/BaseText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as React from 'react';
import { Message } from '../Common/Message';
import { XpFallback } from './XpFallback';
import { Text } from './Text';
import { XP_REQUEST_MODE } from '../constants';

export const BaseText = ({
component,
Expand All @@ -22,10 +23,10 @@ export const BaseText = ({
props
} = component;

const dataPortalComponentType = mode === 'edit' ? 'text' : undefined;
const dataPortalComponentType = mode === XP_REQUEST_MODE.EDIT ? 'text' : undefined;

if (!props) {
if (mode === 'edit' || mode === 'inline') {
if (mode === XP_REQUEST_MODE.EDIT || mode === XP_REQUEST_MODE.INLINE || mode === XP_REQUEST_MODE.ADMIN) {
return (
<Message {...{
'data-portal-component-type': dataPortalComponentType,
Expand Down
3 changes: 2 additions & 1 deletion src/RichText/ErrorBoundary/ErrorBoundaryServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
} from '@enonic-types/core';
import * as React from 'react';
import { Message } from '../../Common/Message';
import { XP_REQUEST_MODE } from '../../constants';

// ErrorBoundaries are not supported on server in Next.js
export function ErrorBoundaryServer({
Expand All @@ -21,7 +22,7 @@ export function ErrorBoundaryServer({
<h2>Error rendering component</h2>
<p>{e.message}</p>
{
mode === 'edit' && (
mode === XP_REQUEST_MODE.EDIT && (
<pre>{e.stack || ''}</pre>
)
}
Expand Down
6 changes: 2 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
CSSProperties,
ReactNode,
} from 'react';
import type { CSSProperties } from 'react';

export const IMG_TAG = 'img';
export const LINK_TAG = 'a';
Expand All @@ -23,6 +20,7 @@ export enum RENDERABLE_COMPONENT_TYPE {
}

export enum XP_REQUEST_MODE {
ADMIN = 'admin',
EDIT = 'edit',
INLINE = 'inline',
LIVE = 'live',
Expand Down

0 comments on commit 108547c

Please sign in to comment.