npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dianamics/pcf-utils

v0.0.33

Published

This npm package is designed for Power Apps Component Framework (PCF). It provides utilities for working with PCF, most of them are React Hooks, which needs to be used in function components.

Downloads

47

Readme

@dianamics\pcf-utils

This npm package is designed for Power Apps Component Framework (PCF). It provides utilities for working with PCF, most of them are React Hooks, which needs to be used in function components.

How to install

npm install @dianamics/pcf-utils

Content

Utils\environmentVariable

Loading value with default value

The purpose of this utility is loading and caching the environmentVariables from Dataverse. It will make the request. It will return the value defined for the environment variable. If no value is found, it will return the default value set in Dataverse.

Type conversion

Another advantage is the automatically conversion to the types: for instance a JSON env var will return an object, a number env var will return a Number, aso.

Caching

Another benefit of this environment variables utility, is the possibility to cache the value. It will cache the content, avoiding a second requests. It will also save the retrieved values in sessionStorage. This way, when the user navigates inside the application, the values will be returned directly from the storage. If that is not possible, will make another request.

If you don't wish a caching, set this in the code using

setupEnvironmentVariable(false);

The default is true. You could also set the starting part of the sessionStorage key:

setupEnvironmentVariable(false, "MyAwesomeRoot.EnvironmentVariable");

The default is "Dianamics.EnvironmentVariables" The sessionStorage key will add the name of the session variable to your key.

Hooks\useEnvironmentVariable

Making use of environment variable utility, this is a custom React Hook, so it can be used inside React function components. Example:

import {useEnvironmentVariable} from "@dianamics/pcf-utils";

export const PCFComponent({webAPI}) : JSX.Element => {
    const {value, isLoading, errorMessage} = useEnvironmentVariable<string>(webAPI, "orb_chosedImage", EnvironmentVariableTypes.String);  

    //or, if you need more env vars inside the same component
    const secondEnvVar = useEnvironmentVariable<string>(webAPI, "orb_secondName", EnvironmentVariableTypes.String); 
    
}

The "webAPI" is the "context.webAPI" property from PCFs. Remember to decalre the webAPI in your PCF manifest

<feature-usage>        
    <uses-feature name="WebAPI" required="true" />
</feature-usage>

Hooks\useResourceImage & useResourceImages

This custom Hook is designed to load resources from your PCF. In order to work, you need first to declare the images in the manifest. Also the utility feature is required.

Example:

<?xml version="1.0" encoding="utf-8" ?>
<manifest>
  <control ...>   
     ...    
    <resources>
      <code path="index.ts" order="1"/>
      <img path="images/skating/s1.png"/> 
      <img path="images/skating/s2.png"/> 
      <img path="images/skating/s3.png"/> 
      <img path="images/My.svg"/>       
    </resources>
     <feature-usage>    
      <uses-feature name="Utility" required="true" />      
    </feature-usage>    
  </control>
</manifest>

The return of this custom hook contains {src, isLoading, errorMessage}. Example to get the content of a image in your react function component:

import {useResourceImage} from "@dianamics/pcf-utils";

export const PCFComponent({resources}) : JSX.Element => {
   const mySVG = useResourceImage(resources, "images/My.svg", "svg");
   return <img src={mySVG.src}>
}

The "resources" is provided by pcf (context.resources).

In case you need to load a bunch of images, you can use "useResourceImages". They only need to be of the same type. The return of this hook is an array of strings, with all the image content (without indicators of loading or error messages). The error messages will be shown in the console. Example:

import {useResourceImages} from "@dianamics/pcf-utils";

export const PCFComponent({webAPI}) : JSX.Element => {
   const images = const images = useResourceImages(resources, ["images/skating/s1.png", "images/skating/s2.png", "images/skating/s3.png"], "png");
   return (<div>
    {images.map((img) => <img src={img}/>)}
   </div>)
}

Hooks\Dataset\usePaging

This custom hook is supposed to be used for PCFs of type dataset. It makes it easier to keep track of the current page, first record on the page, last one and there are some navigation methods.

import {usePaging} from "@dianamics/pcf-utils";
export const PCFComponent({dataset}) : JSX.Element => {
    const { currentPage, moveNext, movePrevious} = usePaging(dataset);
    //currentPage, firstItemNumber, lastItemNumber, totalRecords
    //moveToFirst,  movePrevious, moveNext
        return (<div>                      
            <button onClick={movePrevious}>Prev</button>
            Page:{currentPage}           
            <button onClick={moveNext}>Next</button> 
        </div>
        )
}

More to come...

Making of

This package was made using the tsdx readme tsdx