Repository: strapi/strapi
File: packages/plugins/i18n/admin/src/components/CMHeaderActions.tsx (TypeScript)

Code before:
          const fetchSourceLocaleData = async () => {
            try {
              // We fetch the data from the selected document
              const response = await getDocument({
                collectionType,
                model,
                documentId,
                params: { locale: sourceLocale },
              });

              if (response && response.data) {
                // Extract only non-translatable fields from the source locale document
                const dataToSet = nonTranslatedFields.reduce(
                  (acc: Record<string, unknown>, field: string) => {
                    acc[field] = response.data[field];
                    return acc;
                  },
                  {}
                );

                if (Object.keys(dataToSet).length > 0) {
                  setValues(dataToSet);
                }
              }
            } catch (error) {
              console.warn(
                'Failed to fetch source locale data for non-translatable fields:',
                error
              );
            }
          };

          fetchSourceLocaleData();
        }
      }
    }
  }, [
    document,
    schema,
    setValues,
    currentDesiredLocale,
    locales,
    documentId,
    getDocument,
    collectionType,
    model,
    meta,
  ]);

  React.useEffect(() => {
    if (!Array.isArray(locales) || !hasI18n) {

Review comment (bug):
I'm guessing you didn't use a NotificationError because if it fails, it's not too bad of a problem if the values aren't set by default right?

Code after:
  }, [schema?.attributes]);

  const sourceLocaleData = React.useMemo(() => {
    if (!Array.isArray(locales) || !meta?.availableLocales) return null;

    const defaultLocale = locales.find((locale: Locale) => locale.isDefault);
    const existingLocales = meta.availableLocales.map((loc) => loc.locale);

    const sourceLocaleCode =
      defaultLocale &&
      existingLocales.includes(defaultLocale.code) &&
      defaultLocale.code !== currentDesiredLocale
        ? defaultLocale.code
        : existingLocales.find((locale) => locale !== currentDesiredLocale);

    if (!sourceLocaleCode) return null;

    // Find the document data from availableLocales (now includes non-translatable fields)
    const sourceLocaleDoc = meta.availableLocales.find((loc) => loc.locale === sourceLocaleCode);

    return sourceLocaleDoc
      ? { locale: sourceLocaleCode, data: sourceLocaleDoc as Record<string, unknown> }
      : null;
  }, [locales, meta?.availableLocales, currentDesiredLocale]);

  /**
   * Prefilling form with non-translatable fields from already existing locale
   */
  React.useEffect(() => {
    // Only run when creating a new locale (no document ID yet) and when we have non-translatable fields
    if (!document?.id && nonTranslatedFields.length > 0 && sourceLocaleData?.data) {
      const dataToSet = nonTranslatedFields.reduce(
        (acc: Record<string, unknown>, field: string) => {
          acc[field] = sourceLocaleData.data[field];
          return acc;
        },
        {}
      );

      if (Object.keys(dataToSet).length > 0) {
        setValues(dataToSet);
      }
    }
  }, [document?.id, nonTranslatedFields, sourceLocaleData?.data, setValues]);

  React.useEffect(() => {
    if (!Array.isArray(locales) || !hasI18n) {
      return;
    }
    /**
     * Handle the case where the current locale query param doesn't exist