Using the Hooks

The Torque UI library provides a set of React hooks that make it easy to integrate Torque functionality into your Solana application. To use the hooks below, make sure that you've already setup the TorqueProvider inside of your app. The hooks can only be used if the Torque context exists.

useTorque

The main hook for interacting with the Torque SDK. The hook makes it easy to access and interact with the user's offers if you want to implement them directly in your application.

import { useTorque } from "@torque-labs/torque-ui";

function MyComponent() {
  const {
    offers,
    journeys,
    publicKey,
    user,
    userClient,
    config,
    initialized,
    isLoading,
    initialize,
    logout,
    claimOffer,
    refreshOffers,
  } = useTorque();

  const handleClaim = async (offerId: string) => {
    await claimOffer(offerId);
  };

  return (
    <div>
      {offers.map((offer) => (
        <div key={offer.id}>
          <h3>{offer.title}</h3>
          <button onClick={() => handleClaim(offer.id)}>Claim Offer</button>
        </div>
      ))}
    </div>
  );
}

Returns

useOfferStatus

The useOfferStatus hook provides information about the status of a specific offer for the current user.

import { useOfferStatus } from "@torque-labs/torque-ui";

function OfferComponent({ offerId }: { offerId: string }) {
  const { hasStarted, isCompleted } = useOfferStatus(offerId);

  return (
    <div>
      {hasStarted ? "Offer in progress" : "Start offer"}
      {isCompleted && "Offer completed!"}
    </div>
  );
}

Returns

useAction

The useAction hook provides a utility for handling Torque Solana actions returned from the Torque API. The actions are Solana actions that allow the user to complete a requirement for an offer.

NOTE: The hook will automatically prompt the user's wallet for their signature.

import { useAction } from "@torque-labs/torque-ui";
import type { ApiRequirement } from "@torque-labs/torque-ts-sdk";

function RequirementComponent({
  index,
  requirement,
  data
}: {
  index: number;
  requirement: ApiRequirement;
  data: Record<string,string>
}) {
  const { isLoading, handleBountyStepAction } = useAction();

  const handleComplete = async () => {
    try {
      await handleBountyStepAction({
        campaignId: requirement.campaignId,
        index,
        data,
        onSuccess: () => {
          // Handle success
        },
        onError: (error) => {
          // Handle error
        },
      });
    } catch (error) {
      // Handle error
    }
  };

  return (
    <div>
      <button onClick={handleComplete}>Complete Requirement</button>
    </div>
  );
}

Returns

Types

TorqueOptions

TorqueOptions is an interface that defines the configuration options for the Torque SDK:

interface TorqueOptions {
  apiUrl?: string;
  appUrl?: string;
  functionsUrl?: string;
  rpc?: string;
  publisherHandle?: string;
}

TorqueInitOptions

If you already have a SIWS or basic signature in your app, you can use the loginInput property of the TorqueInitOptions interface to initialize the user with Torque. This lets Torque validate the user's identity and retrieve their offers without requiring a secondary signature to authenticate with Torque.

interface TorqueInitOptions {
  loginInput?: ApiInputLogin;
}

HandleActionProps

The HandleActionProps interface is used by the handleBountyStepAction in the useAction hook to handle Torque actions.

interface HandleActionProps {
  campaignId: string;
  index: number;
  data?: Record<string, string>;
  onSuccess: OfferActionOnSuccess;
  onError: OfferActionOnError;
}

Last updated