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

[Improve code]: components/Client/common/* 手動formatter・Linter対応 #94

Merged
merged 9 commits into from
Apr 13, 2024

Conversation

ZEKE320
Copy link
Collaborator

@ZEKE320 ZEKE320 commented Apr 13, 2024

Resolves #59
以下の対応を行いました。

  • 1064677 src/components/Client/common/*にPrettierを適用
  • 5982e58 セレクトボックス共通実装にて、codeMapにジェネリック型を導入

@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 13, 2024

@nkte8
実装について意見をお伺いしたいです

現状では投稿の言語を設定する際に、codeに設定されている"ja" "en"などの文字列を用いていると思います

const langList = [
{
label: "日本語",
code: "ja",
},
{
label: "English",
code: "en",
},
{
label: "中文",
code: "zh",
},
{
label: "한국어",
code: "ko",
},
{
label: "Polski",
code: "pl",
},
{
label: "Portuguese",
code: "pt",
},
{
label: "Deutsch",
code: "de",
},
{
label: "Français",
code: "fr",
},
{
label: "हिन्दी",
code: "hi",
},
]

また投稿時に用いられるPostButton.tsxでは、上記codeに相当する変数languagestring型であると定義されています

一方で言語選択で用いられている共通実装では、setLanguage()に相当するsetCode()nullがセットできるような仕様となっています

let value: any = null
const item = codeMap.find((opt) => opt.label === event.target.value)
if (typeof item !== "undefined") {
value = item.code
}
setCode(value)

今回の対応では、この部分の実装を

  • nullが許容できない場合は、デフォルト値(言語設定であれば"ja")をセットする
  • codeMap: codeMap[]の1番目のcodeをセットする

といったものに変更する必要がありそうです
どちらの方法で対応しましょうか?

(追記 2024/04/13)
当面の対応として、codeMap[0].codeをデフォルト値として用いる方針で決定しました

Copy link

cloudflare-workers-and-pages bot commented Apr 13, 2024

Deploying skyshare with  Cloudflare Pages  Cloudflare Pages

Latest commit: d44cc71
Status: ✅  Deploy successful!
Preview URL: https://13823ba5.skyshare.pages.dev
Branch Preview URL: https://preview-issue-59.skyshare.pages.dev

View logs

@nkte8
Copy link
Owner

nkte8 commented Apr 13, 2024

@ZEKE320 対応ありがとうございます!
本リポジトリを閲覧する開発者に向けて、内部Discordでの結論をまとめておきます

nullをなぜ許容できる書き方をしているのか
langパラメータは任意なので、空にしてもいいのだけれど、現在のSelectboxではその指定ができない状態になってしまっている
ねこののWillとして思っているのが

  1. i18n対応により、初回で開いたlangのページをlocalstorageに保存
  2. localstorageを参照してデフォルト値をセット

要はlanguage: nullは許容したい、という話が展開されました。
このディスカッションにより以下の方針を取っていただくということで、(対処はi18n対応が進むことで変更され、本変更が一時的なものになる/よりよい形に修正される可能性がある前提で)ねこのが担当者にOKを出しました

未選択時はcodeMapの0番目を選択するような実装で、今回は解決できそうです
ラベルの0番目がnull、言語の0番目が"ja"なので、これでも問題は無いのかなと
...(以下コード)...

選択としては

codeMap: codeMap[]の1番目のcodeをセットする

を踏襲する決定になります。


蛇足:
このコメントの際にコードを見直した際、 #95 の課題を発見したため、Issueを起票しました

@ZEKE320 ZEKE320 linked an issue Apr 13, 2024 that may be closed by this pull request
13 tasks
@ZEKE320 ZEKE320 requested a review from nkte8 April 13, 2024 13:04
@ZEKE320 ZEKE320 marked this pull request as ready for review April 13, 2024 13:04
@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 13, 2024

@nkte8
下記二点対応を行いました。レビューをお願いします!

  • 1064677 src/components/Client/common/*にPrettierを適用
  • 5982e58 セレクトボックス共通実装にて、codeMapにジェネリック型を導入

Copy link
Owner

@nkte8 nkte8 left a comment

Choose a reason for hiding this comment

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

対応ありがとうございます!方針自体はOKです!コメントの追加(おまかせしますがコードの改善コメントの対応)をお願いします

const item: CodeMap<CodeType> | undefined = codeMap.find(
opt => opt.label === event.target.value,
)
const code: CodeType | undefined = item?.code ?? codeMap[0]?.code
Copy link
Owner

Choose a reason for hiding this comment

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

方針自体はDiscordで可決した通りOKです!

内容へのコメントとして、この一行の情報量が(typescript初心者的な意見ですが)すこし多い気がするので、コメントで補足をしていただきたいです。以下は例です

// 選択項目の`code`パラメータが`undefined`または`null`の場合に、codeMapの最初の項を採用する
const code: CodeType | undefined = item?.code ?? codeMap[0]?.code

あとは...なんとなくですがcodeMap[0]?.codeがあまり好ましくないので、この判定の前にcodeMapのlengthが0以上でなかったらreturnしちゃってもいいかもしれませんね。おまかせします。

Copy link
Collaborator Author

@ZEKE320 ZEKE320 Apr 13, 2024

Choose a reason for hiding this comment

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

今気がついたのですが、現状ではnullも正常値として扱われているということでしたね

となると??で判定するのは正しくないので、

// `item`が`undefined`の場合に、codeMapの最初の項を採用する。
const code: CodeType =
    item === undefined ? codeMap[0].code : item.code 

setCode(code)

冗長な書き方とはなってしまいますが、こちらの実装の方が良いでしょうか?

Copy link
Owner

Choose a reason for hiding this comment

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

現状ではnullも正常値として扱われているということでしたね

そうでした、気づいていただきありがとうございます!
ぜひそっちの書き方でおねがいします!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

対応行いました!

@ZEKE320 ZEKE320 requested a review from nkte8 April 13, 2024 16:34
@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 13, 2024

@nkte8
下記2点対応行いました
改めてレビューをお願いします

  • bb4bbbc codeMap: CodeMap[]が空の場合の早期リターン追加
  • 3299ddd itemundefinedの場合だけ、codeMap[0]を用いる実装に変更 & コメント追加

Copy link
Owner

@nkte8 nkte8 left a comment

Choose a reason for hiding this comment

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

対応ありがとうございました!マージします。
Discordでも記載していただいていたように、現在nullとなっている箇所は紛らわしさの解消の意味で、いずれはundefinedで置き換えていきたいですね。

@nkte8 nkte8 merged commit a12296d into develop Apr 13, 2024
4 checks passed
@nkte8 nkte8 deleted the preview/issue_59 branch April 13, 2024 16:57
nkte8 added a commit that referenced this pull request Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Improve code]: components/Client/common/* 手動formatter・Linter対応
2 participants