Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

翻訳コンポーネントをホバーじゃなくクリックに #117

Merged
merged 2 commits into from
Jul 28, 2024

Conversation

ttizze
Copy link
Owner

@ttizze ttizze commented Jul 28, 2024

Closes #113

Summary by CodeRabbit

## リリースノート

### New Feature
- `ToggleButton`コンポーネントを追加し、翻訳オプションの表示・非表示を切り替える機能を実装しました。
- `getBestTranslation`関数を追加し、最適な翻訳を選択するロジックを実装しました。

### Refactor
- 翻訳コンポーネントのホバーイベントをクリックイベントに変更しました。
- `sanitizeAndParseText`関数のインポートパスを更新しました。
- `getBestTranslation`関数とテキストのサニタイズ処理を外部ファイルに移動しました。

### Style
- エラーメッセージのテキストにCSSクラス`text-sm`を追加し、フォントサイズを小さくしました。

### Chore
- `.github/issue-type-config.json`ファイルを使用してラベルを決定するスクリプトを削除しました。

Copy link

This PR is linked to issue #113

Copy link

github-actions bot commented Jul 28, 2024

Image description CodeRabbit

### ウォークスルー

この変更は、翻訳コンポーネントのユーザーインターフェースを改善し、クリックイベントに基づく表示ロジックを導入することを目的としています。また、不要なアイコンやテキストの削除、エラーメッセージのスタイル調整、関数のリファクタリングも行われています。

### 変更点

| ファイル | 要約 |
| --- | --- |
| .github/scripts/determine-issue-type.sh | `.github/issue-type-config.json`を使用してIssueタイトルに基づいてラベルを決定するスクリプトを削除。 |
| web/app/routes/reader.$encodedUrl/components/AddAndVoteTranslations.tsx, web/app/routes/reader.$encodedUrl/components/Translation.tsx | 翻訳コンポーネントのホバーからクリックへの変更、アイコン追加、表示状態切り替えロジック実装。 |
| web/app/routes/reader.$encodedUrl/components/AddTranslationForm.tsx | エラーメッセージのテキストにCSSクラス`text-sm`を追加。 |
| web/app/routes/reader.$encodedUrl/components/TranslationItem.tsx | `sanitizeAndParseText`関数のインポートパスを変更。 |
| web/app/routes/reader.$encodedUrl/functions/get-best-translation.client.ts | `getBestTranslation`関数を追加し、最適な翻訳を選択するロジックを実装。 |

Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

Comment on lines +3 to +18
export function getBestTranslation(
translationsWithVotes: TranslationWithVote[],
): TranslationWithVote {
const upvotedTranslations = translationsWithVotes.filter(
(t) => t.userVote?.isUpvote,
);
if (upvotedTranslations.length > 0) {
return upvotedTranslations.reduce((prev, current) => {
const currentUpdatedAt = current.userVote?.updatedAt ?? new Date(0);
const prevUpdatedAt = prev.userVote?.updatedAt ?? new Date(0);
return currentUpdatedAt > prevUpdatedAt ? current : prev;
});
}
return translationsWithVotes.reduce((prev, current) =>
prev.point > current.point ? prev : current,
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

translationsWithVotesが空の場合に対応するためのエラーハンドリングが不足しています。空配列が渡された場合、関数は未定義を返す可能性があります。

5c5
< ): TranslationWithVote {


<!-- This is an auto-generated comment by OSS CodeRabbit -->

@@ -33,7 +37,7 @@ export function Translation({
userId,
sourceTextId,
}: TranslationProps) {
const [isHovered, setIsHovered] = useState(false);
const [isExpanded, setIsExpanded] = useState(false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

isHoveredステートをisExpandedステートに置き換える変更は適切ですが、変数名が一貫していないため、setIsHoveredsetIsExpandedに変更する必要があります。

-	const [isHovered, setIsHovered] = useState(false);
+	const [isExpanded, setIsExpanded] = useState(false);

Comment on lines +59 to +76
<div className="group relative">
<div className="w-full notranslate mt-2 pt-2 border-t border-gray-200">
{sanitizedAndParsedText}
<ToggleButton
isExpanded={isExpanded}
onClick={() => setIsExpanded(!isExpanded)}
/>
</div>
<div className="absolute top-0 left-0 right-0 z-10 opacity-0 invisible border bg-white dark:bg-gray-900 rounded-xl shadow-xl dark:shadow-white/10 group-hover:opacity-100 group-hover:visible transition-all duration-500 ease-in-out">
{isHovered && (
{isExpanded && (
<div className="absolute top-0 left-0 right-0 z-10 border bg-white dark:bg-gray-900 rounded-xl shadow-xl dark:shadow-white/10 transition-all duration-500 ease-in-out">
<AddAndVoteTranslations
bestTranslationWithVote={bestTranslationWithVote}
alternativeTranslationsWithVotes={alternativeTranslationsWithVotes}
userId={userId}
sourceTextId={sourceTextId}
/>
)}
</div>
</div>
)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

alternativeTranslationsWithVotesが定義されていないため、エラーが発生します。以下のように修正してください。

				<div className="absolute top-0 left-0 right-0 z-10  border bg-white dark:bg-gray-900 rounded-xl shadow-xl dark:shadow-white/10  transition-all duration-500 ease-in-out">
 					<AddAndVoteTranslations
 						bestTranslationWithVote={bestTranslationWithVote}
-						alternativeTranslationsWithVotes={alternativeTranslationsWithVotes}
+						alternativeTranslationsWithVotes={translationsWithVotes.filter(t => t !== bestTranslationWithVote)}
 						userId={userId}
 						sourceTextId={sourceTextId}
 					/>
				</div>

Comment on lines +45 to 46
Alternative
</p>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Alternativeのテキストが変更されましたが、文脈から判断すると、元のAlternative Translationsの方が意味が明確です。

- 					Alternative
+ 					Alternative Translations

@ttizze ttizze merged commit 272a1ad into main Jul 28, 2024
5 checks passed
@ttizze ttizze deleted the ttizze/enhance-issue-113 branch July 28, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
1 participant