diff --git a/src/core/hyperframes.ts b/src/core/hyperframes.ts new file mode 100644 index 0000000000..f00e23bf03 --- /dev/null +++ b/src/core/hyperframes.ts @@ -0,0 +1,25 @@ +import { HyperFrame } from "./types"; + +const frames: Record = {}; + +export function addHyperFrame(label: string, frame: HyperFrame) { + frames[label] = frame; +} + +export function getHyperFrame(frame: string, text: string, button: number) { + const currentFrame = frames[frame]; + const nextFrameIdOrFunction = currentFrame[button as keyof HyperFrame]; + + let nextFrameId: string; + if (typeof nextFrameIdOrFunction === 'function') { + nextFrameId = nextFrameIdOrFunction(text); + } else { + nextFrameId = nextFrameIdOrFunction as string; + } + + if (!frames[nextFrameId]) { + throw new Error(`Frame not found: ${nextFrameId}`); + } + + return frames[nextFrameId].frame; +} diff --git a/src/core/types.ts b/src/core/types.ts index 2cde916435..d3dc011d82 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -132,6 +132,19 @@ export type FrameMetadataType = { */ export type FrameMetadataResponse = Record; +/** + * A named frame with links to other named frames. + * + * Note: exported as public Type + */ +export type HyperFrame = { + frame: string; + 1: string | ((text: string) => string) | (() => string); + 2?: string | ((text: string) => string) | (() => string); + 3?: string | ((text: string) => string) | (() => string); + 4?: string | ((text: string) => string) | (() => string); +}; + /** * Ethereum Attestation Service (EAS) Attester Address * The Ethereum address of the attester who created the attestation.