refactor: update sidecar to use new apis

This commit is contained in:
ByteAtATime 2025-11-22 17:56:17 -08:00
parent 22e519f286
commit 38db5a9783
No known key found for this signature in database

View file

@ -1,159 +1,8 @@
import React from "react";
import ReactJsxRuntime, { jsx } from "react/jsx-runtime";
import type * as RaycastApiType from "@raycast/api";
import ReactJsxRuntime from "react/jsx-runtime";
import { invokeCallback, updateContainer } from "./reconciler";
import { Icon } from "./icons";
import { NavigationRoot, useNavigation } from "./navigation";
import * as protocol from "./protocol";
const LaunchType = {
UserInitiated: "userInitiated",
Background: "background",
} as const;
const ToastStyle = {
Success: "SUCCESS",
Failure: "FAILURE",
Animated: "ANIMATED",
} as const;
export type ToastOptions = {
style?: (typeof ToastStyle)[keyof typeof ToastStyle];
title: string;
message?: string;
};
const createComponent = (name: string) => {
const ComponentFactory = ({
key,
...props
}: {
children?: React.ReactNode;
key?: string | number;
}) => {
return jsx(name as React.ElementType, props, key);
};
ComponentFactory.displayName = name;
return ComponentFactory;
};
const Grid = createComponent("Grid");
const GridSection = createComponent("Grid.Section");
const GridItem = createComponent("Grid.Item");
Object.assign(Grid, {
Section: GridSection,
Item: GridItem,
});
const ActionPanel = createComponent("ActionPanel");
const ActionPanelSection = createComponent("ActionPanel.Section");
Object.assign(ActionPanel, {
Section: ActionPanelSection,
});
const Action = createComponent("Action");
const Detail = createComponent("Detail");
class Cache {
private namespace: string;
private subscribers: Set<RaycastApiType.Cache.Subscriber>;
constructor(options?: RaycastApiType.Cache.Options) {
this.namespace = options?.namespace || "default";
this.subscribers = new Set();
}
public async get(key: string): Promise<string | undefined> {
const result = await protocol.cacheGet(this.namespace, key);
return result === null ? undefined : result;
}
public async has(key: string): Promise<boolean> {
return await protocol.cacheHas(this.namespace, key);
}
public async set(key: string, data: string): Promise<void> {
await protocol.cacheSet(this.namespace, key, data);
this.notifySubscribers(key, data);
}
public async remove(key: string): Promise<boolean> {
const removed = await protocol.cacheRemove(this.namespace, key);
if (removed) {
this.notifySubscribers(key, undefined);
}
return removed;
}
public async clear(
options: { notifySubscribers: boolean } = { notifySubscribers: true }
): Promise<void> {
await protocol.cacheClear(this.namespace);
if (options.notifySubscribers) {
this.notifySubscribers(undefined, undefined);
}
}
public async isEmpty(): Promise<boolean> {
return await protocol.cacheIsEmpty(this.namespace);
}
public subscribe(
subscriber: RaycastApiType.Cache.Subscriber
): RaycastApiType.Cache.Subscription {
this.subscribers.add(subscriber);
return () => {
this.subscribers.delete(subscriber);
};
}
private notifySubscribers(key: string | undefined, data: string | undefined) {
for (const subscriber of this.subscribers) {
try {
subscriber(key, data);
} catch (e) {
console.error("Cache subscriber failed", e);
}
}
}
}
const raycastApi = {
showToast: (message: string) => {},
Grid,
ActionPanel,
Action,
Detail,
LaunchType,
useNavigation,
environment: {
launchType:
LaunchType.UserInitiated as RaycastApiType.LaunchType.UserInitiated,
assetsPath: "./test/assets",
} satisfies Partial<RaycastApiType.Environment>,
Toast: class {
public static Style = ToastStyle;
public primaryAction: RaycastApiType.Toast.ActionOptions | undefined;
constructor(private options: ToastOptions) {}
public show = async () => {
await protocol.showToast(this.options);
};
},
Cache,
// temporary defaults for pokédex extension
getPreferenceValues: () => ({
language: "9",
duration: "0",
artwork: "official",
}),
Icon,
};
import { NavigationRoot } from "./navigation";
import { raycastApi } from "./api";
export {
React,