File size: 7,935 Bytes
ec4551b | 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 'use client';
import 'reflect-metadata';
import { FC, MutableRefObject, useEffect, useMemo } from 'react';
import { FormProvider, useForm, useWatch } from 'react-hook-form';
import { classValidatorResolver } from '@hookform/resolvers/class-validator';
import { Providers } from '@gitroom/frontend/components/new-launch/providers/show.all.providers';
import { getProviderSettingsMeta } from '@gitroom/frontend/components/new-launch/providers/high.order.provider';
import {
IntegrationContext,
type IntegrationContextType,
} from '@gitroom/frontend/components/launches/helpers/use.integration';
import { newDayjs } from '@gitroom/frontend/components/layout/set.timezone';
type MockIntegration = IntegrationContextType['integration'];
export type ProviderPreviewValidation = {
isValid: boolean;
value: Record<string, unknown>;
errors: string[];
/** react-hook-form trigger() result. False = at least one DTO field failed. */
formValid: boolean;
/** Non-null when the provider's checkValidity() returned a string. */
checkValidityError: string | null;
};
export type ProviderPreviewHandle = {
getValues: () => Record<string, unknown>;
validate: () => Promise<ProviderPreviewValidation>;
/**
* Resolves the provider's `maximumCharacters` against the seeded
* integration.additionalSettings. Returns null when the provider doesn't
* declare a limit (caller should treat as unbounded / fall back).
*/
getMaximumCharacters: () => number | null;
};
export type ProviderPreviewProps = {
/** Provider identifier (e.g. "tiktok", "instagram", "youtube"). */
provider: string;
/** Initial settings value (shape matches the provider's DTO). */
value?: Record<string, unknown>;
/**
* Called on every form change with the current settings value — for the
* mobile WebView bridge this is what you postMessage back.
*/
onChange?: (value: Record<string, unknown>) => void;
/** Validator error messages from a previous failed save, rendered above the form. */
errors?: string[];
/**
* Stub integration to feed the SettingsComponent via IntegrationContext.
* Some providers (e.g. TikTok title) branch on `integration.additionalSettings`
* or `value[0].image` — pass what you have, leave the rest to defaults.
*/
integration?: Partial<MockIntegration>;
/**
* Per-post media (outer array = thread entries, inner = media items).
* Forwarded to the provider's `checkValidity` during validate().
*/
posts?: Array<Array<{ path: string; thumbnail?: string }>>;
/**
* Imperative handle populated on mount. The parent calls
* `controlRef.current?.validate()` / `.getValues()` to pull state on demand.
*/
controlRef?: MutableRefObject<ProviderPreviewHandle | null>;
};
const DEFAULT_INTEGRATION: MockIntegration = {
id: 'preview',
name: 'Preview',
identifier: '',
picture: '',
display: '',
type: 'social',
editor: 'normal' as const,
disabled: false,
inBetweenSteps: false,
additionalSettings: '[]',
changeProfilePicture: false,
changeNickName: false,
time: [] as { time: number }[],
};
/** Emits onChange whenever the form changes. Mounted inside FormProvider. */
const FormChangeEmitter: FC<{
onChange?: (value: Record<string, unknown>) => void;
}> = ({ onChange }) => {
const values = useWatch();
useEffect(() => {
if (onChange) onChange(values ?? {});
}, [values, onChange]);
return null;
};
const flattenFormErrors = (errs: unknown): string[] => {
const out: string[] = [];
const walk = (node: unknown) => {
if (!node || typeof node !== 'object') return;
const n = node as Record<string, unknown>;
if (typeof n.message === 'string') out.push(n.message);
if (n.types && typeof n.types === 'object') {
for (const t of Object.values(n.types as Record<string, unknown>)) {
if (typeof t === 'string') out.push(t);
}
}
for (const [key, child] of Object.entries(n)) {
if (['message', 'type', 'types', 'ref', 'root'].includes(key)) continue;
walk(child);
}
};
walk(errs);
return out;
};
export const ProviderPreviewComponent: FC<ProviderPreviewProps> = ({
provider,
value,
onChange,
errors,
integration,
posts,
controlRef,
}) => {
const meta = useMemo(() => {
const entry = Providers.find((p) => p.identifier === provider);
if (!entry) return null;
return getProviderSettingsMeta(entry.component);
}, [provider]);
// When `value` is absent or `{}`, don't feed it to react-hook-form at all —
// passing an empty object as `values` wipes out DTO-level defaults that the
// SettingsComponent relies on (e.g. tiktok privacy = PUBLIC).
const hasSeededValue =
!!value && typeof value === 'object' && Object.keys(value).length > 0;
const form = useForm({
resolver: meta?.dto ? classValidatorResolver(meta.dto) : undefined,
defaultValues: hasSeededValue ? value : undefined,
values: hasSeededValue ? value : undefined,
mode: 'all',
criteriaMode: 'all',
reValidateMode: 'onChange',
});
useEffect(() => {
if (!controlRef) return;
const resolveAdditionalSettings = (): unknown[] => {
const additional = (integration?.additionalSettings as
| string
| unknown[]
| undefined) ?? '[]';
if (Array.isArray(additional)) return additional;
try {
const parsed = JSON.parse(additional || '[]');
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
};
controlRef.current = {
getValues: () => form.getValues() as Record<string, unknown>,
getMaximumCharacters: () => {
const max = meta?.maximumCharacters;
if (typeof max === 'number') return max;
if (typeof max === 'function') {
try {
return max(resolveAdditionalSettings());
} catch {
return null;
}
}
return null;
},
validate: async () => {
const formValid = await form.trigger(undefined, { shouldFocus: false });
const errs = flattenFormErrors(form.formState.errors);
// Media validation (`checkValidity`) now runs server-side at create
// time (`/posts/valid` + `/posts`), so the preview only does the
// local DTO/form validation here.
return {
isValid: formValid,
value: form.getValues() as Record<string, unknown>,
errors: errs,
formValid,
checkValidityError: null,
};
},
};
return () => {
if (controlRef.current) controlRef.current = null;
};
}, [controlRef, form, meta, integration, posts]);
const contextValue = useMemo<IntegrationContextType>(
() => ({
date: newDayjs(),
integration: {
...(DEFAULT_INTEGRATION as MockIntegration),
identifier: provider,
...integration,
} as MockIntegration,
allIntegrations: [],
value: [],
}),
[provider, integration],
);
if (!meta) {
return <div>Provider "{provider}" not found</div>;
}
const { SettingsComponent } = meta;
if (!SettingsComponent) {
return (
<div className="p-4 text-sm">
This provider has no configurable settings.
</div>
);
}
return (
<IntegrationContext.Provider value={contextValue}>
<FormProvider {...form}>
<div className="flex flex-col text-white p-[10px]">
{errors && errors.length > 0 && (
<div className="rounded-md border border-red-500/40 bg-red-500/10 p-3 text-sm text-red-300">
<ul className="list-disc ps-5">
{errors.map((e, i) => (
<li key={i}>{e}</li>
))}
</ul>
</div>
)}
<FormChangeEmitter onChange={onChange} />
<SettingsComponent />
</div>
</FormProvider>
</IntegrationContext.Provider>
);
};
|