index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Plan } from "@/types";
  2. export const groupBy = function (xs, key) {
  3. return xs.reduce(function (rv, x) {
  4. ;(rv[x[key]] = rv[x[key]] || []).push(x);
  5. return rv;
  6. }, {});
  7. };
  8. export const sortByKeys = function (xs) {
  9. return Object.keys(xs)
  10. .sort()
  11. .reduce((obj, key) => {
  12. obj[key] = xs[key];
  13. return obj;
  14. }, {});
  15. };
  16. export const toPascalCase = function (text: string) {
  17. return text
  18. .replace(new RegExp(/[-_]+/, 'g'), ' ')
  19. .replace(new RegExp(/[^\w\s]/, 'g'), '')
  20. .replace(
  21. new RegExp(/\s+(.)(\w+)/, 'g'),
  22. ($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
  23. )
  24. .replace(new RegExp(/\s/, 'g'), '')
  25. .replace(new RegExp(/\w/), (s) => s.toUpperCase());
  26. };
  27. export const baseUrl = {
  28. development: 'http://localhost:3000',
  29. production: 'https://tabler-icons.io',
  30. }[process.env.NODE_ENV];
  31. export const getCurrentBrand = (hostname: string) => {
  32. if (hostname && hostname.match(/tabler-icons/)) {
  33. return 'tabler-icons';
  34. }
  35. return 'tabler-ui';
  36. };
  37. export const getNextAuthErrorMessage = (error: string): string => {
  38. // Nextauth errors
  39. // https://next-auth.js.org/configuration/pages#sign-in-page
  40. // OAuthSignin: Error in constructing an authorization URL (1, 2, 3),
  41. // OAuthCallback: Error in handling the response (1, 2, 3) from an OAuth provider.
  42. // OAuthCreateAccount: Could not create OAuth provider user in the database.
  43. // EmailCreateAccount: Could not create email provider user in the database.
  44. // Callback: Error in the OAuth callback handler route
  45. // OAuthAccountNotLinked: If the email on the account is already linked, but not with this OAuth account
  46. // EmailSignin: Sending the e-mail with the verification token failed
  47. // CredentialsSignin: The authorize callback returned null in the Credentials provider. We don't recommend providing information about which part of the credentials were wrong, as it might be abused by malicious hackers.
  48. // SessionRequired: The content of this page requires you to be signed in at all times. See useSession for configuration.
  49. // Default: Catch all, will apply, if none of the above matched
  50. switch (error) {
  51. case 'OAuthSignin':
  52. case 'OAuthCallback':
  53. case 'OAuthCreateAccount':
  54. case 'EmailCreateAccount':
  55. case 'Callback':
  56. return 'Try signing in with a different account.';
  57. case 'OAuthAccountNotLinked':
  58. return 'To confirm your identity, sign in with the same account you used originally.';
  59. case 'EmailSignin':
  60. return 'The e-mail could not be sent.';
  61. case 'CredentialsSignin':
  62. return 'Sign in failed. Check the details you provided are correct.';
  63. case 'SessionRequired':
  64. return 'Please sign in to access this page.';
  65. case 'Default':
  66. default:
  67. return 'Unable to sign in.';
  68. }
  69. };
  70. export const isPlanFeatured = (plan: Plan) => {
  71. return plan.variantName === 'Advanced'
  72. }