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

use object (key is type safe) to choice alert message rather than switch #479

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/session/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,16 @@ const Timer = ({
setIterationCount(1);

if (window.Notification && Notification.permission !== "granted") {
const alertMessageByNotificationPermission: {
[key in NotificationPermission]: string;
} = {
granted: "Thanks to accept the notification :)",
denied: "You rejected the notification :(Please accept it.",
default: "Can not judge to use notification :(Please accept it.",
};
Copy link
Member

@kachick kachick Jun 28, 2022

Choose a reason for hiding this comment

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

Suggested change
const alertMessageByNotificationPermission: {
[key in NotificationPermission]: string;
} = {
granted: "Thanks to accept the notification :)",
denied: "You rejected the notification :(Please accept it.",
default: "Can not judge to use notification :(Please accept it.",
};
const alertMessageByNotificationPermission = {
granted: "Thanks to accept the notification :)",
denied: "You rejected the notification :(Please accept it.",
default: "Can not judge to use notification :(Please accept it.",
} as const;

is simple and robust here, dependent NotificationPermission is enough in fetching key, I think

And I suggest to put this object out of this Timer function, this does not depends any context and variables.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got another idea now. How about this?

      if (window.Notification && Notification.permission !== "granted") {
        Notification.requestPermission((result) => {
          const alertMessageByNotificationPermission: {
            [key in typeof result]: string;
          } = {
            granted: "Thanks to accept the notification :)",
            denied: "You rejected the notification :(Please accept it.",
            default: "Can not judge to use notification :(Please accept it.",
          };
          alert(alertMessageByNotificationPermission[result]);
        });
      }

My thought of key in ~'s advantages are:

  • Using as const doesn't prevent adding "extra" elements.
  • key in ~ can get good IDE support.

On the other hand, I agree using as const is more simpler.
I'd like to hear your opinion again which is better.

Copy link
Member

Choose a reason for hiding this comment

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

I can understand your motivation to write key ~.

I have 2 suggestions as below

        Notification.requestPermission((result) => {
          alert(
            {
              granted: "Thanks to accept the notification :)",
              denied: "You rejected the notification :(Please accept it.",
              default: "Can not judge to use notification :(Please accept it.",
            }[result]
          );
        });

This is simple and typescript supports the branches, my favor.
The constant only use here, less cost, so writing as literal is best, I think.

However if you want to extract to another variable, as const makes readonly, I prefer the benefit.

Suggested change
const alertMessageByNotificationPermission: {
[key in NotificationPermission]: string;
} = {
granted: "Thanks to accept the notification :)",
denied: "You rejected the notification :(Please accept it.",
default: "Can not judge to use notification :(Please accept it.",
};
const alertMessageByNotificationPermission: Readonly<{
[key in NotificationPermission]: string;
}> = {
granted: "Thanks to accept the notification :)",
denied: "You rejected the notification :(Please accept it.",
default: "Can not judge to use notification :(Please accept it.",
};

This is a similar way as this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like it! Let me update.

Copy link
Member Author

Choose a reason for hiding this comment

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

addressed in 2e302af
Cloud you check again?

Copy link
Member

Choose a reason for hiding this comment

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

I would update this with #724


Notification.requestPermission((result) => {
switch (result) {
case "granted":
alert("Thanks to accept the notification :)");
return;
case "denied":
alert("You rejected the notification :(Please accept it.");
return;
case "default":
alert("Can not judge to use notification :(Please accept it.");
return;
default:
const _: never = result;
return _;
}
alert(alertMessageByNotificationPermission[result]);
});
}
}
Expand Down