Files
Stirling-PDF/frontend/editor/src/saas/hooks/usePlans.ts
T
Reece BrowneandClaude Opus 4.7 0a50e765b7 Restructure/frontend editor (#6404)
## Move editor under `frontend/editor/`

Pure restructure: `frontend/` becomes the workspace, `frontend/editor/`
holds
  the PDF editor. 1775 file renames + 40 wiring edits. No logic changes.

  ### Why

`frontend/` is currently the editor — its `src/`, `public/`,
`src-tauri/`,
  config files all sit at the root. Promoting `frontend/` to a
workspace and putting the editor in a sibling folder leaves room for
future
apps to drop in alongside it, sharing one `package.json` /
`node_modules` /
  lint config / Storybook.

  ### What moves

  frontend/
  ├── editor/                ← NEW: everything editor-specific
  │   ├── src/               ← was frontend/src/
  │   ├── public/            ← was frontend/public/
  │   ├── src-tauri/         ← was frontend/src-tauri/
│ ├── index.html, vite.config.ts, vitest.config.ts, playwright.config.ts
  │   ├── tsconfig*.json, tailwind.config.js, postcss.config.js
  │   ├── scripts/
  │   ├── .env, .env.desktop, .env.saas
  │   └── DeveloperGuide.md
├── package.json, package-lock.json, node_modules/ ← workspace install
  ├── eslint.config.mjs, .prettierrc, .prettierignore ← shared tooling
  ├── .gitignore
  └── README.md

  ### Wiring edits (40 files)

  - `.taskfiles/frontend.yml`, `desktop.yml`, `e2e.yml`
  - `build.gradle`, `app/core/build.gradle`
- `eslint.config.mjs`, `frontend/package.json`, `.gitignore`,
`.prettierignore`
  - `docker/frontend/Dockerfile`
  - 8 `.github/workflows/*.yml`, plus `.github/dependabot.yml`,
    `.github/config/.files.yaml`, `.github/labeler-config-srvaroa.yml`
  - `scripts/translations/**`
- Docs: `AGENTS.md`, `CLAUDE.md`, `ADDING_TOOLS.md`,
`DeveloperGuide.md`,
`WINDOWS_SIGNING.md`, `devGuide/HowToAddNewLanguage.md`,
`frontend/README.md`,
    `frontend/editor/DeveloperGuide.md`

Plus 3 renamed + edited: `editor/vite.config.ts` (env path +
node_modules
  walk-up), `editor/scripts/setup-env.mts` (renamed from `.ts` for
`import.meta.url`), `editor/scripts/build-provisioner.mjs` (resolve
src-tauri
  relative to script).

  ### Verification

  | Check | Result |
  |---|---|
  | `task frontend:typecheck:all` (6 variants) | exit 0 |
  | `task frontend:lint` (eslint + dpdm) | exit 0 |
  | `task frontend:format:check` | exit 0 |
  | `task frontend:test` | 657 tests pass, 50 files |
| `task frontend:build:{core,proprietary,saas,desktop,prototypes}` | all
green |
| `task desktop:build` | full Tauri pipeline →
`Stirling-PDF_2.11.0_x64_en-US.msi` |
  | `playwright test --list --project=stubbed` | 172 tests discovered |

`task desktop:build` exercises the heaviest path — Rust + WiX + MSI
bundle
against the moved `editor/src-tauri/`. If anything in the restructure
was
  wrong it wouldn't have built.

  ### Test plan

  - [ ] `frontend-validation.yml` green
  - [ ] `e2e-stubbed.yml` green
  - [ ] `tauri-build.yml` green on at least one platform
  - [ ] `check_toml.yml` runs on a translation-touching PR

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
2026-05-22 13:40:34 +01:00

355 lines
11 KiB
TypeScript

import { useState, useEffect, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { supabase } from "@app/auth/supabase";
import { useAuth } from "@app/auth/UseSession";
// Currency mapping
const getCurrencySymbol = (currency: string): string => {
const currencySymbols: { [key: string]: string } = {
gbp: "£",
usd: "$",
eur: "€",
cny: "¥",
inr: "₹",
brl: "R$",
idr: "Rp",
};
return currencySymbols[currency.toLowerCase()] || currency.toUpperCase();
};
export interface PlanFeature {
name: string;
included: boolean;
}
export interface PlanTier {
id: string;
name: string;
price: number;
currency: string;
period: string;
popular?: boolean;
features: PlanFeature[];
highlights: string[];
isContactOnly?: boolean;
}
export interface ApiPackage {
id: string;
name: string;
price: number;
currency: string;
credits: number;
description: string;
}
export interface PlansData {
plans: Map<string, PlanTier>;
apiPackages: ApiPackage[];
currentPlan: PlanTier;
nextBillingDate?: string;
activeSince?: string;
}
export const usePlans = (currency: string = "gbp") => {
const { t } = useTranslation();
const { isPro, refreshProStatus } = useAuth();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [currentPlanId, setCurrentPlanId] = useState<string>("free");
const [dynamicPrices, setDynamicPrices] = useState<
Map<string, { unit_amount: number; currency: string }>
>(new Map());
const fetchPricing = async () => {
try {
setLoading(true);
setError(null);
const lookupKeys = [
"plan:pro",
"api:xsmall",
"api:small",
"api:medium",
"api:large",
];
const { data, error } = await supabase.functions.invoke<{
prices: Record<string, { unit_amount: number; currency: string }>;
missing: string[];
}>("stripe-price-lookup", {
body: { lookup_keys: lookupKeys, currency },
});
if (error) throw error;
if (!data || !data.prices || !data.missing)
throw new Error("No pricing data returned");
console.log("Fetched pricing data:", data);
const priceMap = new Map<
string,
{ unit_amount: number; currency: string }
>();
// map your UI keys to lookup keys (if names differ)
const keyMap: Record<string, string> = {
pro: "plan:pro",
xsmall: "api:xsmall",
small: "api:small",
medium: "api:medium",
large: "api:large",
};
for (const [uiKey, lookupKey] of Object.entries(keyMap)) {
const p = data?.prices?.[lookupKey];
if (p) {
priceMap.set(uiKey, {
unit_amount: p.unit_amount ?? 0,
currency: p.currency,
});
}
}
if (data.missing.length) {
console.warn("Missing prices for", data.missing, "in", currency);
// Optionally re-request with a fallback currency (e.g., 'usd')
}
setDynamicPrices(priceMap);
} catch (err) {
console.error("Error fetching pricing:", err);
setError(
err instanceof Error ? err.message : "Failed to fetch pricing data",
);
// continue with static prices if needed
} finally {
setLoading(false);
}
};
// Memoize static plan and package data to prevent recreation on every render
const staticPlansData = useMemo(() => {
const plans: PlanTier[] = [
{
id: "free",
name: t("plan.free.name", "Free"),
price: 0,
currency: getCurrencySymbol(currency),
period: t("plan.period.month", "/month"),
highlights: [
t("plan.free.highlight1", "Limited Tool Usage Per week"),
t("plan.free.highlight2", "Access to all tools"),
t("plan.free.highlight3", "Community support"),
],
features: [
{
name: t("plan.feature.pdfTools", "Basic PDF Tools"),
included: true,
},
{
name: t("plan.feature.fileSize", "File Size Limit"),
included: false,
},
{
name: t("plan.feature.automation", "automate tool workflows"),
included: false,
},
{ name: t("plan.feature.api", "API Access"), included: false },
{
name: t("plan.feature.priority", "Priority Support"),
included: false,
},
{
name: t("plan.feature.customPricing", "Custom Pricing"),
included: false,
},
],
},
{
id: "pro",
name: t("plan.pro.name", "Pro"),
price: dynamicPrices.get("pro")
? dynamicPrices.get("pro")!.unit_amount / 100
: 8,
currency: dynamicPrices.get("pro")
? getCurrencySymbol(dynamicPrices.get("pro")!.currency)
: getCurrencySymbol(currency),
period: t("plan.period.month", "/month"),
popular: true,
highlights: [
t("plan.pro.highlight1", "Unlimited Tool Usage"),
t("plan.pro.highlight2", "Advanced PDF tools"),
t("plan.pro.highlight3", "No watermarks"),
],
features: [
{
name: t("plan.feature.pdfTools", "Basic PDF Tools"),
included: true,
},
{
name: t("plan.feature.fileSize", "File Size Limit"),
included: true,
},
{
name: t("plan.feature.automation", "automate tool workflows"),
included: true,
},
{ name: t("plan.feature.api", "Weekly API Credits"), included: true },
{
name: t("plan.feature.priority", "Priority Support"),
included: false,
},
{
name: t("plan.feature.customPricing", "Custom Pricing"),
included: false,
},
],
},
{
id: "enterprise",
name: t("plan.enterprise.name", "Enterprise"),
price: 0,
currency: getCurrencySymbol(currency),
period: "",
isContactOnly: true,
highlights: [
t("plan.enterprise.highlight1", "Custom pricing"),
t("plan.enterprise.highlight2", "Dedicated support"),
t("plan.enterprise.highlight3", "Latest features"),
],
features: [
{
name: t("plan.feature.pdfTools", "Basic PDF Tools"),
included: true,
},
{
name: t("plan.feature.fileSize", "File Size Limit"),
included: true,
},
{
name: t("plan.feature.automation", "automate tool workflows"),
included: true,
},
{ name: t("plan.feature.api", "Weekly API Credits"), included: true },
{
name: t("plan.feature.priority", "Priority Support"),
included: true,
},
{
name: t("plan.feature.customPricing", "Custom Pricing"),
included: true,
},
],
},
];
// Helper function to get price info
const getPriceInfo = (key: string, fallbackPrice: number) => {
const priceObj = dynamicPrices.get(key);
const price = priceObj ? priceObj.unit_amount / 100 : fallbackPrice;
const currencySymbol = priceObj
? getCurrencySymbol(priceObj.currency)
: getCurrencySymbol(currency);
return { price, currencySymbol };
};
const xsmallPrice = getPriceInfo("xsmall", 4);
const smallPrice = getPriceInfo("small", 15);
const mediumPrice = getPriceInfo("medium", 25);
const largePrice = getPriceInfo("large", 90);
// Calculate dynamic discounts based on per-credit cost (using xsmall as baseline)
const xsmallPerCredit = xsmallPrice.price / 100;
const smallPerCredit = smallPrice.price / 500;
const mediumPerCredit = mediumPrice.price / 1000;
const largePerCredit = largePrice.price / 5000;
const smallDiscount = Math.round(
(1 - smallPerCredit / xsmallPerCredit) * 100,
);
const mediumDiscount = Math.round(
(1 - mediumPerCredit / xsmallPerCredit) * 100,
);
const largeDiscount = Math.round(
(1 - largePerCredit / xsmallPerCredit) * 100,
);
const apiPackages: ApiPackage[] = [
{
id: "xsmall",
name: t("plan.api.xsmall", "100 Credits"),
price: xsmallPrice.price,
currency: xsmallPrice.currencySymbol,
credits: 100,
description: `${xsmallPrice.currencySymbol}${(xsmallPrice.price / 100).toFixed(3)} per credit`,
},
{
id: "small",
name: t("plan.api.small", "500 Credits"),
price: smallPrice.price,
currency: smallPrice.currencySymbol,
credits: 500,
description: `${smallPrice.currencySymbol}${(smallPrice.price / 500).toFixed(3)} per credit${smallDiscount > 0 ? ` • ${smallDiscount}% discount` : ""}`,
},
{
id: "medium",
name: t("plan.api.medium", "1,000 Credits"),
price: mediumPrice.price,
currency: mediumPrice.currencySymbol,
credits: 1000,
description: `${mediumPrice.currencySymbol}${(mediumPrice.price / 1000).toFixed(3)} per credit${mediumDiscount > 0 ? ` • ${mediumDiscount}% discount` : ""}`,
},
{
id: "large",
name: t("plan.api.large", "5,000 Credits"),
price: largePrice.price,
currency: largePrice.currencySymbol,
credits: 5000,
description: `${largePrice.currencySymbol}${(largePrice.price / 5000).toFixed(3)} per credit${largeDiscount > 0 ? ` • ${largeDiscount}% discount` : ""}`,
},
];
const plansMap = new Map(plans.map((plan) => [plan.id, plan]));
return { plans: plansMap, apiPackages };
}, [t, dynamicPrices]);
// Create final data object with current plan info
const data = useMemo<PlansData | null>(() => {
if (!staticPlansData) return null;
const currentPlan = staticPlansData.plans.get(currentPlanId);
if (!currentPlan) return null;
return {
plans: staticPlansData.plans,
apiPackages: staticPlansData.apiPackages,
currentPlan,
nextBillingDate: "Feb 15, 2025",
activeSince: "January 2025",
};
}, [staticPlansData, currentPlanId]);
// Update currentPlanId when isPro changes
useEffect(() => {
if (isPro !== null) {
setCurrentPlanId(isPro ? "pro" : "free");
}
}, [isPro]);
// Initial load - fetch pricing data
useEffect(() => {
fetchPricing();
}, [currency]); // Re-fetch when currency changes
const updateCurrentPlan = (newPlanId: string) => {
setCurrentPlanId(newPlanId);
};
return {
data,
plans: data ? Array.from(data.plans.values()) : [], // Convert Map to array for compatibility with proprietary code
loading,
error,
refetch: refreshProStatus, // Refetch pro status from auth context
updateCurrentPlan, // Add local plan update function
};
};