2
0

render.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Label } from 'semantic-ui-react';
  2. import {Tag} from "@douyinfe/semi-ui";
  3. export function renderText(text, limit) {
  4. if (text.length > limit) {
  5. return text.slice(0, limit - 3) + '...';
  6. }
  7. return text;
  8. }
  9. export function renderGroup(group) {
  10. if (group === '') {
  11. return <Tag size='large'>default</Tag>;
  12. }
  13. let groups = group.split(',');
  14. groups.sort();
  15. return <>
  16. {groups.map((group) => {
  17. if (group === 'vip' || group === 'pro') {
  18. return <Tag size='large' color='yellow'>{group}</Tag>;
  19. } else if (group === 'svip' || group === 'premium') {
  20. return <Tag size='large' color='red'>{group}</Tag>;
  21. }
  22. if (group === 'default') {
  23. return <Tag size='large'>{group}</Tag>;
  24. } else {
  25. return <Tag size='large' color={stringToColor(group)}>{group}</Tag>;
  26. }
  27. })}
  28. </>;
  29. }
  30. export function renderNumber(num) {
  31. if (num >= 1000000000) {
  32. return (num / 1000000000).toFixed(1) + 'B';
  33. } else if (num >= 1000000) {
  34. return (num / 1000000).toFixed(1) + 'M';
  35. } else if (num >= 10000) {
  36. return (num / 1000).toFixed(1) + 'k';
  37. } else {
  38. return num;
  39. }
  40. }
  41. export function renderNumberWithPoint(num) {
  42. num = num.toFixed(2);
  43. if (num >= 100000) {
  44. // Convert number to string to manipulate it
  45. let numStr = num.toString();
  46. // Find the position of the decimal point
  47. let decimalPointIndex = numStr.indexOf('.');
  48. let wholePart = numStr;
  49. let decimalPart = '';
  50. // If there is a decimal point, split the number into whole and decimal parts
  51. if (decimalPointIndex !== -1) {
  52. wholePart = numStr.slice(0, decimalPointIndex);
  53. decimalPart = numStr.slice(decimalPointIndex);
  54. }
  55. // Take the first two and last two digits of the whole number part
  56. let shortenedWholePart = wholePart.slice(0, 2) + '..' + wholePart.slice(-2);
  57. // Return the formatted number
  58. return shortenedWholePart + decimalPart;
  59. }
  60. // If the number is less than 100,000, return it unmodified
  61. return num;
  62. }
  63. export function getQuotaPerUnit() {
  64. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  65. quotaPerUnit = parseFloat(quotaPerUnit);
  66. return quotaPerUnit;
  67. }
  68. export function renderQuota(quota, digits = 2) {
  69. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  70. let displayInCurrency = localStorage.getItem('display_in_currency');
  71. quotaPerUnit = parseFloat(quotaPerUnit);
  72. displayInCurrency = displayInCurrency === 'true';
  73. if (displayInCurrency) {
  74. return '$' + (quota / quotaPerUnit).toFixed(digits);
  75. }
  76. return renderNumber(quota);
  77. }
  78. export function renderQuotaWithPrompt(quota, digits) {
  79. let displayInCurrency = localStorage.getItem('display_in_currency');
  80. displayInCurrency = displayInCurrency === 'true';
  81. if (displayInCurrency) {
  82. return `(等价金额:${renderQuota(quota, digits)})`;
  83. }
  84. return '';
  85. }
  86. const colors = ['amber', 'blue', 'cyan', 'green', 'grey', 'indigo',
  87. 'light-blue', 'lime', 'orange', 'pink',
  88. 'purple', 'red', 'teal', 'violet', 'yellow'
  89. ]
  90. export function stringToColor(str) {
  91. let sum = 0;
  92. // 对字符串中的每个字符进行操作
  93. for (let i = 0; i < str.length; i++) {
  94. // 将字符的ASCII值加到sum中
  95. sum += str.charCodeAt(i);
  96. }
  97. // 使用模运算得到个位数
  98. let i = sum % colors.length;
  99. return colors[i];
  100. }