Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk,react): env-specific template preview #611

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tiny-cameras-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@carrot-kpi/react": minor
"@carrot-kpi/sdk": minor
---

Env-specific template previews
11 changes: 5 additions & 6 deletions packages/react/src/hooks/useTemplateModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { useFederatedModuleContainer } from "./useFederatedModuleContainer";
import { type State, useSelector } from "@carrot-kpi/shared-state";
import { useIPFSGatewayURL } from "./useIPFSGatewayURL";
import { useStagingMode } from "./useStagingMode";
import { useAccount } from "wagmi";
import type {
RemoteComponentProps,
Expand Down Expand Up @@ -65,18 +64,18 @@ export const useTemplateModule = <
: state.preferences.oracleTemplateBaseURL,
);
const { chain } = useAccount();
const stagingMode = useStagingMode();
const ipfsGatewayURL = useIPFSGatewayURL();

const baseURL = useMemo(() => {
if (!template || !chain) return undefined;
let root;
let root: string;
if (customBaseURL) root = customBaseURL;
else if (stagingMode && template.specification.stagingURL)
root = template.specification.stagingURL;
else if (template.specification.previewUrl?.[chain.environment])
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
root = template.specification.previewUrl[chain.environment]!;
else root = `${ipfsGatewayURL}/ipfs/${template.specification.cid}`;
return root.endsWith("/") ? `${root}${type}` : `${root}/${type}`;
}, [chain, customBaseURL, ipfsGatewayURL, stagingMode, template, type]);
}, [chain, customBaseURL, ipfsGatewayURL, template, type]);
const { loading: loadingFederatedModule, container } =
useFederatedModuleContainer({ type, baseUrl: baseURL, entry });

Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/entities/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Address } from "viem";
import type { Environment } from "../commons";

export interface OnChainTemplate {
addrezz: Address;
Expand All @@ -24,7 +25,10 @@ export class TemplateSpecification {
public readonly tags: string[],
public readonly repository: string,
public readonly commitHash: string,
public readonly stagingURL?: string,
public readonly previewUrl?: {
[Environment.Development]?: string;
[Environment.Staging]?: string;
},
) {}
}
export class ResolvedTemplate {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/fetcher/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class CoreFetcher implements ICoreFetcher {
parsedTemplateSpecification.tags,
parsedTemplateSpecification.repository,
parsedTemplateSpecification.commitHash,
parsedTemplateSpecification.stagingURL,
parsedTemplateSpecification.previewUrl,
);
return ResolvedTemplate.from(template, specification);
}),
Expand Down
Loading