AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

format.ts 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import BillingController from '@/actions/App/Http/Controllers/BillingController';
  2. import OrderController from '@/actions/App/Http/Controllers/OrderController';
  3. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  4. import StoreController from '@/actions/App/Http/Controllers/StoreController';
  5. import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
  6. /** "RM 12,480" or, with cents, "RM 87.90". */
  7. export function ringgit(amount: number, cents = false): string {
  8. return (
  9. 'RM ' +
  10. amount.toLocaleString('en-MY', {
  11. minimumFractionDigits: cents ? 2 : 0,
  12. maximumFractionDigits: cents ? 2 : 0,
  13. })
  14. );
  15. }
  16. /** "RM 1.2k" for chart labels. */
  17. export function shortRinggit(amount: number): string {
  18. return amount >= 1000
  19. ? `RM ${(amount / 1000).toLocaleString('en-MY', { maximumFractionDigits: 1 })}k`
  20. : `RM ${Math.round(amount)}`;
  21. }
  22. /** "2 hours ago", "Yesterday", "20 Nov". */
  23. export function when(iso: string | null): string {
  24. if (!iso) {
  25. return '';
  26. }
  27. const date = new Date(iso);
  28. const minutes = Math.round((Date.now() - date.getTime()) / 60_000);
  29. if (minutes < 1) {
  30. return 'Just now';
  31. }
  32. if (minutes < 60) {
  33. return `${minutes} min ago`;
  34. }
  35. if (minutes < 60 * 24 && date.getDate() === new Date().getDate()) {
  36. const hours = Math.round(minutes / 60);
  37. return hours === 1 ? '1 hour ago' : `${hours} hours ago`;
  38. }
  39. const yesterday = new Date();
  40. yesterday.setDate(yesterday.getDate() - 1);
  41. if (date.toDateString() === yesterday.toDateString()) {
  42. return 'Yesterday';
  43. }
  44. return date.toLocaleDateString('en-MY', { day: 'numeric', month: 'short' });
  45. }
  46. /** Where a "Needs attention" item or a pulse button leads. */
  47. export function actionHref(
  48. kind: string,
  49. storeSlug: string | null | undefined,
  50. productId?: number | null,
  51. ): string {
  52. if (kind === 'plan' || !storeSlug) {
  53. return BillingController.index.url();
  54. }
  55. if (kind === 'ship') {
  56. return OrderController.index.url(storeSlug);
  57. }
  58. if (kind === 'restock' && productId) {
  59. return ProductController.edit.url({
  60. slug: storeSlug,
  61. product: productId,
  62. });
  63. }
  64. if (kind === 'restock') {
  65. return ProductController.index.url(storeSlug);
  66. }
  67. if (kind === 'payments') {
  68. return `${StoreSettingsController.edit.url(storeSlug)}#payments`;
  69. }
  70. return StoreController.show.url(storeSlug);
  71. }
  72. export function prefersReducedMotion(): boolean {
  73. return (
  74. typeof window !== 'undefined' &&
  75. window.matchMedia('(prefers-reduced-motion: reduce)').matches
  76. );
  77. }