videogen/apps/mobile/components/avatars/CreateAvatarForm.tsx
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

153 lines
4.3 KiB
TypeScript

/**
* CreateAvatarForm — turns a creator's selfie into a HeyGen talking-photo
* avatar. Uploads the photo (SelfieUploader → R2 `image_key`), collects a
* display name and an optional default narrator voice, then calls POST /avatars
* (createAvatarFromPhoto). On success it hands the created avatar up to the
* parent (which prepends it to the list) and resets the form.
*
* Owns its own submit lifecycle + error; the selfie upload lifecycle lives in
* SelfieUploader.
*/
import { useState } from "react";
import {
ActivityIndicator,
Pressable,
StyleSheet,
Text,
TextInput,
View,
} from "react-native";
import { describeError } from "@/components/me/errors";
import { colors } from "@/components/me/theme";
import { createAvatarFromPhoto } from "@/lib/api";
import type { CreateAvatarResult } from "@/lib/types";
import { AvatarVoicePicker } from "./AvatarVoicePicker";
import { SelfieUploader } from "./SelfieUploader";
interface CreateAvatarFormProps {
token: string | undefined;
/** Called with the created avatar preset after a successful POST /avatars. */
onCreated: (avatar: CreateAvatarResult) => void;
}
export function CreateAvatarForm({ token, onCreated }: CreateAvatarFormProps) {
const [imageKey, setImageKey] = useState<string | null>(null);
const [displayName, setDisplayName] = useState("");
const [voiceName, setVoiceName] = useState<string | null>(null);
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const canSubmit = !!imageKey && !submitting;
const onUploaded = (key: string) => {
setImageKey(key);
setError(null);
};
const reset = () => {
setImageKey(null);
setDisplayName("");
setVoiceName(null);
};
const onSubmit = async () => {
if (!imageKey) return;
setSubmitting(true);
setError(null);
try {
const created = await createAvatarFromPhoto(
{
image_key: imageKey,
...(displayName.trim() ? { display_name: displayName.trim() } : {}),
...(voiceName ? { voice_name: voiceName } : {}),
},
{ token },
);
onCreated(created);
reset();
} catch (err) {
setError(describeError(err, "Failed to create the avatar."));
} finally {
setSubmitting(false);
}
};
return (
<View style={styles.container}>
<SelfieUploader
token={token}
hasImage={!!imageKey}
onUploaded={onUploaded}
disabled={submitting}
/>
<Text style={styles.label}>Name</Text>
<TextInput
style={styles.input}
value={displayName}
onChangeText={setDisplayName}
placeholder="e.g. My spokesperson"
placeholderTextColor={colors.muted}
editable={!submitting}
maxLength={200}
/>
<Text style={styles.label}>Default voice (optional)</Text>
<AvatarVoicePicker
token={token}
value={voiceName}
onSelect={setVoiceName}
disabled={submitting}
/>
{error ? <Text style={styles.error}>{error}</Text> : null}
<Pressable
style={[styles.button, !canSubmit && styles.buttonDisabled]}
onPress={() => void onSubmit()}
disabled={!canSubmit}
accessibilityRole="button"
accessibilityState={{ disabled: !canSubmit }}
>
{submitting ? (
<ActivityIndicator color={colors.textPrimary} />
) : (
<Text style={styles.buttonText}>Create avatar</Text>
)}
</Pressable>
{!imageKey ? (
<Text style={styles.meta}>Upload a photo above to continue.</Text>
) : null}
</View>
);
}
const styles = StyleSheet.create({
container: { gap: 10 },
label: { color: colors.textSecondary, fontSize: 13, fontWeight: "600", marginTop: 4 },
input: {
backgroundColor: colors.card,
borderColor: colors.border,
borderWidth: 1,
borderRadius: 10,
color: colors.textPrimary,
fontSize: 16,
paddingHorizontal: 14,
paddingVertical: 12,
},
error: { color: colors.error, fontSize: 13 },
button: {
backgroundColor: colors.primary,
borderRadius: 10,
paddingVertical: 14,
alignItems: "center",
marginTop: 4,
},
buttonDisabled: { opacity: 0.5 },
buttonText: { color: colors.textPrimary, fontWeight: "700", fontSize: 15 },
meta: { color: colors.muted, fontSize: 12, textAlign: "center" },
});