Skip to content

Commit

Permalink
editing, saving, canceling; moving things around
Browse files Browse the repository at this point in the history
  • Loading branch information
khill-fbmc committed Oct 20, 2024
1 parent 594563e commit 0b417dd
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 313 deletions.
8 changes: 4 additions & 4 deletions src/hooks/useActiveApp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useStore } from "./useStore";

export function useActiveApp() {
const apps = useStore((s) => s.apps);
const activeAppName = useStore((s) => s.activeAppName);

return apps.find((app) => app.name === activeAppName);
const getActiveApp = useStore((s) => s.getActiveApp);
const updateActiveApp = useStore((s) => s.updateActiveApp);
const app = getActiveApp();
return [app, updateActiveApp] as const;
}
15 changes: 15 additions & 0 deletions src/hooks/useAppList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useStore } from "./useStore";

export function useAppList() {
const apps = useStore((s) => s.apps);
const addApp = useStore((s) => s.addApp);
const removeApp = useStore((s) => s.removeApp);
const updateApp = useStore((s) => s.updateApp);

return {
apps,
addApp,
removeApp,
updateApp,
};
}
19 changes: 0 additions & 19 deletions src/hooks/useChromeStorage.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/hooks/useComposedUrl.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/hooks/useDebounce.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/hooks/useRetoolAppStore.ts

This file was deleted.

17 changes: 10 additions & 7 deletions src/hooks/useRetoolAppUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ function composeAppUrl(domain: string, app: RetoolApp): URL {
const path = `${app.public ? "p" : "app"}/${app.name}`;
const url = new URL(path, base);

app.query.forEach((q) => url.searchParams.append(q.param, q.value));
if (app?.query?.length > 0) {
app.query.forEach((q) => url.searchParams.append(q.param, q.value));
}

url.searchParams.append("_version", app.version);
url.searchParams.append("_environment", app.env);

if (app.hash.length === 0) return url;

const hashParams = new URLSearchParams(
app.hash.map((h) => [h.param, h.value])
);
if (app?.hash?.length === 0) return url;

url.hash = hashParams.toString();
if (app?.hash?.length > 0) {
const hashParams = new URLSearchParams(
app.hash.map((h) => [h.param, h.value])
);
url.hash = hashParams.toString();
}

return url;
}
Expand Down
3 changes: 0 additions & 3 deletions src/hooks/useRetoolWorkflowUrl.ts

This file was deleted.

36 changes: 25 additions & 11 deletions src/hooks/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { DEMO_APPS, INSPECTOR_APP } from "@/lib/EmbeddableApps";

import type { RetoolApp } from "@/types/extension";

type TabKeys = "config" | "storage" | "workflow" | "json";

export type State = {
currentTab: TabKeys;
domain: string;
isEditing: boolean;
activeAppName: RetoolApp["name"] | undefined;
Expand All @@ -19,21 +22,24 @@ export type State = {
};

export type Actions = {
setDomain: (domain: State["domain"]) => void;
setActiveApp: (activeAppName: State["activeAppName"]) => void;
reset: () => void;
addApp: (app: RetoolApp) => void;
updateApp: (name: string, props: Partial<RetoolApp>) => void;
removeApp: (name: RetoolApp["name"]) => void;
updateApp: (name: RetoolApp["name"], props: Partial<RetoolApp>) => void;
updateActiveApp: (props: Partial<RetoolApp>) => void;
setDomain: (domain: State["domain"]) => void;
getActiveApp: () => RetoolApp | undefined;
setActiveApp: (activeAppName: State["activeAppName"]) => void;
setEditMode: (state: boolean) => void;
setActiveTab: (tab: TabKeys) => void;
updateWorkflow: (workflow: Partial<State["workflow"]>) => void;
getActiveApp: () => RetoolApp | undefined;
reset: () => void;
};

export const STORAGE_KEY = "app-embedder-for-retool2";
export const STORAGE_KEY = "app-embedder-for-retool4";

const initialState: State = {
domain: "fortunabmc",
currentTab: "config",
isEditing: false,
activeAppName: INSPECTOR_APP["name"],
workflow: {
Expand All @@ -48,17 +54,29 @@ export const useStore = create<State & Actions>()(
(set, get) => ({
...initialState,
reset: () => set(initialState),
setActiveTab: (tab) => set({ currentTab: tab }),
setDomain: (domain) => set(() => ({ domain })),
setEditMode: (isEditing) => set(() => ({ isEditing })),
setActiveApp: (activeAppName) => set(() => ({ activeAppName })),
addApp: (app) => set((state) => ({ apps: [...state.apps, app] })),
removeApp: (name) => {
set((state) => ({
apps: state.apps.filter((app) => app.name !== name),
}));
},
updateApp: (name, props) => {
set((state) => ({
apps: state.apps.map((app) => {
return app.name === name ? { ...app, ...props } : app;
}),
}));
},
getActiveApp: () => {
const activeAppName = get().activeAppName;
return get().apps.find((app) => app.name === activeAppName);
},
setActiveApp: (name) => {
set(() => ({ activeAppName: name }));
},
updateActiveApp: (props) => {
const name = get().activeAppName;
if (name) {
Expand All @@ -71,10 +89,6 @@ export const useStore = create<State & Actions>()(
workflow: { ...state.workflow, ...props },
}));
},
getActiveApp: () => {
const activeAppName = get().activeAppName;
return get().apps.find((app) => app.name === activeAppName);
},
}),
{
name: STORAGE_KEY,
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useTabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useStore } from "./useStore";

export function useTabs() {
const currentTab = useStore((s) => s.currentTab);
const setCurrentTab = useStore((s) => s.setActiveTab);
return [currentTab, setCurrentTab] as const;
}
67 changes: 30 additions & 37 deletions src/hooks/useWorkflow.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
import { useState } from "react";
import useSWR from "swr";

import type { RetoolApp } from "@/types/extension";

export function useWorkflow(url: string, apiKey: string) {
const [workflowUrl, setWorkflowUrl] = useState<string>(url);
const [workflowApiKey, setWorkflowApiKey] = useState<string>(apiKey);
export function useWorkflowData(apiKey: string, id: string) {
const [isLoading, setIsLoading] = useState(false);
const [workflowData, setWorkflowData] = useState<RetoolApp[]>([]);
const [workflowError, setWorkflowError] = useState<string | undefined>();

const fetchWorkflowData = async () => {
setIsLoading(true);
try {
const url = `https://api.retool.com/v1/workflows/${id}/startTrigger`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Workflow-Api-Key": apiKey,
},
});

const result = (await res.json()) as { apps: RetoolApp[] };

setWorkflowData(result?.apps);
} catch (e) {
setWorkflowError((e as Error).message);
}
setIsLoading(false);
};

return {
state: {
workflowUrl,
workflowApiKey,
fetchWorkflowData,
workflow: {
isLoading,
data: workflowData,
error: workflowError,
},
setWorkflowUrl,
setWorkflowApiKey,
...useSWR<WorkflowResult>(url, () => callWorkflow(url, workflowApiKey)),
};
}

async function callWorkflow(
url: string,
workflowApiKey: string
): Promise<WorkflowResult> {
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Workflow-Api-Key": workflowApiKey,
},
});

const data = (await res.json()) as { apps: RetoolApp[] };

if (!data?.apps) {
throw new Error(
"The returned object from the workflow must have `apps` as a property."
);
}

return { apps: data.apps };
}

type WorkflowResult = {
apps: RetoolApp[];
};
32 changes: 0 additions & 32 deletions src/hooks/useWorkflow2.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/lib/WorkflowDataFetcher.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/toast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import toast, { type ToastOptions, type ToastType } from "react-hot-toast";
import toast from "react-hot-toast";

type Message = Parameters<typeof toast>[0];

Expand Down
Loading

0 comments on commit 0b417dd

Please sign in to comment.