Custom option in the topic admin panel?

AI-generated summary

Custom Option in the Topic Admin Panel

Kampfkarren is having trouble adding a custom button to the admin panel for each post. After 15 hours of research, they’re stuck on getting the button to appear in the correct location and be clickable. They’ve tried using api.decorateWidget('topic-admin-menu:after'), but it adds the button to the wrong element and can’t be clicked.

Kampfkarren eventually figured out the solution by using api.decorateWidget('topic-admin-menu:adminMenuButtons') to add the button to the correct location, and api.attachWidgetAction('topic-admin-menu', 'actionTlLock') to make it clickable. The corrected code is:

api.decorateWidget('topic-admin-menu:adminMenuButtons', (decorator) => {
  return {
    icon: 'ban',
    fullLabel: 'tl_post_lock.button_label',
    action: 'actionTlLock'
  }
})

api.attachWidgetAction('topic-admin-menu', 'actionTlLock', () => {
  // code
})

The past 15 hours for me have been a nightmare of reading existing plugins and source code just to get extremely basic functionality to write my first plugin. I need help.

Specifically, I need help just getting a new button in the admin panel for each post (the button in the bottom left). The most I’ve gotten done is putting the button in the same element as the widget…

But notice the bullet point. This is because what I’ve created isn’t going in the ul as it should be. I have no clue how to fix this.

(the div is my option).

I did that with this code:

import { withPluginApi } from 'discourse/lib/plugin-api';
import showModal from "discourse/lib/show-modal";

export default {
	name: 'tl-post-lock',
	initialize() {
		withPluginApi('0.8.24', function(api) {
			const user = api.getCurrentUser()

			if(user.trust_level >= api.container.lookup('site-settings:main').tl_lock_minimum) {
				// User is allowed to see the button

				api.decorateWidget('topic-admin-menu:after', (decorator) => {
					// Adds the button to the admin menu
					return decorator.attach('admin-menu-button', {
						icon: 'ban',
						fullLabel: 'tl_post_lock.button_label',
						action: 'actionTlLock'
					})
				})
				
			}
		})
	}
}

After this, there’s no way to get it to actually click. After the decorateWidget block, if I write any form of this:

image

I get the following completely unhelpful error with a line and character that doesn’t even EXIST.

Along with a bunch of other spam.

I’m losing my sanity. How do I just add a button to the bottom left admin panel so I can run code when I click on it?

Finally figured it out.

api.decorateWidget('topic-admin-menu:adminMenuButtons', (decorator) => {
					// Adds the button to the admin menu
					return {
						icon: 'ban',
						fullLabel: 'tl_post_lock.button_label',
						action: 'actionTlLock'
					}
				})
				
				api.attachWidgetAction('topic-admin-menu', 'actionTlLock', () => {
					// code
				})