Skip to content

Commit 8cd7520

Browse files
authored
Fix Figma runner endpoint persistence (#93)
1 parent 731fb73 commit 8cd7520

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

plugins/figma-to-wordpress/src/code.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { NormalizedAsset, NormalizedDocument, NormalizedSceneNode, PluginTo
33

44
figma.showUI(__html__, { width: 420, height: 420, themeColors: true });
55

6+
const runnerEndpointStorageKey = "figma-to-wordpress-runner-endpoint";
7+
68
function postToUi(message: PluginToUiMessage) {
79
figma.ui.postMessage(message);
810
}
@@ -196,6 +198,17 @@ figma.ui.onmessage = async (message: UiToPluginMessage) => {
196198
return;
197199
}
198200

201+
if (message.type === "get-runner-endpoint") {
202+
const endpoint = await figma.clientStorage.getAsync(runnerEndpointStorageKey);
203+
postToUi({ type: "runner-endpoint", endpoint: typeof endpoint === "string" ? endpoint : null });
204+
return;
205+
}
206+
207+
if (message.type === "set-runner-endpoint") {
208+
await figma.clientStorage.setAsync(runnerEndpointStorageKey, message.endpoint);
209+
return;
210+
}
211+
199212
if (message.type === "open-wordpress") {
200213
figma.openExternal(message.url);
201214
return;

plugins/figma-to-wordpress/src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export type PluginToUiMessage =
8080
selection: NormalizedSelection | null;
8181
artifact: GeneratedArtifact | null;
8282
}
83+
| {
84+
type: "runner-endpoint";
85+
endpoint: string | null;
86+
}
8387
| {
8488
type: "error";
8589
message: string;
@@ -89,6 +93,13 @@ export type UiToPluginMessage =
8993
| {
9094
type: "refresh-document";
9195
}
96+
| {
97+
type: "get-runner-endpoint";
98+
}
99+
| {
100+
type: "set-runner-endpoint";
101+
endpoint: string;
102+
}
92103
| {
93104
type: "open-wordpress";
94105
url: string;

plugins/figma-to-wordpress/src/ui.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const playgroundButton = document.querySelector<HTMLButtonElement>("#playground"
1010
const runnerEndpointInput = document.querySelector<HTMLInputElement>("#runner-endpoint");
1111

1212
const defaultRunnerEndpoint = "http://localhost:8882/wp-json/static-site-importer/v1/import-figma";
13-
const runnerEndpointStorageKey = "figma-to-wordpress-runner-endpoint";
1413

1514
function log(message: string, details?: unknown) {
1615
if (typeof details === "undefined") {
@@ -54,7 +53,7 @@ function runnerEndpoint(): string {
5453
function persistRunnerEndpoint() {
5554
const endpoint = runnerEndpoint();
5655

57-
localStorage.setItem(runnerEndpointStorageKey, endpoint);
56+
sendToPlugin({ type: "set-runner-endpoint", endpoint });
5857
}
5958

6059
async function openPlayground() {
@@ -134,6 +133,13 @@ window.onmessage = (event: MessageEvent<{ pluginMessage?: PluginToUiMessage }>)
134133
return;
135134
}
136135

136+
if (message.type === "runner-endpoint") {
137+
if (runnerEndpointInput && message.endpoint) {
138+
runnerEndpointInput.value = message.endpoint;
139+
}
140+
return;
141+
}
142+
137143
if (message.type === "error") {
138144
setStatus(message.message);
139145
}
@@ -144,8 +150,9 @@ playgroundButton?.addEventListener("click", () => void openPlayground());
144150
runnerEndpointInput?.addEventListener("change", persistRunnerEndpoint);
145151

146152
if (runnerEndpointInput) {
147-
runnerEndpointInput.value = localStorage.getItem(runnerEndpointStorageKey) || defaultRunnerEndpoint;
153+
runnerEndpointInput.value = defaultRunnerEndpoint;
148154
}
149155

150156
log("UI loaded.", { endpoint: runnerEndpoint() });
157+
sendToPlugin({ type: "get-runner-endpoint" });
151158
sendToPlugin({ type: "refresh-document" });

plugins/figma-to-wordpress/src/wordpress-runner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface WordPressRunnerResponse {
1717
preview_session?: Record<string, unknown>;
1818
materialization?: Record<string, unknown>;
1919
error?: WordPressRunnerError;
20+
message?: string;
2021
}
2122

2223
const runnerTimeoutMs = 15000;
@@ -48,11 +49,11 @@ export async function createWordPressPreview(endpoint: string, artifact: Generat
4849
const data = await response.json().catch(() => null) as WordPressRunnerResponse | null;
4950

5051
if (!response.ok) {
51-
throw new Error(data?.error?.message || `WordPress runner failed with HTTP ${response.status}.`);
52+
throw new Error(data?.error?.message || data?.message || `WordPress runner failed with HTTP ${response.status}.`);
5253
}
5354

5455
if (!data?.success || !data.open_url) {
55-
throw new Error(data?.error?.message || "WordPress runner did not return a Playground URL.");
56+
throw new Error(data?.error?.message || data?.message || "WordPress runner did not return a Playground URL.");
5657
}
5758

5859
return data;

0 commit comments

Comments
 (0)