payloadcms / plugin-seo

The official SEO plugin for Payload
97 stars 15 forks source link

Auto-generate buttons are there even if no generateTitle/generateDescription/etc configured #34

Closed zanami closed 1 year ago

zanami commented 1 year ago

Clicking on 'Auto-generate' does nothing in this case. Just a minor UI issue I guess.

dragos199993 commented 1 year ago

Hey 👋 Is it supposed to be something like this? image

Did a fix on MetaTitle, but wanted to be sure this is the correct approach. If I get the green light from one of the maintainers I can open a PR.

The solution will be to check in each component: MetaTitle, MetaDescription and MetaImage if we have value on the field. This can be achieved by using a useState and an useEffect that updates this value based on the generate* function response:

const [fieldVisible, setFieldVisible] = useState(false);

    const { generateTitle } = pluginConfig;

    if (typeof generateTitle === "function") {
      setFieldVisible(!!(await generateTitle({ doc: { ...fields }, locale })));
    }
  }, []);

After that we only add the "if statement" in JSX to disable the Auto-generate button:

{fieldVisible && (
            <>
              &nbsp; &mdash; &nbsp;
              <button
                onClick={regenerateTitle}
                type="button"
                style={{
                  padding: 0,
                  background: "none",
                  border: "none",
                  backgroundColor: "transparent",
                  cursor: "pointer",
                  textDecoration: "underline",
                  color: "currentcolor",
                }}
              >
                Auto-generate
              </button>
            </>
          )}

However since this logic will be repeated, maybe it can be extracted in a small custom hook?

jacobsfletch commented 1 year ago

@dragos199993 that's pretty much it but to make this much simpler just add that condition directly to the JSX and don't use state, like this:

{typeof generateTitle === 'function' && (
  <>
    &nbsp; &mdash; &nbsp;
    <button
      onClick={regenerateTitle}
      type="button"
      style={{
        padding: 0,
        background: "none",
        border: "none",
        backgroundColor: "transparent",
        cursor: "pointer",
        textDecoration: "underline",
        color: "currentcolor",
      }}
    >
      Auto-generate
    </button>
  </>
)}

I'd happily merge a PR if you wanted to give this a shot!

edtorba commented 1 year ago

Am on 1.0.10 and it's still showing 👀 My config:

seo({
    collections: [
      'pages',
    ],
    uploadsCollection: 'media',
  },
),
Screenshot 2023-05-13 at 11 54 17
jacobsfletch commented 1 year ago

Hey @edtorba looks like this PR was merged in but not released. I'll cut a new release for this as soon as possible.

jacobsfletch commented 1 year ago

@edtorba this was just released in 1.0.11 🎉