Skip to content

Commit ed5371b

Browse files
authored
Merge pull request #746 from XYOracleNetwork/feature/plugin-props-context
plugin props context
2 parents 4228172 + 95b8029 commit ed5371b

File tree

15 files changed

+96
-10
lines changed

15 files changed

+96
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@xylabs/tsconfig-dom": "~7.1.8",
8383
"@xylabs/tsconfig-react": "~7.1.8",
8484
"@xyo-network/hash": "~5.1.15",
85+
"axios": "^1.13.2",
8586
"chromatic": "~13.3.2",
8687
"copyfiles": "~2.4.1",
8788
"dotenv": "~17.2.3",

packages/sdk/packages/embed/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@xylabs/react-hooks": "~7.1.8",
5151
"@xylabs/react-select": "~7.1.8",
5252
"@xylabs/react-shared": "~7.1.8",
53+
"@xylabs/typeof": "~5.0.18",
5354
"@xyo-network/huri": "~5.1.15",
5455
"@xyo-network/payload-model": "~5.1.15",
5556
"@xyo-network/react-payload-plugin": "workspace:^",

packages/sdk/packages/embed/src/components/embed-card/card/EmbedCardHeader.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Avatar, CardHeader, Chip,
55
} from '@mui/material'
66
import { FlexRow } from '@xylabs/react-flexbox'
7+
import { isDefined } from '@xylabs/typeof'
78
import React from 'react'
89

910
import { useEmbedPluginState, useResolvePayload } from '../../../contexts/index.ts'
@@ -33,7 +34,7 @@ export const EmbedCardHeader: React.FC<CardHeaderProps> = () => {
3334
}
3435
action={(
3536
<FlexRow flexWrap="wrap" columnGap={0.5}>
36-
{timestamp
37+
{isDefined(timestamp) && !Number.isNaN(timestamp)
3738
? hideTimestamp && hideRefreshButton
3839
? ''
3940
: (

packages/sdk/packages/embed/src/components/embed-card/card/EmbedPluginCard.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { CardContent } from '@mui/material'
22
import { FlexGrowRow } from '@xylabs/react-flexbox'
3+
import { isTruthy } from '@xylabs/typeof'
34
import { useListMode } from '@xyo-network/react-shared'
45
import React from 'react'
56

6-
import { useEmbedPluginState, useResolvePayload } from '../../../contexts/index.ts'
7+
import {
8+
useEmbedPluginState, usePluginProps, useResolvePayload,
9+
} from '../../../contexts/index.ts'
710
import { EmbedRenderSelect, ListModeSelectFormControl } from '../../controls/index.ts'
811
import type { BusyCardProps } from './BusyCard.tsx'
912
import { BusyCard } from './BusyCard.tsx'
@@ -15,7 +18,8 @@ export const EmbedPluginCard: React.FC<BusyCardProps> = ({ ...props }) => {
1518
activePlugin: ActivePlugin, plugins, hideElementsConfig,
1619
} = useEmbedPluginState()
1720
const { listMode } = useListMode()
18-
const supportsListMode = ActivePlugin?.components?.box?.listModes?.length ?? 0 > 1
21+
const { pluginProps } = usePluginProps()
22+
const supportsListMode = isTruthy(ActivePlugin?.components?.box?.listModes?.length) ? true : false
1923

2024
return (
2125
<BusyCard {...props}>
@@ -35,7 +39,7 @@ export const EmbedPluginCard: React.FC<BusyCardProps> = ({ ...props }) => {
3539
: null}
3640
<CardContent sx={{ height: '100%' }}>
3741
{ActivePlugin
38-
? <ActivePlugin.components.box.detailsBox payload={payload} {...(supportsListMode && { listMode })} />
42+
? <ActivePlugin.components.box.detailsBox payload={payload} {...pluginProps} {...(supportsListMode && { listMode })} />
3943
: null}
4044
</CardContent>
4145
</BusyCard>

packages/sdk/packages/embed/src/components/embed-card/error-handling/EmbedErrorCard.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AlertProps, CardProps } from '@mui/material'
22
import {
33
Alert, AlertTitle, Card, CardContent, Typography,
44
} from '@mui/material'
5+
import { isDefined } from '@xylabs/typeof'
56
import type { PropsWithChildren } from 'react'
67
import React from 'react'
78

@@ -34,7 +35,7 @@ const DefaultErrorAlert: React.FC<EmbedErrorCardBaseProps> = ({
3435
return (
3536
<Alert severity="error" {...alertProps}>
3637
<AlertTitle>Whoops! Something went wrong</AlertTitle>
37-
{scope
38+
{isDefined(scope)
3839
? (
3940
<Typography variant="caption">
4041
Scope:

packages/sdk/packages/embed/src/components/embed-card/menu/JsonMenuItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { MenuItemProps } from '@mui/material'
33
import {
44
ListItemIcon, ListItemText, MenuItem,
55
} from '@mui/material'
6+
import { isDefined } from '@xylabs/typeof'
67
import React from 'react'
78

89
import { useResolvePayload } from '../../../contexts/index.ts'
@@ -12,7 +13,7 @@ export const JsonMenuItem: React.FC<MenuItemProps> = (props) => {
1213

1314
return (
1415
<>
15-
{huri
16+
{isDefined(huri)
1617
? (
1718
<MenuItem title="Source Payload JSON" onClick={() => window.open(huri, '_blank')} {...props}>
1819
<ListItemText sx={{ mr: 1 }}>JSON</ListItemText>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { PropsWithChildren } from 'react'
2+
import React, {
3+
useEffect, useMemo, useState,
4+
} from 'react'
5+
6+
import { PluginPropsContext } from './context.ts'
7+
import type { PluginProps, PluginPropsState } from './state.ts'
8+
9+
export interface PluginPropsProviderProps extends PropsWithChildren {
10+
pluginProps: PluginProps
11+
}
12+
13+
export const PluginPropsProvider: React.FC<PluginPropsProviderProps> = ({ children, pluginProps: pluginPropsProp }) => {
14+
const [pluginProps, setPluginProps] = useState<PluginProps>(pluginPropsProp)
15+
16+
useEffect(() => {
17+
// needs to be in useEffect since we are in a provider
18+
// eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect
19+
setPluginProps(pluginPropsProp)
20+
}, [pluginPropsProp])
21+
22+
const value: PluginPropsState = useMemo(() => ({
23+
pluginProps,
24+
provided: true,
25+
}), [pluginProps])
26+
27+
return (
28+
<PluginPropsContext value={value}>
29+
{children}
30+
</PluginPropsContext>
31+
)
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContextEx } from '@xylabs/react-shared'
2+
3+
import type { PluginPropsState } from './state.ts'
4+
5+
export const PluginPropsContext = createContextEx<PluginPropsState>()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './context.ts'
2+
export * from './Provider.tsx'
3+
export * from './state.ts'
4+
export * from './use.ts'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { ContextExState } from '@xylabs/react-shared'
2+
3+
export type PluginProps = React.PropsWithChildren
4+
5+
export interface PluginPropsStateFields {
6+
pluginProps: Record<string, unknown>
7+
}
8+
9+
export type PluginPropsState = ContextExState<PluginPropsStateFields>

0 commit comments

Comments
 (0)