Files
community-rule/app/(app)/create/utils/resolvePreviewSurfaceFallback.ts
T
adilalloandCursor b12de611f7 Read the review card fill from localStorage before picking a new one.
The review screen’s effect ran before draft hydration, so reload always generated a fresh color and overwrote the stored one.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 14:05:30 -06:00

65 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { CreateFlowState } from "../types";
import {
isPreviewSurfaceColor,
randomPreviewSurfaceColor,
} from "../../../../lib/create/previewSurfaceColor";
import {
readAnonymousCreateFlowState,
writeAnonymousCreateFlowState,
} from "./anonymousDraftStorage";
function validPreviewSurfaceColor(
value: string | undefined,
): string | undefined {
return typeof value === "string" && isPreviewSurfaceColor(value)
? value
: undefined;
}
/** Existing draft fill from React state or localStorage, if it is a stored hsl. */
export function readDraftPreviewSurfaceColor(
storedFallback?: string,
): string | undefined {
return (
validPreviewSurfaceColor(storedFallback) ??
validPreviewSurfaceColor(
readAnonymousCreateFlowState().previewSurfaceColor,
)
);
}
/**
* Keep a fallback fill on the anonymous draft immediately. The provider’s
* first mirrored write is skipped, so waiting on React state loses the color
* across reload. `state` is merged so a write before hydrate does not drop
* other draft fields already in memory.
*/
export function persistDraftPreviewSurfaceColor(
color: string,
state: CreateFlowState = {},
): void {
const next = validPreviewSurfaceColor(color);
if (!next) return;
const current = readAnonymousCreateFlowState();
const stored = validPreviewSurfaceColor(current.previewSurfaceColor);
writeAnonymousCreateFlowState({
...current,
...state,
previewSurfaceColor: stored ?? next,
});
}
/**
* Reuse the draft fill when one exists; otherwise pick one and write it now.
* Reads localStorage itself so a child effect can run before context hydrates.
*/
export function resolvePreviewSurfaceFallback(
storedFallback?: string,
): string {
const existing = readDraftPreviewSurfaceColor(storedFallback);
if (existing) return existing;
const color = randomPreviewSurfaceColor();
persistDraftPreviewSurfaceColor(color);
return color;
}