chore(client/jsx): use different method for launcher widget defs

This commit is contained in:
Elian Doran 2025-12-21 10:26:20 +02:00
parent 783b5ac8e3
commit 9071e54bfe
No known key found for this signature in database
2 changed files with 17 additions and 5 deletions

View File

@ -14,6 +14,11 @@ export interface WidgetDefinitionWithType extends WidgetDefinition {
type: "preact-widget" type: "preact-widget"
} }
export interface LauncherWidgetDefinitionWithType {
type: "preact-launcher-widget"
render: () => VNode
}
export const preactAPI = Object.freeze({ export const preactAPI = Object.freeze({
// Core // Core
h, h,
@ -31,6 +36,13 @@ export const preactAPI = Object.freeze({
}; };
}, },
defineLauncherWidget(definition: Omit<LauncherWidgetDefinitionWithType, "type">) {
return {
type: "preact-launcher-widget",
...definition
};
},
RightPanelWidget, RightPanelWidget,
...hooks, ...hooks,

View File

@ -4,7 +4,7 @@ import appContext, { CommandNames } from "../../components/app_context";
import FNote from "../../entities/fnote"; import FNote from "../../entities/fnote";
import date_notes from "../../services/date_notes"; import date_notes from "../../services/date_notes";
import dialog from "../../services/dialog"; import dialog from "../../services/dialog";
import { WidgetDefinition } from "../../services/frontend_script_api_preact"; import { LauncherWidgetDefinitionWithType } from "../../services/frontend_script_api_preact";
import { t } from "../../services/i18n"; import { t } from "../../services/i18n";
import toast from "../../services/toast"; import toast from "../../services/toast";
import { getErrorMessage, isMobile } from "../../services/utils"; import { getErrorMessage, isMobile } from "../../services/utils";
@ -122,7 +122,7 @@ export function QuickSearchLauncherWidget() {
export function CustomWidget({ launcherNote }: LauncherNoteProps) { export function CustomWidget({ launcherNote }: LauncherNoteProps) {
const [ widgetNote ] = useNoteRelationTarget(launcherNote, "widget"); const [ widgetNote ] = useNoteRelationTarget(launcherNote, "widget");
const [ widget, setWidget ] = useState<BasicWidget | NoteContextAwareWidget | WidgetDefinition>(); const [ widget, setWidget ] = useState<BasicWidget | NoteContextAwareWidget | LauncherWidgetDefinitionWithType>();
const parentComponent = useContext(ParentComponent) as BasicWidget | null; const parentComponent = useContext(ParentComponent) as BasicWidget | null;
parentComponent?.contentSized(); parentComponent?.contentSized();
@ -151,8 +151,8 @@ export function CustomWidget({ launcherNote }: LauncherNoteProps) {
return ( return (
<div> <div>
{widget && ( {widget && (
("type" in widget && widget.type === "preact-widget") ("type" in widget && widget.type === "preact-launcher-widget")
? <ReactWidgetRenderer widget={widget as WidgetDefinition} /> ? <ReactWidgetRenderer widget={widget as LauncherWidgetDefinitionWithType} />
: <LegacyWidgetRenderer widget={widget as BasicWidget} /> : <LegacyWidgetRenderer widget={widget as BasicWidget} />
)} )}
</div> </div>
@ -167,7 +167,7 @@ export function LegacyWidgetRenderer({ widget }: { widget: BasicWidget }) {
return widgetEl; return widgetEl;
} }
function ReactWidgetRenderer({ widget }: { widget: WidgetDefinition }) { function ReactWidgetRenderer({ widget }: { widget: LauncherWidgetDefinitionWithType }) {
const El = widget.render; const El = widget.render;
return <El />; return <El />;
} }