From bc936e0e169e279e7edd27e67d4f16ef47693016 Mon Sep 17 00:00:00 2001 From: jayesh Date: Sun, 17 Aug 2025 22:06:04 +0530 Subject: [PATCH] feat(templates): add helper for template source URL --- src/lib/helpers/templateSource.ts | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/lib/helpers/templateSource.ts diff --git a/src/lib/helpers/templateSource.ts b/src/lib/helpers/templateSource.ts new file mode 100644 index 000000000..5e34a8e1b --- /dev/null +++ b/src/lib/helpers/templateSource.ts @@ -0,0 +1,32 @@ +export type Template = { + key?: string; // template slug, e.g. "magic-portfolio" + frameworks?: Array<{ + key?: string; + providerRootDirectory?: string; + }>; // e.g. [{ key: "nextjs", providerRootDirectory: "./nextjs/magic-portfolio" }] + providerOwner?: string; // e.g. "appwrite" + providerRepositoryId?: string; // e.g. "templates-for-sites" or "template-for-store" + repository?: string; // if API already returns full URL, use it + repoUrl?: string; // alias some builds may use + source?: string; // alias some builds may use +}; + +export function getTemplateSourceUrl(template: Template): string | null { + // 1) Prefer a direct URL if the API already provides it + const direct = template.repository || template.repoUrl || template.source; + if (direct) return direct; + + // 2) Ensure we have the parts to build the GitHub URL + const owner = template.providerOwner || "appwrite"; + const repo = template.providerRepositoryId || "templates-for-sites"; + const rootDir = template.frameworks?.[0]?.providerRootDirectory ?? "./"; + + // strip leading "./" + const cleanDir = rootDir.startsWith("./") ? rootDir.slice(2) : rootDir; + + if (!cleanDir || cleanDir === "") { + return `https://github.com/${owner}/${repo}/tree/main`; + } + + return `https://github.com/${owner}/${repo}/tree/main/${cleanDir}`; +}