Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/connectors/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import type { ConnectorConfig } from './types';
* import { __experimentalRegisterConnector as registerConnector, __experimentalConnectorItem as ConnectorItem } from '@wordpress/connectors';
*
* registerConnector( 'my-plugin/openai', {
* label: 'OpenAI',
* name: 'OpenAI',
* description: 'Text, image, and code generation with GPT.',
* icon: <MyOpenAIIcon />,
* render: ( { slug, label, description, icon } ) => (
* logo: <MyOpenAIIcon />,
* render: ( { slug, name, description, logo } ) => (
* <ConnectorItem
* icon={ icon }
* name={ label }
* logo={ logo }
* name={ name }
* description={ description }
* >
* <MyCustomSettings />
Expand Down
9 changes: 5 additions & 4 deletions packages/connectors/src/connector-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { __, sprintf } from '@wordpress/i18n';
* Internal dependencies
*/
import type { ReactNode } from 'react';
import type { ApiKeySource } from './types';

export interface ConnectorItemProps {
className?: string;
icon?: ReactNode;
logo?: ReactNode;
name: string;
description: string;
actionArea?: ReactNode;
Expand All @@ -30,7 +31,7 @@ export interface ConnectorItemProps {

export function ConnectorItem( {
className,
icon,
logo,
name,
description,
actionArea,
Expand All @@ -41,7 +42,7 @@ export function ConnectorItem( {
<Item className={ className }>
<VStack spacing={ 4 } role="group" aria-labelledby={ headingId }>
<HStack alignment="center" spacing={ 4 } wrap>
{ icon }
{ logo }
<FlexBlock>
<VStack spacing={ 0 }>
<Text
Expand All @@ -65,7 +66,7 @@ export function ConnectorItem( {
);
}

export type ApiKeySource = 'env' | 'constant' | 'database' | 'none';
export type { ApiKeySource } from './types';

export interface DefaultConnectorSettingsProps {
onSave?: ( apiKey: string ) => void | Promise< void >;
Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export {
ConnectorItem as __experimentalConnectorItem,
DefaultConnectorSettings as __experimentalDefaultConnectorSettings,
} from './connector-item';
export type { ApiKeySource as __experimentalApiKeySource } from './connector-item';
export type { ApiKeySource as __experimentalApiKeySource } from './types';
export type { ConnectorConfig, ConnectorRenderProps } from './types';
export { privateApis } from './private-apis';
20 changes: 10 additions & 10 deletions packages/connectors/src/test/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe( 'connectors store', () => {
const before = getConnectors();

registerConnector( 'test', {
label: 'Test',
name: 'Test',
description: 'A test connector',
} );

Expand All @@ -47,7 +47,7 @@ describe( 'connectors store', () => {
expect( after ).toHaveLength( 1 );
expect( after[ 0 ] ).toMatchObject( {
slug: 'test',
label: 'Test',
name: 'Test',
} );
} );
} );
Expand All @@ -61,23 +61,23 @@ describe( 'connectors store', () => {
);

registerConnector( 'test', {
label: 'Original',
name: 'Original',
description: 'Original description',
} );

registerConnector( 'test', {
label: 'Updated',
name: 'Updated',
} );

const connector = getConnector( 'test' );
expect( connector ).toMatchObject( {
slug: 'test',
label: 'Updated',
name: 'Updated',
description: 'Original description',
} );
} );

it( 'should allow updating render and icon independently', () => {
it( 'should allow updating render and logo independently', () => {
const registry = createRegistryWithStore();
const { getConnector } = unlock( registry.select( STORE_NAME ) );
const { registerConnector } = unlock(
Expand All @@ -86,7 +86,7 @@ describe( 'connectors store', () => {

const originalRender = () => null;
registerConnector( 'test', {
label: 'Test',
name: 'Test',
description: 'A test connector',
render: originalRender,
} );
Expand All @@ -98,7 +98,7 @@ describe( 'connectors store', () => {

const connector = getConnector( 'test' );
expect( connector?.render ).toBe( newRender );
expect( connector?.label ).toBe( 'Test' );
expect( connector?.name ).toBe( 'Test' );
expect( connector?.description ).toBe( 'A test connector' );
} );
} );
Expand All @@ -114,7 +114,7 @@ describe( 'connectors store', () => {
);

registerConnector( 'test', {
label: 'Test',
name: 'Test',
description: 'A test connector',
} );

Expand All @@ -134,7 +134,7 @@ describe( 'connectors store', () => {
);

registerConnector( 'test', {
label: 'Test',
name: 'Test',
description: 'A test connector',
} );

Expand Down
30 changes: 26 additions & 4 deletions packages/connectors/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,40 @@
*/
import type { ReactNode } from 'react';

export type ApiKeySource = 'env' | 'constant' | 'database' | 'none';

export type ConnectorAuthentication =
| {
method: 'api_key';
settingName: string;
credentialsUrl: string | null;
keySource?: ApiKeySource;
isConnected?: boolean;
}
| { method: 'none' };

export interface ConnectorPlugin {
slug: string;
isInstalled: boolean;
isActivated: boolean;
}

export interface ConnectorRenderProps {
slug: string;
label: string;
name: string;
description: string;
icon?: ReactNode;
logo?: ReactNode;
authentication?: ConnectorAuthentication;
plugin?: ConnectorPlugin;
}

export interface ConnectorConfig {
slug: string;
label: string;
name: string;
description: string;
icon?: ReactNode;
logo?: ReactNode;
authentication?: ConnectorAuthentication;
plugin?: ConnectorPlugin;
render?: ( props: ConnectorRenderProps ) => ReactNode;
}

Expand Down
78 changes: 25 additions & 53 deletions routes/connectors-home/default-connectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
__experimentalRegisterConnector as registerConnector,
__experimentalConnectorItem as ConnectorItem,
__experimentalDefaultConnectorSettings as DefaultConnectorSettings,
type __experimentalApiKeySource as ApiKeySource,
type ConnectorConfig,
type ConnectorRenderProps,
} from '@wordpress/connectors';
import { __ } from '@wordpress/i18n';
Expand All @@ -24,16 +24,6 @@ import {
DefaultConnectorLogo,
} from './logos';

type ConnectorAuthentication =
| {
method: 'api_key';
settingName: string;
credentialsUrl: string | null;
keySource?: ApiKeySource;
isConnected?: boolean;
}
| { method: 'none' };

interface ConnectorData {
name: string;
description: string;
Expand All @@ -44,7 +34,7 @@ interface ConnectorData {
isInstalled: boolean;
isActivated: boolean;
};
authentication: ConnectorAuthentication;
authentication: NonNullable< ConnectorConfig[ 'authentication' ] >;
}

/**
Expand Down Expand Up @@ -101,28 +91,19 @@ const ConnectedBadge = () => (

const UnavailableActionBadge = () => <Badge>{ __( 'Not available' ) }</Badge>;

interface ApiKeyConnectorConfig {
pluginSlug?: string;
settingName: string;
helpUrl?: string;
isInstalled?: boolean;
isActivated?: boolean;
keySource?: ApiKeySource;
initialIsConnected?: boolean;
}

function ApiKeyConnector( {
label,
name,
description,
pluginSlug,
settingName,
helpUrl,
icon,
isInstalled,
isActivated,
keySource: initialKeySource,
initialIsConnected,
}: ConnectorRenderProps & ApiKeyConnectorConfig ) {
logo,
authentication,
plugin,
}: ConnectorRenderProps ) {
const auth =
authentication?.method === 'api_key' ? authentication : undefined;
const settingName = auth?.settingName ?? '';
const helpUrl = auth?.credentialsUrl ?? undefined;
const pluginSlug = plugin?.slug;

let helpLabel: string | undefined;
try {
if ( helpUrl ) {
Expand All @@ -149,11 +130,11 @@ function ApiKeyConnector( {
} = useConnectorPlugin( {
pluginSlug,
settingName,
connectorName: label,
isInstalled,
isActivated,
keySource: initialKeySource,
initialIsConnected,
connectorName: name,
isInstalled: plugin?.isInstalled,
isActivated: plugin?.isActivated,
keySource: auth?.keySource,
initialIsConnected: auth?.isConnected,
} );
const isExternallyConfigured =
keySource === 'env' || keySource === 'constant';
Expand Down Expand Up @@ -185,8 +166,8 @@ function ApiKeyConnector( {
className={
pluginSlug ? `connector-item--${ pluginSlug }` : undefined
}
icon={ icon }
name={ label }
logo={ logo }
name={ name }
description={ description }
actionArea={
<HStack spacing={ 3 } expanded={ false }>
Expand Down Expand Up @@ -266,21 +247,12 @@ export function registerDefaultConnectors() {
connectorId
) }`;
registerConnector( connectorName, {
label: data.name,
name: data.name,
description: data.description,
icon: getConnectorLogo( connectorId, data.logoUrl ),
render: ( props ) => (
<ApiKeyConnector
{ ...props }
pluginSlug={ data.plugin?.slug }
settingName={ authentication.settingName }
helpUrl={ authentication.credentialsUrl ?? undefined }
isInstalled={ data.plugin?.isInstalled }
isActivated={ data.plugin?.isActivated }
keySource={ authentication.keySource }
initialIsConnected={ authentication.isConnected }
/>
),
logo: getConnectorLogo( connectorId, data.logoUrl ),
authentication,
plugin: data.plugin,
render: ApiKeyConnector,
} );
}
}
8 changes: 6 additions & 2 deletions routes/connectors-home/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ function ConnectorsPage() {
<connector.render
key={ connector.slug }
slug={ connector.slug }
label={ connector.label }
name={ connector.name }
description={ connector.description }
icon={ connector.icon }
logo={ connector.logo }
authentication={
connector.authentication
}
plugin={ connector.plugin }
/>
);
}
Expand Down
Loading