How to have a media asset area/dialogue inside of a custom page/sidebar/extension

How do I work with media asses outside of the media asset page?

Currently I have the code below that is just a text box asking for the media asset upload ID

But what I really want is code that would display like this:

And I couldnā€™t seem to find an answer here:
Plugin UI Documentation

Essentially I want this file to come from media assets. Or failing that a way to write a button that would open a file picker that would then be saved to the standard media assets.

Specifically there are two things I want to do:

  • Be able to load an image (no need to specify focal point) within a field extension presentation config
  • Be able to load several PDFs within a sidebar control

This is the code I currently have that I would like to update to use media asset, instead of forcing the user to go get the ID


export function ThumbnailTranslateConfigScreen({ ctx }: ConfigPropTypes) {
    const [formValues, setFormValues] = useState<Partial<ThumbnailParameters>>(
        ctx.parameters,
    );
    const errors = ctx.errors as Partial<Record<string, string>>;
    
    const update = useCallback((field:any, value:any) => {
        const newParameters = { ...formValues, [field]: value };
        setFormValues(newParameters);
        ctx.setParameters(newParameters).then(() => {console.log("Parameters set")});
    }, [formValues, setFormValues, ctx.setParameters]);
    return (
        <Canvas ctx={ctx}>
            <Form>
                <TextField
                    id="baseThumbnailId"
                    name="baseThumbnailId"
                    label="The Upload Id of the base thumbnail, see media library"
                    required
                    value={formValues.baseThumbnailId ?? "VhJF2CPgQ0CFwinsAZbSKQ"}
                    onChange={update.bind(null, 'baseThumbnailId')}
                />
                //...
            </Form>
        </Canvas>
    );
}

And then I am also building something like this where I would like the user to have the option with ā€œreferenceā€ to choose from PDFs inside the media asset library, add a new one, or use an external link

div className={"row-container"}>
                    {
                        allReferences.map((currentReference, index) => {

                            return (
                                <>
                                    <div className={"row"}>
                                   ///.....
                                           <div className="referenceSection">
                                            <label htmlFor="reference">Reference:</label><br/>
                                            <input
                                                id="reference"
                                                type="text"
                                                value={currentReference.link}  ///I would like this, instead of being a manually inserted url to have the option, that would allow the user to choose from PDFs inside the media asset library, upload a new PDF, or use an external url link
                                                onChange={(e) => updateReference({link: e.currentTarget.value}, index)}
                                            />
                                        </div>
                                    </div>
                                    <div className={"row"}>
                                        <div className="addDeleteSection"><Button
                                            onClick={() => deleteReference(index)}>Delete This</Button></div>
                                        <div className="addDeleteSection"><Button
                                            onClick={() => addNewReferenceAtIndex(index)}>Add New after this</Button>
                                        </div>
                                    </div>

                                </>

Hey @chrispepper1989,

For the first part of it (choosing an upload within a plugin), did you already try ctx.selectUpload()? It should be available in every kind of ctx, but let me know if itā€™s not working somewhere.

For the second part, can please you clarify where that code is used? Is it still inside a plugin? If so, you should still be able to access ctx.selectUpload() in a change handler, Maybe make the <input/> a button instead, and then render its returned assetā€™s filename or thumbnail? That way your user can just choose an upload using our standard interface and then see (for verification) which one they picked, without having to manually copy & paste any IDs.

If itā€™s not inside a plugin (like itā€™s on a frontend outside of the Dato UI altogether, some external webpage), youā€™ll have to use the CMA to query uploads instead.

Does that help?

1 Like

Thank you Roger! that is certainty what I was looking for, ideally I would replicate the functionality of the ā€œDrag a file hereā€ UI component within Dato. Which I believe I can do through ctx.selectUpload and client.uploads.createFromFileOrBlob :+1:

p.s. it possible to send a filter to the ctx.selectUpload? e.g. ā€œfile is pdfā€

Not currently, sorry :frowning:

It could be a feature request: https://community.datocms.com/c/feature-requests/22

Iā€™ll also make a note of it internally for the next round of plugin updates!