pieces-app / example-typescript

A React example project showing how to get started with Pieces TS SDK.
MIT License
35 stars 45 forks source link

Generate shareable code snippet links from code snippets saved on the user’s DB: #92

Open shivay-at-pieces opened 6 months ago

shivay-at-pieces commented 6 months ago
  1. Make sure the user is logged in to Pieces - logic to check whether the user is signed in or not - User property when checking for PiecesOS status.

    • Pieces.Auth0Api.getAuth0UserInfo()
    • If userContext is empty - user is logged out
  2. Create an asset for the snippet - AssetsApi().assetsCreateNewAsset()

  3. LinkifyApi - Use this to create the link that contains the asset

const link: Share | number = await api.linkifyApi
    .linkify({
      linkify: {
        asset,
        distributions,
        access: AccessEnum.Public,
      },
    })
    .catch((error) => error.status);

  if (typeof link === 'number' || !('link' in link)) {
    const errorMessage = `Error occurred with status ${link}. Please check that Pieces OS is installed and running.`;
    console.log(`Failed to create link. ${errorMessage}`);
    return;
  }

  console.log('Link creation successful');

  // Assuming a function to copy text to the clipboard
  const copyToClipboard = async (text: string) => {
    // Implementation to copy text to clipboard
    console.log(`Copied to clipboard: ${text}`);
  };

  const message = `Shareable Link: ${link.link}`;
  if (true /* Assuming the setting to copy link to clipboard is enabled */) {
    await copyToClipboard(link.link);
    console.log(`Created a link! It is copied to clipboard. Your ${message}`);
    return;
  }
RohanMishra315 commented 6 months ago

Hey @shivay-at-pieces the code should be in App.tsx file right, just confirmation !