Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (frontend): New playground frontend state #2406

Merged

Conversation

ardaerzin
Copy link
Contributor

@ardaerzin ardaerzin commented Jan 6, 2025

closes AGE-1497

What has changed?

  • Implemented a single usePlayground hook, that allows atomic access to ui and data state of the new playground.
  • Implemented mutations that are exposed via usePlayground to change data & ui state
  • Implemented a selector middleware for this hook to support custom look ups

What else has changed?

  • added transformers for initial data state

Usage

here are some examples:

// initializes the state, by fetching variant & schema data and transforming variants 
usePlayground({
    hookId: "playground",
})
/**
* viewType, displayedVariants, setSelectedVariant are defined in UIState middleware.
* this usage triggers re-renders only if displayedVariants and/or viewType values change
*/
const {viewType, displayedVariants, setSelectedVariant} = usePlayground()
/**
* you'll have access to a stateSelector, that allows you to pick whatever you need 
* from the whole state, and limits re-renders to changes to your selection
*/
const {variantsWithNames} = usePlayground({
    stateSelector: (state) => ({
        variantsWithNames: state.variants.map((variant) => ({
            variantId: variant.id,
            variantName: variant.variantName,
        })),
    }),
})
/**
* if you provide a variantId, then you can use variantSelector that let's you access the variant 
* again, re-renders are controlled by the return values of the selector.
*/ 
const {isChat} = usePlayground({
        variantId,
        variantSelector: useCallback((variant: EnhancedVariant) => {
            return {
                isChat: variant.isChat,
            }
        }, []),
    })
// for common things like reaching a variant, you don't have to write selectors as these are already included
// trying to access variant without providing a variantId will cause an error
const {variant} = usePlayground({
        variantId,
    })
/**
* if you are building a UI element that allows user to change some values of a property of a 
* variant [we are talking about configuration / test related parameters here]
* just provide the id of the property you are targeting to the hook, and it'll give you an
* object with its value and a mutator
*/
const {variantConfigProperty: property} = usePlayground({
    variantId,
    propertyId,
})


const {__metadata: metadata, value, handleChange} = property

@ashrafchowdury
Copy link
Collaborator

👍

@ardaerzin ardaerzin marked this pull request as ready for review January 14, 2025 09:27
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. feature Frontend typescript labels Jan 14, 2025
@ardaerzin ardaerzin merged commit aa2d5d6 into feat/AGE-1430-new-playground Jan 14, 2025
8 of 11 checks passed
aybruhm pushed a commit that referenced this pull request Feb 13, 2025
…frontend

feat (frontend): New playground frontend state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Frontend size:XXL This PR changes 1000+ lines, ignoring generated files. typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants