render.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  10. * Render group tags based on the input group string
  11. * @param {string} group - The input group string
  12. * @returns {JSX.Element} - The rendered group tags
  13. */
  14. export function renderGroup(group) {
  15. if (group === '') {
  16. return (
  17. <Tag size='large' key='default'>
  18. default
  19. </Tag>
  20. );
  21. }
  22. const tagColors = {
  23. vip: 'yellow',
  24. pro: 'yellow',
  25. svip: 'red',
  26. premium: 'red',
  27. };
  28. const groups = group.split(',').sort();
  29. return (
  30. <span key={group}>
  31. {groups.map((group) => (
  32. <Tag
  33. size='large'
  34. color={tagColors[group] || stringToColor(group)}
  35. key={group}
  36. >
  37. {group}
  38. </Tag>
  39. ))}
  40. </span>
  41. );
  42. }
  43. export function renderNumber(num) {
  44. if (num >= 1000000000) {
  45. return (num / 1000000000).toFixed(1) + 'B';
  46. } else if (num >= 1000000) {
  47. return (num / 1000000).toFixed(1) + 'M';
  48. } else if (num >= 10000) {
  49. return (num / 1000).toFixed(1) + 'k';
  50. } else {
  51. return num;
  52. }
  53. }
  54. export function renderQuotaNumberWithDigit(num, digits = 2) {
  55. let displayInCurrency = localStorage.getItem('display_in_currency');
  56. num = num.toFixed(digits);
  57. if (displayInCurrency) {
  58. return '$' + num;
  59. }
  60. return num;
  61. }
  62. export function renderNumberWithPoint(num) {
  63. num = num.toFixed(2);
  64. if (num >= 100000) {
  65. // Convert number to string to manipulate it
  66. let numStr = num.toString();
  67. // Find the position of the decimal point
  68. let decimalPointIndex = numStr.indexOf('.');
  69. let wholePart = numStr;
  70. let decimalPart = '';
  71. // If there is a decimal point, split the number into whole and decimal parts
  72. if (decimalPointIndex !== -1) {
  73. wholePart = numStr.slice(0, decimalPointIndex);
  74. decimalPart = numStr.slice(decimalPointIndex);
  75. }
  76. // Take the first two and last two digits of the whole number part
  77. let shortenedWholePart = wholePart.slice(0, 2) + '..' + wholePart.slice(-2);
  78. // Return the formatted number
  79. return shortenedWholePart + decimalPart;
  80. }
  81. // If the number is less than 100,000, return it unmodified
  82. return num;
  83. }
  84. export function getQuotaPerUnit() {
  85. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  86. quotaPerUnit = parseFloat(quotaPerUnit);
  87. return quotaPerUnit;
  88. }
  89. export function renderUnitWithQuota(quota) {
  90. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  91. quotaPerUnit = parseFloat(quotaPerUnit);
  92. quota = parseFloat(quota);
  93. return quotaPerUnit * quota;
  94. }
  95. export function getQuotaWithUnit(quota, digits = 6) {
  96. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  97. quotaPerUnit = parseFloat(quotaPerUnit);
  98. return (quota / quotaPerUnit).toFixed(digits);
  99. }
  100. export function renderQuotaWithAmount(amount) {
  101. let displayInCurrency = localStorage.getItem('display_in_currency');
  102. displayInCurrency = displayInCurrency === 'true';
  103. if (displayInCurrency) {
  104. return '$' + amount;
  105. } else {
  106. return renderUnitWithQuota(amount);
  107. }
  108. }
  109. export function renderQuota(quota, digits = 2) {
  110. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  111. let displayInCurrency = localStorage.getItem('display_in_currency');
  112. quotaPerUnit = parseFloat(quotaPerUnit);
  113. displayInCurrency = displayInCurrency === 'true';
  114. if (displayInCurrency) {
  115. return '$' + (quota / quotaPerUnit).toFixed(digits);
  116. }
  117. return renderNumber(quota);
  118. }
  119. export function renderQuotaWithPrompt(quota, digits) {
  120. let displayInCurrency = localStorage.getItem('display_in_currency');
  121. displayInCurrency = displayInCurrency === 'true';
  122. if (displayInCurrency) {
  123. return `(等价金额:${renderQuota(quota, digits)})`;
  124. }
  125. return '';
  126. }
  127. const colors = [
  128. 'amber',
  129. 'blue',
  130. 'cyan',
  131. 'green',
  132. 'grey',
  133. 'indigo',
  134. 'light-blue',
  135. 'lime',
  136. 'orange',
  137. 'pink',
  138. 'purple',
  139. 'red',
  140. 'teal',
  141. 'violet',
  142. 'yellow',
  143. ];
  144. export const modelColorMap = {
  145. 'dall-e': 'rgb(147,112,219)', // 深紫色
  146. 'dall-e-2': 'rgb(147,112,219)', // 介于紫色和蓝色之间的色调
  147. 'dall-e-3': 'rgb(153,50,204)', // 介于紫罗兰和洋红之间的色调
  148. midjourney: 'rgb(136,43,180)', // 介于紫罗兰和洋红之间的色调
  149. 'gpt-3.5-turbo': 'rgb(184,227,167)', // 浅绿色
  150. 'gpt-3.5-turbo-0301': 'rgb(131,220,131)', // 亮绿色
  151. 'gpt-3.5-turbo-0613': 'rgb(60,179,113)', // 海洋绿
  152. 'gpt-3.5-turbo-1106': 'rgb(32,178,170)', // 浅海洋绿
  153. 'gpt-3.5-turbo-16k': 'rgb(252,200,149)', // 淡橙色
  154. 'gpt-3.5-turbo-16k-0613': 'rgb(255,181,119)', // 淡桃色
  155. 'gpt-3.5-turbo-instruct': 'rgb(175,238,238)', // 粉蓝色
  156. 'gpt-4': 'rgb(135,206,235)', // 天蓝色
  157. 'gpt-4-0314': 'rgb(70,130,180)', // 钢蓝色
  158. 'gpt-4-0613': 'rgb(100,149,237)', // 矢车菊蓝
  159. 'gpt-4-1106-preview': 'rgb(30,144,255)', // 道奇蓝
  160. 'gpt-4-0125-preview': 'rgb(2,177,236)', // 深天蓝
  161. 'gpt-4-turbo-preview': 'rgb(2,177,255)', // 深天蓝
  162. 'gpt-4-32k': 'rgb(104,111,238)', // 中紫色
  163. 'gpt-4-32k-0314': 'rgb(90,105,205)', // 暗灰蓝色
  164. 'gpt-4-32k-0613': 'rgb(61,71,139)', // 暗蓝灰色
  165. 'gpt-4-all': 'rgb(65,105,225)', // 皇家蓝
  166. 'gpt-4-gizmo-*': 'rgb(0,0,255)', // 纯蓝色
  167. 'gpt-4-vision-preview': 'rgb(25,25,112)', // 午夜蓝
  168. 'text-ada-001': 'rgb(255,192,203)', // 粉红色
  169. 'text-babbage-001': 'rgb(255,160,122)', // 浅珊瑚色
  170. 'text-curie-001': 'rgb(219,112,147)', // 苍紫罗兰色
  171. 'text-davinci-002': 'rgb(199,21,133)', // 中紫罗兰红色
  172. 'text-davinci-003': 'rgb(219,112,147)', // 苍紫罗兰色(与Curie相同,表示同一个系列)
  173. 'text-davinci-edit-001': 'rgb(255,105,180)', // 热粉色
  174. 'text-embedding-ada-002': 'rgb(255,182,193)', // 浅粉红
  175. 'text-embedding-v1': 'rgb(255,174,185)', // 浅粉红色(略有区别)
  176. 'text-moderation-latest': 'rgb(255,130,171)', // 强粉色
  177. 'text-moderation-stable': 'rgb(255,160,122)', // 浅珊瑚色(与Babbage相同,表示同一类功能)
  178. 'tts-1': 'rgb(255,140,0)', // 深橙色
  179. 'tts-1-1106': 'rgb(255,165,0)', // 橙色
  180. 'tts-1-hd': 'rgb(255,215,0)', // 金色
  181. 'tts-1-hd-1106': 'rgb(255,223,0)', // 金黄色(略有区别)
  182. 'whisper-1': 'rgb(245,245,220)', // 米色
  183. };
  184. export function stringToColor(str) {
  185. let sum = 0;
  186. // 对字符串中的每个字符进行操作
  187. for (let i = 0; i < str.length; i++) {
  188. // 将字符的ASCII值加到sum中
  189. sum += str.charCodeAt(i);
  190. }
  191. // 使用模运算得到个位数
  192. let i = sum % colors.length;
  193. return colors[i];
  194. }