-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da68d5f
commit 29ac858
Showing
31 changed files
with
1,243 additions
and
529 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useExtensionState } from "./useExtensionState"; | ||
|
||
export function useComposedUrl() { | ||
return useExtensionState((state) => { | ||
const app = state.getActiveApp(); | ||
|
||
if (app) { | ||
const url = new URL( | ||
`${app.public ? "p" : "app"}/${app.name}}`, | ||
`https://${state.domain}.retool.com/` | ||
); | ||
app.query.forEach((q) => url.searchParams.append(q.param, q.value)); | ||
|
||
if (app.hash.length === 0) { | ||
return `${url.toString()}`; | ||
} | ||
|
||
const hashParams = new URLSearchParams(app.hash.map((h) => [h.param, h.value])); | ||
return `${url.toString()}#${hashParams.toString()}`; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { create } from "zustand"; | ||
import { createJSONStorage, persist } from "zustand/middleware"; | ||
|
||
import ChromeStateStorage from "../lib/chrome/ChromeStateStorage"; | ||
import { DEMO_APPS, INSPECTOR_APP } from "../pages/Options/EmbeddableApps"; | ||
|
||
import type { RetoolApp } from "../types"; | ||
|
||
type State = { | ||
domain: string; | ||
apps: RetoolApp[]; | ||
activeAppName: RetoolApp["name"] | undefined; | ||
// activeApp: RetoolApp | undefined; | ||
workflowUrl: string; | ||
workflowApiKey: string; | ||
}; | ||
|
||
interface Actions { | ||
reset: () => void; | ||
getActiveApp: () => RetoolApp | undefined; | ||
setDomain: (domain: State["domain"]) => void; | ||
setActiveApp: (name: State["domain"]) => void; | ||
addApp: (app: RetoolApp) => void; | ||
updateApp: (name: string, app: Partial<RetoolApp>) => void; | ||
} | ||
|
||
export const STORAGE_KEY = "app-embedder-for-retool"; | ||
|
||
const initialState: State = { | ||
domain: "", | ||
activeAppName: INSPECTOR_APP["name"], | ||
workflowUrl: "", | ||
workflowApiKey: "", | ||
apps: [INSPECTOR_APP, ...DEMO_APPS], | ||
}; | ||
|
||
export const useExtensionState = create<State & Actions>()( | ||
persist( | ||
(set, get) => ({ | ||
...initialState, | ||
reset: () => set(initialState), | ||
setDomain: (domain) => set(() => ({ domain })), | ||
setActiveApp: (name) => set(() => ({ activeAppName: name })), | ||
addApp: (app) => set((state) => ({ apps: [...state.apps, app] })), | ||
updateApp: (name, props) => { | ||
set((state) => ({ | ||
apps: state.apps.map((app) => (app.name === name ? { ...app, ...props } : app)), | ||
})); | ||
}, | ||
getActiveApp: () => get().apps.find((app) => app.name === get().activeAppName), | ||
}), | ||
{ | ||
name: STORAGE_KEY, | ||
storage: createJSONStorage(() => ChromeStateStorage), | ||
} | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { create } from "zustand"; | ||
|
||
import type { RetoolApp, UrlParamSpec } from "../types"; | ||
|
||
type Actions = { | ||
setAppId: (appName: string) => void; | ||
setEnvironment: (env: RetoolApp["env"]) => void; | ||
setVersion: (version: RetoolApp["version"]) => void; | ||
setHashParams: (spec: UrlParamSpec[]) => void; | ||
setQueryParams: (spec: UrlParamSpec[]) => void; | ||
updateParam: (which: "query" | "hash", index: number, spec: UrlParamSpec[]) => void; | ||
resetApp: () => void; | ||
}; | ||
|
||
const initialState: RetoolApp = { | ||
id: "", | ||
env: "development", | ||
version: "latest", | ||
hash: [], | ||
query: [], | ||
}; | ||
|
||
export const useRetoolAppStore = create<RetoolApp & Actions>()((set) => ({ | ||
...initialState, | ||
setAppId: (id) => set(() => ({ id })), | ||
setEnvironment: (env) => set(() => ({ env })), | ||
setVersion: (version) => set(() => ({ version })), | ||
setHashParams: (hash) => set(() => ({ hash })), | ||
setQueryParams: (query) => set(() => ({ query })), | ||
updateParam: (which, index, param) => | ||
set((state) => ({ | ||
hash: state[which].map((item, idx) => (idx === index ? { ...item, ...param } : item)), | ||
})), | ||
resetApp: () => set(() => initialState), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { StateStorage } from "zustand/middleware"; | ||
|
||
const ChromeStateStorage: StateStorage = { | ||
getItem, | ||
setItem, | ||
removeItem, | ||
}; | ||
|
||
export default ChromeStateStorage; | ||
|
||
function getItem(name: string): string | null | Promise<string | null> { | ||
return runtimePromise<string>((resolve, rejectIfRuntimeError) => { | ||
chrome.storage.sync.get(name, (items) => { | ||
rejectIfRuntimeError(); | ||
resolve(items[name]); | ||
}); | ||
}); | ||
} | ||
|
||
function setItem(name: string, value: string): unknown | Promise<unknown> { | ||
return runtimePromise((resolve, rejectIfRuntimeError) => { | ||
chrome.storage.sync.set({ [name]: value }, () => { | ||
rejectIfRuntimeError(); | ||
resolve(void 0); | ||
}); | ||
}); | ||
} | ||
|
||
function removeItem(name: string): unknown | Promise<unknown> { | ||
return runtimePromise((resolve, rejectIfRuntimeError) => { | ||
chrome.storage.sync.remove(name, () => { | ||
rejectIfRuntimeError(); | ||
resolve(void 0); | ||
}); | ||
}); | ||
} | ||
|
||
function runtimePromise<T = unknown>(handler: PromiseHandler<T>): Promise<T> { | ||
const { promise, resolve, reject } = Promise.withResolvers<T>(); | ||
const rejectIfRuntimeError = () => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError.message); | ||
} | ||
}; | ||
handler(resolve, rejectIfRuntimeError); | ||
return promise; | ||
} | ||
|
||
type Resolver<T> = (value: T | PromiseLike<T>) => void; | ||
|
||
type PromiseHandler<T> = (resolve: Resolver<T>, rejectIfRuntimeError: () => void) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { ChromeStorage } from "./chrome/ChromeStorage"; | ||
|
||
import type { ExtensionSettings, ParamEntry } from "../types"; | ||
import type { UrlParamSpec } from "../hooks/useExtensionState"; | ||
import type { ExtensionSettings } from "../types"; | ||
|
||
export type SerializedSettings = { | ||
urlParams: ParamEntry[]; | ||
hashParams: ParamEntry[]; | ||
urlParams: UrlParamSpec[]; | ||
hashParams: UrlParamSpec[]; | ||
} & Required<ExtensionSettings>; | ||
|
||
export const storage = new ChromeStorage<SerializedSettings>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const callWorkflow = async (url: string, workflowApiKey: string): Promise<string[]> => { | ||
const res = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Workflow-Api-Key": workflowApiKey, | ||
}, | ||
}); | ||
const data = (await res.json()) as { apps: string[] }; | ||
return data.apps ?? []; | ||
}; |
Oops, something went wrong.