AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.ts 2.5KB

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