Skip to content

Commit

Permalink
feat: Add wrapper component to FrameMetadata (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
syntag authored Feb 6, 2024
1 parent 90b3f48 commit e5b305a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/components/FrameMetadata.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,17 @@ describe('FrameMetadata', () => {
).toBe('10');
expect(meta.container.querySelectorAll('meta').length).toBe(3);
});

it('renders with wrapperComponent', () => {
const meta = render(
<FrameMetadata
image="https://example.com/image.png"
wrapperComponent={({ children }) => <div id="wrapper">{children}</div>}
/>,
);

expect(meta.container.querySelector('#wrapper')).not.toBeNull();
expect(meta.container.querySelector('meta[name="fc:frame:image"]')).not.toBeNull();
expect(meta.container.querySelectorAll('meta').length).toBe(2);
});
});
7 changes: 5 additions & 2 deletions src/components/FrameMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fragment } from 'react';
import type { FrameMetadataType } from '../core/types';

/**
Expand Down Expand Up @@ -32,6 +33,7 @@ import type { FrameMetadataType } from '../core/types';
* @param {string} props.post_url - The post URL.
* @param {number} props.refresh_period - The refresh period.
* @param {Array<{ label: string, action?: string }>} props.buttons - The buttons.
* @param {React.ComponentType<any> | undefined} props.wrapperComponent - The wrapper component meta tags are rendered in.
* @returns {React.ReactElement} The FrameMetadata component.
*/
export function FrameMetadata({
Expand All @@ -40,9 +42,10 @@ export function FrameMetadata({
input,
post_url,
refresh_period,
wrapperComponent: WrapperComponent = Fragment,
}: FrameMetadataType) {
return (
<>
<WrapperComponent>
<meta name="fc:frame" content="vNext" />
{!!image && <meta name="fc:frame:image" content={image} />}
{!!input && <meta name="fc:frame:input:text" content={input.text} />}
Expand All @@ -63,6 +66,6 @@ export function FrameMetadata({
{!!refresh_period && (
<meta name="fc:frame:refresh_period" content={refresh_period.toString()} />
)}
</>
</WrapperComponent>
);
}
4 changes: 1 addition & 3 deletions src/core/getFrameMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { FrameMetadataType } from './types';

type FrameMetadataResponse = Record<string, string>;
import { FrameMetadataResponse, FrameMetadataType } from './types';

/**
* This function generates the metadata for a Farcaster Frame.
Expand Down
8 changes: 8 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ export type FrameMetadataType = {
input?: FrameInputMetadata;
post_url?: string;
refresh_period?: number;
wrapperComponent?: React.ComponentType<any>;
};

/**
* Frame Metadata Response
*
* Note: exported as public Type
*/
export type FrameMetadataResponse = Record<string, string>;

0 comments on commit e5b305a

Please sign in to comment.