/**
* ConnectAccountModal - OAuth account connection flow.
*/
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { TextControl, Spinner, Button } from '@aime/wp-components';
import useApi from '../../../hooks/useApi';
import { toast } from '../../common/Toast';
/**
* Per-platform manual connection guide.
*/
const PLATFORM_GUIDE = {
facebook: {
color: '#1877F2',
tokenLabel: __( 'Page Access Token or User Access Token', 'ai-marketing-expert' ),
nameLabel: __( 'Page Name', 'ai-marketing-expert' ),
namePlaceholder: __( 'Facebook page name', 'ai-marketing-expert' ),
nameHelp: __( 'This is the page name label shown inside the plugin for the connected Facebook Page.', 'ai-marketing-expert' ),
needsSecret: false,
steps: [
{ n: 1, text: __( 'Go to', 'ai-marketing-expert' ), link: { href: 'https://developers.facebook.com', label: 'developers.facebook.com' }, after: __( 'and log in with the account that manages your Page.', 'ai-marketing-expert' ) },
{ n: 2, text: __( 'Create or open an App (type "Business"), then open', 'ai-marketing-expert' ), link: { href: 'https://developers.facebook.com/tools/explorer/', label: 'Graph API Explorer' }, after: __( 'and select your App from the top-right dropdown.', 'ai-marketing-expert' ) },
{ n: 3, text: __( 'Click "Generate Access Token" and add permissions: pages_show_list, pages_manage_posts, and pages_read_engagement. Complete the login dialog.', 'ai-marketing-expert' ) },
{ n: 4, text: __( 'If Graph API Explorer still shows "User Token", that is okay. The plugin can now resolve your Page automatically when the token can access only one Page.', 'ai-marketing-expert' ) },
{ n: 5, text: __( 'If your token can access multiple Pages, run', 'ai-marketing-expert' ), link: { href: 'https://developers.facebook.com/tools/explorer/', label: '/me/accounts' }, after: __( 'in Graph API Explorer and copy the Page ID you want to connect.', 'ai-marketing-expert' ) },
{ n: 6, text: __( 'You can still switch from "User Token" to your Page in Graph API Explorer to get a Page Access Token, then paste that token below.', 'ai-marketing-expert' ) },
],
},
instagram: {
color: '#E4405F',
tokenLabel: __( 'Instagram Access Token', 'ai-marketing-expert' ),
nameLabel: __( 'Account Name', 'ai-marketing-expert' ),
namePlaceholder: __( 'Instagram account name', 'ai-marketing-expert' ),
nameHelp: __( 'This is the label shown inside the plugin for the connected Instagram account.', 'ai-marketing-expert' ),
needsSecret: false,
steps: [
{ n: 1, text: __( 'Make sure your Instagram is a Professional account (Business or Creator). On Instagram: Profile → ☰ → Settings → Professional account.', 'ai-marketing-expert' ) },
{ n: 2, text: __( 'Go to', 'ai-marketing-expert' ), link: { href: 'https://developers.facebook.com/apps', label: 'developers.facebook.com/apps' }, after: __( '→ create or open an App (type "Business").', 'ai-marketing-expert' ) },
{ n: 3, text: __( 'In the left menu click "Add Product" → find "Instagram" → click "Set up" → choose "API setup with Instagram business login".', 'ai-marketing-expert' ) },
{ n: 4, text: __( 'Scroll down in the Instagram setup page to find your account → click "Generate token" → log in when prompted. The token is already long-lived (60 days). No Facebook Page needed.', 'ai-marketing-expert' ) },
{ n: 5, text: __( 'Copy the Access Token AND your Instagram User ID shown on the same page. Paste both in the fields below.', 'ai-marketing-expert' ) },
],
},
x: {
color: '#000000',
tokenLabel: __( 'Access Token', 'ai-marketing-expert' ),
nameLabel: __( 'Account Name', 'ai-marketing-expert' ),
namePlaceholder: __( 'X account name', 'ai-marketing-expert' ),
nameHelp: __( 'This is the label shown inside the plugin for the connected X account.', 'ai-marketing-expert' ),
needsSecret: true,
needsAppCredentials: true,
apiKeyLabel: __( 'API Key (Consumer Key)', 'ai-marketing-expert' ),
apiSecretLabel: __( 'API Secret (Consumer Secret / Secret Key)', 'ai-marketing-expert' ),
apiKeyHelp: __( 'In X docs this is also called the Consumer Key.', 'ai-marketing-expert' ),
apiSecretHelp: __( 'In X docs this is also called the Consumer Secret or Secret Key.', 'ai-marketing-expert' ),
bearerTokenHelp: __( 'Bearer Token is not required for this plugin. X posting here uses OAuth 1.0a user context with API Key, API Secret, Access Token, and Access Token Secret.', 'ai-marketing-expert' ),
steps: [
{ n: 1, isWarning: true, text: __( 'Notice: X.com (Twitter) API is NOT free. It is a premium/pay-as-you-go service and requires paid credits to publish posts.', 'ai-marketing-expert' ) },
{ n: 2, text: __( 'Go to', 'ai-marketing-expert' ), link: { href: 'https://developer.twitter.com/en/portal/projects-and-apps', label: 'developer.twitter.com' }, after: __( 'and create a Project + App.', 'ai-marketing-expert' ) },
{ n: 3, text: __( 'Under App Settings → "User authentication settings", set permissions to Read and Write.', 'ai-marketing-expert' ) },
{ n: 4, text: __( 'Go to the "Keys and Tokens" tab of your App.', 'ai-marketing-expert' ) },
{ n: 5, text: __( 'Copy your Consumer Key and Secret Key. In this plugin those map to API Key and API Secret.', 'ai-marketing-expert' ) },
{ n: 6, text: __( 'Under Authentication Tokens, generate an Access Token and Access Token Secret.', 'ai-marketing-expert' ) },
{ n: 7, text: __( 'You do not need the Bearer Token for this plugin. Paste only those four values in the fields below.', 'ai-marketing-expert' ) },
],
},
linkedin: {
color: '#0A66C2',
tokenLabel: __( 'LinkedIn Access Token', 'ai-marketing-expert' ),
nameLabel: __( 'Account / Company Name', 'ai-marketing-expert' ),
namePlaceholder: __( 'LinkedIn account or page name', 'ai-marketing-expert' ),
nameHelp: __( 'This is the label shown inside the plugin for the connected LinkedIn profile or company page.', 'ai-marketing-expert' ),
needsSecret: false,
steps: [
{ n: 1, text: __( 'Go to', 'ai-marketing-expert' ), link: { href: 'https://www.linkedin.com/developers/apps', label: 'linkedin.com/developers' }, after: __( 'and create an App (linked to your LinkedIn Page or Personal profile).', 'ai-marketing-expert' ) },
{ n: 2, text: __( 'Under the Products tab, request access to "Share on LinkedIn" and "Sign In with LinkedIn using OpenID Connect".', 'ai-marketing-expert' ) },
{ n: 3, text: __( 'Under the Auth tab, generate an OAuth 2.0 token with permissions (w_member_social or w_organization_social), or use the token tool.', 'ai-marketing-expert' ) },
{ n: 4, text: __( 'Paste the Access Token below. The plugin will automatically detect your member or organization URN.', 'ai-marketing-expert' ) },
],
},
};
const GuideStep = ( { step } ) => (
{ __( 'Switch to Professional Account (Business or Creator)', 'ai-marketing-expert' ) }
{ __( 'Meta does not permit posting to Personal accounts. It is 100% free to switch.', 'ai-marketing-expert' ) }
📱 { __( 'In Instagram App: Settings & privacy → Account type & tools → Switch to professional account.', 'ai-marketing-expert' ) }
{ /* Step 2 */ }
2
{ __( 'Link Instagram to a Facebook Page', 'ai-marketing-expert' ) }
{ __( 'Your Instagram account must be connected to a Facebook Page where you are an Admin.', 'ai-marketing-expert' ) }
🔗 { __( 'In Instagram App: Edit profile → Under "Public business information", tap Page → Connect your Facebook Page.', 'ai-marketing-expert' ) }
{ /* Tip */ }
💡{ __( 'Tip during Meta popup login:', 'ai-marketing-expert' ) }{ ' ' }
{ __( 'Make sure to select BOTH your Facebook Page and Instagram Account. If you previously connected, click "Edit settings" in the popup to verify both are checked.', 'ai-marketing-expert' ) }
{ /* Confirmation Checkbox */ }
{ __( 'Prefer direct token setup without Facebook Page?', 'ai-marketing-expert' ) }{ ' ' }
{ __( 'How to get your access token', 'ai-marketing-expert' ) }
{ ( guide.steps || [] ).map( ( s ) => (
) ) }
{ /* Form fields */ }
setManualData( { ...manualData, name: v } ) }
placeholder={ guide.namePlaceholder || __( 'Enter an account name', 'ai-marketing-expert' ) }
help={ guide.nameHelp || __( 'This is the label shown inside the plugin for the connected account.', 'ai-marketing-expert' ) }
__nextHasNoMarginBottom
/>
{ isEditing && (
{ __( 'Leave token and secret fields blank to keep the current saved values.', 'ai-marketing-expert' ) }
)}
{ guide.needsAppCredentials && (
<>
setManualData( { ...manualData, api_key: v } ) }
placeholder={ __( 'Paste your X API key here', 'ai-marketing-expert' ) }
help={ guide.apiKeyHelp }
__nextHasNoMarginBottom
/>
setManualData( { ...manualData, api_secret: v } ) }
placeholder={ __( 'Paste your X API secret here', 'ai-marketing-expert' ) }
type="password"
help={ guide.apiSecretHelp }
__nextHasNoMarginBottom
/>
{ guide.bearerTokenHelp }
>
) }
setManualData( { ...manualData, access_token: v } ) }
placeholder="Paste your access token here"
__nextHasNoMarginBottom
/>
{ guide.needsSecret && (
setManualData( { ...manualData, access_secret: v } ) }
placeholder={ __( 'Paste your access token secret here', 'ai-marketing-expert' ) }
help={ __( 'Required for X (Twitter) — found in Keys and Tokens tab.', 'ai-marketing-expert' ) }
__nextHasNoMarginBottom
/>
) }
{ selectedPlatform === 'instagram' && (
setManualData( { ...manualData, platform_user_id: v } ) }
placeholder={ __( 'e.g. 17841400000000000', 'ai-marketing-expert' ) }
help={ __( 'Found on App Dashboard → Instagram → API setup page, next to your account. Leave blank to auto-detect.', 'ai-marketing-expert' ) }
__nextHasNoMarginBottom
/>
) }
{ selectedPlatform === 'facebook' && (
setManualData( { ...manualData, platform_user_id: v } ) }
placeholder={ __( 'Paste a Page ID if your token manages multiple Pages', 'ai-marketing-expert' ) }
help={ __( 'Leave blank when the token belongs to one Page only. If you use a user token with multiple Pages, this field tells the plugin which Page to connect.', 'ai-marketing-expert' ) }
__nextHasNoMarginBottom
/>