File size: 2,646 Bytes
482a617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbcd2e8
 
 
 
 
 
 
 
 
 
482a617
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
  getTemplates,
  getTemplateAvailabilitySignal,
  listCustomTemplates,
  type CustomTemplateItem,
  type TemplateMeta,
} from "./client";

export type AvailabilityForBlogUrlForm = {
  hasCraftedTemplatesEligible: boolean;
  customTemplates: CustomTemplateItem[];
};

let builtinTemplatesPrefetch: Promise<TemplateMeta[]> | null = null;

function builtinTemplatesDeduped(): Promise<TemplateMeta[]> {
  if (!builtinTemplatesPrefetch) {
    builtinTemplatesPrefetch = getTemplates()
      .then((r) => r.data ?? [])
      .catch((err) => {
        builtinTemplatesPrefetch = null;
        throw err;
      });
  }
  return builtinTemplatesPrefetch;
}

let availabilityPrefetch: Promise<AvailabilityForBlogUrlForm> | null = null;

async function availabilityBundle(): Promise<AvailabilityForBlogUrlForm> {
  try {
    const r = await getTemplateAvailabilitySignal();
    const hasCrafted = Boolean(r.data?.has_crafted_templates);
    const hasCustom = Boolean(r.data?.has_custom_templates);
    let customTemplates: CustomTemplateItem[] = [];
    if (hasCustom) {
      try {
        customTemplates = (await listCustomTemplates()).data;
      } catch {
        customTemplates = [];
      }
    }
    return { hasCraftedTemplatesEligible: hasCrafted, customTemplates };
  } catch {
    try {
      const customTemplates = (await listCustomTemplates()).data ?? [];
      return { hasCraftedTemplatesEligible: true, customTemplates };
    } catch {
      return { hasCraftedTemplatesEligible: true, customTemplates: [] };
    }
  }
}

function availabilityDeduped(): Promise<AvailabilityForBlogUrlForm> {
  if (!availabilityPrefetch) {
    availabilityPrefetch = availabilityBundle().catch((err) => {
      availabilityPrefetch = null;
      throw err;
    });
  }
  return availabilityPrefetch;
}

/** Starts built-in templates + availability/custom fetch in the background (idempotent). */
export function primeBlogUrlFormStep2Prefetch(): void {
  void builtinTemplatesDeduped();
  void availabilityDeduped();
}

/**
 * Drops the cached availability/custom-template bundle so the next fetch hits the
 * server fresh. Call after creating or deleting a custom template — otherwise the
 * project-creation picker keeps serving the stale module-cached list until a full
 * page refresh.
 */
export function invalidateBlogUrlFormAvailabilityCache(): void {
  availabilityPrefetch = null;
}

export function fetchBlogUrlFormBuiltinTemplatesDeduped(): Promise<TemplateMeta[]> {
  return builtinTemplatesDeduped();
}

export function fetchBlogUrlFormAvailabilityDeduped(): Promise<AvailabilityForBlogUrlForm> {
  return availabilityDeduped();
}