#Filter Documents from enabledCollections in LinkFeature

8 messages · Page 1 of 1 (latest)

little crown
#

Okay, I know I can filter what collections are included in LinkFeature for lexical RichText editing. That's good. But, how do I filter further the documents within the collection.

My scenario:

My site is multi-tenant so there is a Tenant collection and every document is bound to a tenant collection. If adding a Link to the RichText content, I only want the user to be able to select a document from the same tenant the referencing document is in (it's all the same collection).

I know I can exclude other collections, of course security will limit only to those documents that the user has access to. However, when adding a link to a document in Tenant A, I only want Tenant A content to show up (even if the user has access to other tenants -- which my content team always will have). This prevent errors.

Do I need to rewrite LinkFeature to do this or is there a config I can use (if not why not)?

Example:

This document is in the "Nuevco" teannat. Right now it can create links to documents in both the Nuevco and Denice tenants (for testing I only setup two tenants). This is because I have access to both tenants, but documents from the Nuevco tenant should not be able to internal link to documents of the Denice tenant (an external link is fine if we want to do that but then the user will have to provide the full URL).

It would be nice if in addition to enabledCollections there was a filter handler.

With almost any level of hook this would be easy, either (a) the ability to create a Where clause for each collection based on the filter or (b) the ability to be called with both documents to see if it's acceptable.

NOTE: Option A would probably be most performant.

woeful sundialBOT
little crown
#

Okay so I'm trying to see what it would take to make the modifications needed. Can someone help me pull this project out and bringing into my own code so I can try to get this working. Then I can submit it back as a PR. But right now I'm tripping on every type of Typescript/JavaScript worst of it always goes wrong getting anything to work (right now I'm in webpack hell). Anyone know how to help get me through this?

little crown
#

I have a working example of this which supports somethign like this:

  {
            name: 'content',
            label: 'Content',
            type: 'richText',
            editor: lexicalEditor({
                features({ defaultFeatures, rootFeatures, }) {
                    return [
                        LinkFeature({
                            filterOptions: async (props) => {
                                console.log(`Link Feature filterOptions Called: ${props.relationTo}`);
                                console.log(`Currently editing document: ${props.id}`);
                                if (props.relationTo === Entity.TenantContent) {
                                    const doc = await getContentById(props.id as string);
                                    const tenant = doc?.metadata?.contentMetadata?.tenant;
                                    if (tenant) {
                                        const where: Where = {
                                            'metadata.contentMetadata.tenant': {
                                                equals: tenant
                                            },
                                        };
                                        return where;
                                    }
                                }
                                return true;
                            },
                            enabledCollections: [
                                Entity.TenantContent,
                            ]
                        }),
                    ];
                },
            }),
        },
little crown
#

@verbal hornet I replied for clarity on the discussion on GitHub please let me know when you can take a look.

little crown
#

So sorry, some how I refreshed and it didn't show me that, I didn't know you had replied. I'll check that out. I'll let you know if it works out for me.