render.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Label } from 'semantic-ui-react';
  2. export function renderText(text, limit) {
  3. if (text.length > limit) {
  4. return text.slice(0, limit - 3) + '...';
  5. }
  6. return text;
  7. }
  8. export function renderGroup(group) {
  9. if (group === '') {
  10. return <Label>default</Label>;
  11. }
  12. let groups = group.split(',');
  13. groups.sort();
  14. return <>
  15. {groups.map((group) => {
  16. if (group === 'vip' || group === 'pro') {
  17. return <Label color='yellow'>{group}</Label>;
  18. } else if (group === 'svip' || group === 'premium') {
  19. return <Label color='red'>{group}</Label>;
  20. }
  21. return <Label>{group}</Label>;
  22. })}
  23. </>;
  24. }
  25. export function renderNumber(num) {
  26. if (num >= 1000000000) {
  27. return (num / 1000000000).toFixed(1) + 'B';
  28. } else if (num >= 1000000) {
  29. return (num / 1000000).toFixed(1) + 'M';
  30. } else if (num >= 10000) {
  31. return (num / 1000).toFixed(1) + 'k';
  32. } else {
  33. return num;
  34. }
  35. }
  36. export function getQuotaPerUnit() {
  37. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  38. quotaPerUnit = parseFloat(quotaPerUnit);
  39. return quotaPerUnit;
  40. }
  41. export function renderQuota(quota, digits = 2) {
  42. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  43. let displayInCurrency = localStorage.getItem('display_in_currency');
  44. quotaPerUnit = parseFloat(quotaPerUnit);
  45. displayInCurrency = displayInCurrency === 'true';
  46. if (displayInCurrency) {
  47. return '$' + (quota / quotaPerUnit).toFixed(digits);
  48. }
  49. return renderNumber(quota);
  50. }
  51. export function renderQuotaWithPrompt(quota, digits) {
  52. let displayInCurrency = localStorage.getItem('display_in_currency');
  53. displayInCurrency = displayInCurrency === 'true';
  54. if (displayInCurrency) {
  55. return `(等价金额:${renderQuota(quota, digits)})`;
  56. }
  57. return '';
  58. }
  59. const colors = ['amber', 'blue', 'cyan', 'green', 'grey', 'indigo',
  60. 'light-blue', 'lime', 'orange', 'pink',
  61. 'purple', 'red', 'teal', 'violet', 'yellow'
  62. ]
  63. export function stringToColor(str) {
  64. let sum = 0;
  65. // 对字符串中的每个字符进行操作
  66. for (let i = 0; i < str.length; i++) {
  67. // 将字符的ASCII值加到sum中
  68. sum += str.charCodeAt(i);
  69. }
  70. // 使用模运算得到个位数
  71. let i = sum % colors.length;
  72. return colors[i];
  73. }