mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import { createContext, useContext } from 'react';
|
|
import { clearExtensionError, reportExtensionError } from '@/extensions/registry';
|
|
|
|
export const ExtensionContext = createContext<{ extensionId: string; context: string } | null>(null);
|
|
|
|
export function useExtensionCallback<TArgs extends readonly unknown[]>(
|
|
event: string,
|
|
callback: (...args: TArgs) => void | Promise<void>
|
|
): (...args: TArgs) => void {
|
|
const mount = useContext(ExtensionContext);
|
|
if (!mount) throw new Error('Extension callbacks require an extension mount.');
|
|
const context = `${mount.context}, event "${event}"`;
|
|
return (...args: TArgs): void => {
|
|
try {
|
|
const result = callback(...args);
|
|
if (result) {
|
|
void result.then(
|
|
() => clearExtensionError(mount.extensionId, context),
|
|
(error) => reportExtensionError(mount.extensionId, context, error)
|
|
);
|
|
} else {
|
|
clearExtensionError(mount.extensionId, context);
|
|
}
|
|
} catch (error) {
|
|
reportExtensionError(mount.extensionId, context, error);
|
|
}
|
|
};
|
|
}
|