updated plasmic package to work with new hooks + added example

This commit is contained in:
dswbx
2024-12-18 08:41:11 +01:00
parent 1631bbb754
commit c4138ef823
34 changed files with 491 additions and 334 deletions

View File

@@ -16,6 +16,10 @@ export class SystemApi extends ModuleApi<any> {
};
}
readConfig() {
return this.get<{ version: number } & ModuleConfigs>("config");
}
readSchema(options?: { config?: boolean; secrets?: boolean }) {
return this.get<ApiSchemaResponse>("schema", {
config: options?.config ? 1 : 0,

View File

@@ -12,44 +12,38 @@ export type ClientProviderProps = {
};
export const ClientProvider = ({ children, baseUrl, user }: ClientProviderProps) => {
const [actualBaseUrl, setActualBaseUrl] = useState<string | null>(null);
//const [actualBaseUrl, setActualBaseUrl] = useState<string | null>(null);
const winCtx = useBkndWindowContext();
const _ctx_baseUrl = useBaseUrl();
let actualBaseUrl = baseUrl ?? _ctx_baseUrl ?? "";
try {
const _ctx_baseUrl = useBaseUrl();
if (_ctx_baseUrl) {
console.warn("wrapped many times");
setActualBaseUrl(_ctx_baseUrl);
if (!baseUrl) {
if (_ctx_baseUrl) {
actualBaseUrl = _ctx_baseUrl;
console.warn("wrapped many times, take from context", actualBaseUrl);
} else if (typeof window !== "undefined") {
actualBaseUrl = window.location.origin;
console.log("setting from window", actualBaseUrl);
}
}
} catch (e) {
console.error("error", e);
}
useEffect(() => {
// Only set base URL if running on the client side
if (typeof window !== "undefined") {
setActualBaseUrl(baseUrl || window.location.origin);
}
}, [baseUrl]);
if (!actualBaseUrl) {
// Optionally, return a fallback during SSR rendering
return null; // or a loader/spinner if desired
console.error("error .....", e);
}
const api = new Api({ host: actualBaseUrl, user: user ?? winCtx.user });
return (
<ClientContext.Provider value={{ baseUrl: actualBaseUrl, api }}>
<ClientContext.Provider value={{ baseUrl: api.baseUrl, api }}>
{children}
</ClientContext.Provider>
);
};
export const useApi = (host?: ApiOptions["host"]) => {
export const useApi = (host?: ApiOptions["host"]): Api => {
const context = useContext(ClientContext);
if (host && host !== context.baseUrl) {
return new Api({ host });
if (!context?.api || (host && host.length > 0 && host !== context.baseUrl)) {
return new Api({ host: host ?? "" });
}
return context.api;

View File

@@ -15,7 +15,7 @@ import {
} from "@mantine/core";
import { twMerge } from "tailwind-merge";
// default: https://github.com/mantinedev/mantine/blob/master/src/mantine-core/src/core/MantineProvider/default-theme.ts
// default: https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/core/src/core/MantineProvider/default-theme.ts
export function createMantineTheme(scheme: "light" | "dark"): {
theme: ReturnType<typeof createTheme>;