render.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 renderQuota(quota, digits = 2) {
  37. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  38. let displayInCurrency = localStorage.getItem('display_in_currency');
  39. quotaPerUnit = parseFloat(quotaPerUnit);
  40. displayInCurrency = displayInCurrency === 'true';
  41. if (displayInCurrency) {
  42. return '$' + (quota / quotaPerUnit).toFixed(digits);
  43. }
  44. return renderNumber(quota);
  45. }
  46. export function renderQuotaWithPrompt(quota, digits) {
  47. let displayInCurrency = localStorage.getItem('display_in_currency');
  48. displayInCurrency = displayInCurrency === 'true';
  49. if (displayInCurrency) {
  50. return `(等价金额:${renderQuota(quota, digits)})`;
  51. }
  52. return '';
  53. }
  54. const colors = ['amber', 'blue', 'cyan', 'green', 'grey', 'indigo',
  55. 'light-blue', 'lime', 'orange', 'pink',
  56. 'purple', 'red', 'teal', 'violet', 'yellow'
  57. ]
  58. export function stringToColor(str) {
  59. let sum = 0;
  60. // 对字符串中的每个字符进行操作
  61. for (let i = 0; i < str.length; i++) {
  62. // 将字符的ASCII值加到sum中
  63. sum += str.charCodeAt(i);
  64. }
  65. // 使用模运算得到个位数
  66. let i = sum % colors.length;
  67. return colors[i];
  68. }