Skip to content
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
36 changes: 15 additions & 21 deletions components/ui/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Image from "next/image";
import { usePostHog } from "posthog-js/react";
import { useState } from "react";
import type { FlowerType, HairCurliness } from "@/lib/themes/types";
import { buildAnimationPath, buildMarkdown } from "@/lib/utils/markdown";

interface PreviewCardProps {
username: string;
Expand All @@ -20,27 +21,20 @@ export default function PreviewCard({
hairColor,
curliness,
}: PreviewCardProps) {
const isHairTheme = hairColor !== undefined || curliness !== undefined;
const theme = isHairTheme ? "hair" : "flower";

let params = `theme=${theme}&quality=low`;

if (theme === "flower") {
params += `&flower=${flowerType || "default"}`;
if (flowerColor) {
params += `&color=${encodeURIComponent(flowerColor)}`;
}
} else if (theme === "hair") {
if (hairColor) {
params += `&color=${encodeURIComponent(hairColor)}`;
}
if (curliness) {
params += `&curliness=${curliness}`;
}
}

const animationUrl = `/api/${username}/animation?${params}`;
const markdown = `![GitStyle](https://git-style.vercel.app${animationUrl})`;
const animationUrl = buildAnimationPath({
username,
flowerType,
flowerColor,
hairColor,
curliness,
});
const markdown = buildMarkdown({
username,
flowerType,
flowerColor,
hairColor,
curliness,
});

const [isLoading, setIsLoading] = useState<boolean>(true);
const [hasError, setHasError] = useState<boolean>(false);
Expand Down
44 changes: 44 additions & 0 deletions lib/utils/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { FlowerType, HairCurliness } from "@/lib/themes/types";

export const SITE_URL = "https://git-style.vercel.app";

export interface AnimationOptions {
username: string;
flowerType?: FlowerType;
flowerColor?: string;
hairColor?: string;
curliness?: HairCurliness;
}

export function buildAnimationPath({
username,
flowerType,
flowerColor,
hairColor,
curliness,
}: AnimationOptions): string {
const isHairTheme = hairColor !== undefined || curliness !== undefined;
const theme = isHairTheme ? "hair" : "flower";

let params = `theme=${theme}&quality=low`;

if (theme === "flower") {
params += `&flower=${flowerType || "default"}`;
if (flowerColor) {
params += `&color=${encodeURIComponent(flowerColor)}`;
}
} else {
if (hairColor) {
params += `&color=${encodeURIComponent(hairColor)}`;
}
if (curliness) {
params += `&curliness=${curliness}`;
}
}

return `/api/${username}/animation?${params}`;
}

export function buildMarkdown(options: AnimationOptions): string {
return `[![GitStyle](${SITE_URL}${buildAnimationPath(options)})](${SITE_URL})`;
}
Loading