Skip to content

Commit bbf9af3

Browse files
authored
feat: [BREAKING] remove adapter from Action class (#32)
* feat: [BREAKING] remove adapter from Action class * fix: add missing adapter to isSupported * chore: adjust comment
1 parent 9ef02ef commit bbf9af3

File tree

6 files changed

+32
-60
lines changed

6 files changed

+32
-60
lines changed

examples/mini-blinks/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function App() {
1616
);
1717
const { action, isLoading } = useAction({
1818
url: 'solana-action:https://dial.to/api/donate',
19-
adapter,
2019
});
2120

2221
return (
@@ -28,6 +27,7 @@ function App() {
2827
<span>Loading</span>
2928
) : (
3029
<Miniblink
30+
adapter={adapter}
3131
selector={(currentAction) =>
3232
currentAction.actions.find((a) => a.label === 'Donate')!
3333
}

packages/blinks-core/src/BlinkContainer.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
AbstractActionComponent,
1010
Action,
11+
type ActionAdapter,
1112
type ActionCallbacksConfig,
1213
type ActionContext,
1314
type ActionSupportability,
@@ -252,6 +253,7 @@ type NormalizedSecurityLevel = Record<Source, SecurityLevel>;
252253

253254
export interface BlinkContainerProps {
254255
action: Action;
256+
adapter: ActionAdapter;
255257
selector?: (currentAction: Action) => AbstractActionComponent | null;
256258
websiteUrl?: string | null;
257259
websiteText?: string | null;
@@ -263,6 +265,7 @@ export interface BlinkContainerProps {
263265
// overall flow: check-supportability -> idle/block -> executing -> success/error or chain
264266
export const BlinkContainer = ({
265267
action: initialAction,
268+
adapter,
266269
websiteUrl,
267270
websiteText,
268271
callbacks,
@@ -387,7 +390,7 @@ export const BlinkContainer = ({
387390
return;
388391
}
389392
try {
390-
const supportability = await action.isSupported();
393+
const supportability = await action.isSupported(adapter);
391394
setSupportability(supportability);
392395
} finally {
393396
dispatch({
@@ -400,7 +403,13 @@ export const BlinkContainer = ({
400403
};
401404

402405
checkSupportability(action);
403-
}, [action, executionState.status, overallState, isPassingSecurityCheck]);
406+
}, [
407+
action,
408+
adapter,
409+
executionState.status,
410+
overallState,
411+
isPassingSecurityCheck,
412+
]);
404413

405414
const execute = async (
406415
component: AbstractActionComponent,
@@ -454,7 +463,7 @@ export const BlinkContainer = ({
454463
};
455464

456465
try {
457-
const account = await action.adapter.connect(context);
466+
const account = await adapter.connect(context);
458467
if (!account) {
459468
dispatch({ type: ExecutionType.RESET });
460469
return;
@@ -504,7 +513,7 @@ export const BlinkContainer = ({
504513
};
505514

506515
if (response.type === 'transaction' || !response.type) {
507-
const signResult = await action.adapter.signTransaction(
516+
const signResult = await adapter.signTransaction(
508517
response.transaction,
509518
context,
510519
);
@@ -514,17 +523,14 @@ export const BlinkContainer = ({
514523
return;
515524
}
516525

517-
await action.adapter.confirmTransaction(signResult.signature, context);
526+
await adapter.confirmTransaction(signResult.signature, context);
518527

519528
await chain(signResult.signature);
520529
return;
521530
}
522531

523532
if (response.type === 'message') {
524-
const signResult = await action.adapter.signMessage(
525-
response.data,
526-
context,
527-
);
533+
const signResult = await adapter.signMessage(response.data, context);
528534

529535
if (!signResult || isSignMessageError(signResult)) {
530536
dispatch({ type: ExecutionType.RESET });

packages/blinks-core/src/api/Action/Action.ts

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export class Action {
6161
private readonly _data: NextAction,
6262
private readonly _metadata: ActionMetadata,
6363
private readonly _supportStrategy: ActionSupportStrategy,
64-
private _adapter?: ActionAdapter,
6564
private readonly _chainMetadata: ActionChainMetadata = { isChained: false },
6665
private readonly _id?: string,
6766
private readonly _experimental?: ExperimentalFeatures,
@@ -162,30 +161,9 @@ export class Action {
162161
};
163162
}
164163

165-
public get adapterUnsafe() {
166-
return this._adapter;
167-
}
168-
169-
public get adapter() {
170-
if (!this._adapter) {
171-
throw new Error('No adapter provided');
172-
}
173-
174-
return this._adapter;
175-
}
176-
177-
/**
178-
* Set the adapter for the action.
179-
* Not recommended to use in react environments, consider using Action.update() instead that returns a new instance.
180-
* @param adapter The adapter to set
181-
*/
182-
public setAdapter(adapter: ActionAdapter) {
183-
this._adapter = adapter;
184-
}
185-
186-
public async isSupported() {
164+
public async isSupported(adapter: ActionAdapter) {
187165
try {
188-
return await this._supportStrategy(this);
166+
return await this._supportStrategy(this, adapter);
189167
} catch (e) {
190168
console.error(
191169
`[@dialectlabs/blinks] Failed to check supportability for action ${this.url}`,
@@ -210,7 +188,6 @@ export class Action {
210188
next.action,
211189
this.metadata,
212190
this._supportStrategy,
213-
this.adapter,
214191
{
215192
isChained: true,
216193
isInline: true,
@@ -257,7 +234,6 @@ export class Action {
257234
data,
258235
metadata,
259236
this._supportStrategy,
260-
this.adapter,
261237
{
262238
isChained: true,
263239
isInline: false,
@@ -272,23 +248,20 @@ export class Action {
272248
data: NextAction,
273249
metadata: ActionMetadata,
274250
supportStrategy: ActionSupportStrategy,
275-
adapter?: ActionAdapter,
276251
) {
277252
const id = nanoid();
278253
return new Action(
279254
url,
280255
data,
281256
metadata,
282257
supportStrategy,
283-
adapter,
284258
{ isChained: false },
285259
id,
286260
);
287261
}
288262

289263
private static async _fetch(
290264
apiUrl: string,
291-
adapter?: ActionAdapter,
292265
supportStrategy: ActionSupportStrategy = defaultActionSupportStrategy,
293266
chainMetadata?: ActionChainMetadata,
294267
id?: string,
@@ -314,7 +287,6 @@ export class Action {
314287
{ ...data, type: 'action' },
315288
metadata,
316289
supportStrategy,
317-
adapter,
318290
chainMetadata,
319291
id,
320292
data.dialectExperimental,
@@ -323,13 +295,11 @@ export class Action {
323295

324296
static async fetch(
325297
apiUrl: string,
326-
adapter?: ActionAdapter,
327298
supportStrategy: ActionSupportStrategy = defaultActionSupportStrategy,
328299
) {
329300
const id = nanoid();
330301
return Action._fetch(
331302
apiUrl,
332-
adapter,
333303
supportStrategy,
334304
{
335305
isChained: false,
@@ -341,23 +311,18 @@ export class Action {
341311
refresh() {
342312
return Action._fetch(
343313
this.url,
344-
this.adapter,
345314
this._supportStrategy,
346315
this._chainMetadata,
347316
this._id,
348317
);
349318
}
350319

351-
withUpdate(update: {
352-
adapter?: ActionAdapter;
353-
supportStrategy?: ActionSupportStrategy;
354-
}) {
320+
withUpdate(update: { supportStrategy?: ActionSupportStrategy }) {
355321
return new Action(
356322
this._url,
357323
this._data,
358324
this._metadata,
359325
update.supportStrategy ?? this._supportStrategy,
360-
update.adapter ?? this._adapter,
361326
this._chainMetadata,
362327
this._id,
363328
this._experimental,

packages/blinks-core/src/api/Action/action-supportability.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BlockchainIds, getShortBlockchainName } from '../../utils/caip-2.ts';
22
import { ACTIONS_SPEC_VERSION } from '../../utils/dependency-versions.ts';
3+
import type { ActionAdapter } from '../ActionConfig.ts';
34
import type { Action } from './Action.ts';
45

56
/**
@@ -44,18 +45,21 @@ export type ActionSupportability =
4445

4546
export type ActionSupportStrategy = (
4647
action: Action,
48+
adapter: ActionAdapter,
4749
) => Promise<ActionSupportability>;
4850

4951
/**
5052
* Default implementation for checking if an action is supported.
5153
* Checks if the action version and the action blockchain IDs are supported by blink.
5254
* @param action Action.
55+
* @param adapter Action adapter.
5356
*
5457
* @see {isVersionSupported}
5558
* @see {isBlockchainSupported}
5659
*/
5760
export const defaultActionSupportStrategy: ActionSupportStrategy = async (
5861
action,
62+
adapter,
5963
) => {
6064
const { version: actionVersion, blockchainIds: actionBlockchainIds } =
6165
action.metadata;
@@ -74,9 +78,7 @@ export const defaultActionSupportStrategy: ActionSupportStrategy = async (
7478
}
7579

7680
const supportedActionVersion = MAX_SUPPORTED_ACTION_VERSION;
77-
const supportedBlockchainIds = !action.adapterUnsafe
78-
? actionBlockchainIds // Assuming action is supported if adapter absent for optimistic compatibility
79-
: action.adapterUnsafe.metadata.supportedBlockchainIds;
81+
const supportedBlockchainIds = adapter.metadata.supportedBlockchainIds;
8082

8183
const versionSupported = isVersionSupported({
8284
actionVersion,

packages/blinks-core/src/hooks/useAction.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect, useState } from 'react';
22
import {
33
Action,
4-
type ActionAdapter,
54
type ActionSupportStrategy,
65
defaultActionSupportStrategy,
76
} from '../api';
@@ -10,7 +9,6 @@ import { useActionsRegistryInterval } from './useActionRegistryInterval.ts';
109

1110
interface UseActionOptions {
1211
url: string | URL;
13-
adapter: ActionAdapter;
1412
securityRegistryRefreshInterval?: number;
1513
supportStrategy?: ActionSupportStrategy;
1614
}
@@ -46,7 +44,6 @@ function useActionApiUrl(url: string | URL) {
4644

4745
export function useAction({
4846
url,
49-
adapter,
5047
supportStrategy = defaultActionSupportStrategy,
5148
}: UseActionOptions) {
5249
const { isRegistryLoaded } = useActionsRegistryInterval();
@@ -63,7 +60,7 @@ export function useAction({
6360

6461
let ignore = false;
6562
setHasFetched(false);
66-
Action.fetch(actionApiUrl, undefined, supportStrategy)
63+
Action.fetch(actionApiUrl, supportStrategy)
6764
.then((action) => {
6865
if (ignore) {
6966
return;
@@ -87,23 +84,22 @@ export function useAction({
8784
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if actionApiUrl changes
8885
}, [actionApiUrl, isRegistryLoaded]);
8986

90-
// this effect handles race conditions between fetching the action adapter change
87+
// this effect handles race conditions between fetching the action support strategy changes
9188
// hasFetched dependency is used instead of action dependency to ensure there's no infinite loop
9289
useEffect(() => {
9390
if (!action || !hasFetched) {
9491
return;
9592
}
9693
try {
9794
const updated = action.withUpdate({
98-
adapter,
9995
supportStrategy,
10096
});
10197
setAction(updated);
10298
} catch (e) {
10399
console.error('[@dialectlabs/blinks-core] Failed to update action', e);
104100
}
105-
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if adapter or supportStrategy changes
106-
}, [adapter, supportStrategy, hasFetched]);
101+
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if supportStrategy changes
102+
}, [supportStrategy, hasFetched]);
107103

108104
return { action, isLoading };
109105
}

packages/blinks/src/ext/twitter.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ async function handleNewNode(
192192

193193
const action = await Action.fetch(
194194
actionApiUrl,
195-
config,
196195
options.supportStrategy,
197196
).catch(noop);
198197

@@ -201,6 +200,7 @@ async function handleNewNode(
201200
}
202201

203202
const { container: actionContainer, reactRoot } = createAction({
203+
config,
204204
originalUrl: actionUrl,
205205
action,
206206
callbacks,
@@ -230,12 +230,14 @@ function createAction({
230230
action,
231231
callbacks,
232232
options,
233+
config,
233234
}: {
234235
originalUrl: URL;
235236
action: Action;
236237
callbacks: Partial<ActionCallbacksConfig>;
237238
options: NormalizedObserverOptions;
238239
isInterstitial: boolean;
240+
config: ActionAdapter;
239241
}) {
240242
const container = document.createElement('div');
241243
container.className = 'dialect-action-root-container';
@@ -245,6 +247,7 @@ function createAction({
245247
actionRoot.render(
246248
<div onClick={(e) => e.stopPropagation()}>
247249
<Blink
250+
adapter={config}
248251
stylePreset={resolveXStylePreset()}
249252
action={action}
250253
websiteUrl={originalUrl.toString()}

0 commit comments

Comments
 (0)