Repository: directus/directus
File: app/src/interfaces/translations/translations.vue (Vue)

Code before:
		const sortField = relationInfo.value.relatedCollection.meta?.sort_field;

		fields.add(pkField);

		loading.value = true;

		try {
			languages.value = await fetchAll<Record<string, any>[]>(getEndpoint(collection), {
				params: {
					fields: Array.from(fields),
					sort: sortField ?? props.languageField ?? pkField,
				},
			});

			if (!firstLang.value) {
				const userLocale = userLanguage.value ? locale.value : defaultLanguage.value;
				const lang = languages.value.find((lang) => lang[pkField] === userLocale) || languages.value[0];
				firstLang.value = lang?.[pkField];
			}

			if (!secondLang.value) {
				const defaultLocale = userLanguage.value ? defaultLanguage.value : null;
				const alternativeLang = languages.value.find((lang) => lang[pkField] !== firstLang.value);
				const defaultLang = languages.value.find((lang) => lang[pkField] === defaultLocale);

				secondLang.value = (alternativeLang || defaultLang || languages.value[0])?.[pkField];
			}
		} catch (error) {
			unexpectedError(error);
		} finally {
			loading.value = false;
		}
	}
}

const firstItemPrimaryKey = computed(
	() => relationInfo.value && firstItemInitial.value?.[relationInfo.value.junctionPrimaryKeyField.field],
);

const secondItemPrimaryKey = computed(
	() => relationInfo.value && secondItemInitial.value?.[relationInfo.value.junctionPrimaryKeyField.field],
);

const firstItemNew = computed(() => !!(relationInfo.value && firstItemPrimaryKey.value === undefined));

const secondItemNew = computed(() => !!(relationInfo.value && secondItemPrimaryKey.value === undefined));

const {
	itemPermissions: { saveAllowed: firstSaveAllowed, fields: firstFields },
} = usePermissions(
	computed(() => relationInfo.value?.junctionCollection.collection ?? null),

Review comment (refactor):
```
	if (!secondLang.value) {
		const defaultLocale = userLanguage.value ? defaultLanguage.value : null;
		let lang;

		lang = languages.value.find((lang) => lang[pkField] === defaultLocale) || languages.value[0];

		if (!lang || lang[pkField] === firstLang.value) {
			lang = languages.value.find((lang) => lang[pkField] !== firstLang.value) || languages.value[1];
		}

		secondLang.value = lang?.[pkField];
	}
```

I think we can maybe simplify this by just assigning lang to a let, and then reassigning it if it matches `firstLang`

This way we don't have to perform unnecessary `Array.find` calls unless they're needed :)

Code after:

		loading.value = true;

		try {
			languages.value = await fetchAll<Record<string, any>[]>(getEndpoint(collection), {
				params: {
					fields: Array.from(fields),
					sort: sortField ?? props.languageField ?? pkField,
				},
			});

			if (!firstLang.value) {
				const userLocale = userLanguage.value ? locale.value : defaultLanguage.value;
				const lang = languages.value.find((lang) => lang[pkField] === userLocale) || languages.value[0];
				firstLang.value = lang?.[pkField];
			}

			if (!secondLang.value) {
				const defaultLocale = userLanguage.value ? defaultLanguage.value : null;
				let lang = languages.value.find((lang) => lang[pkField] === defaultLocale) || languages.value[0];

				if (!lang || lang[pkField] === firstLang.value) {
					lang = languages.value.find((lang) => lang[pkField] !== firstLang.value) || languages.value[1];
				}

				secondLang.value = lang?.[pkField];
			}
		} catch (error) {
			unexpectedError(error);
		} finally {
			loading.value = false;
		}
	}
}

const firstItemPrimaryKey = computed(
	() => relationInfo.value && firstItemInitial.value?.[relationInfo.value.junctionPrimaryKeyField.field],
);

const secondItemPrimaryKey = computed(
	() => relationInfo.value && secondItemInitial.value?.[relationInfo.value.junctionPrimaryKeyField.field],
);

const firstItemNew = computed(() => !!(relationInfo.value && firstItemPrimaryKey.value === undefined));

const secondItemNew = computed(() => !!(relationInfo.value && secondItemPrimaryKey.value === undefined));

const {
	itemPermissions: { saveAllowed: firstSaveAllowed, fields: firstFields },
} = usePermissions(
	computed(() => relationInfo.value?.junctionCollection.collection ?? null),