| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import BillingController from '@/actions/App/Http/Controllers/BillingController';
- import OrderController from '@/actions/App/Http/Controllers/OrderController';
- import ProductController from '@/actions/App/Http/Controllers/ProductController';
- import StoreController from '@/actions/App/Http/Controllers/StoreController';
-
- /** "RM 12,480" or, with cents, "RM 87.90". */
- export function ringgit(amount: number, cents = false): string {
- return (
- 'RM ' +
- amount.toLocaleString('en-MY', {
- minimumFractionDigits: cents ? 2 : 0,
- maximumFractionDigits: cents ? 2 : 0,
- })
- );
- }
-
- /** "RM 1.2k" for chart labels. */
- export function shortRinggit(amount: number): string {
- return amount >= 1000
- ? `RM ${(amount / 1000).toLocaleString('en-MY', { maximumFractionDigits: 1 })}k`
- : `RM ${Math.round(amount)}`;
- }
-
- /** "2 hours ago", "Yesterday", "20 Nov". */
- export function when(iso: string | null): string {
- if (!iso) {
- return '';
- }
-
- const date = new Date(iso);
- const minutes = Math.round((Date.now() - date.getTime()) / 60_000);
-
- if (minutes < 1) {
- return 'Just now';
- }
-
- if (minutes < 60) {
- return `${minutes} min ago`;
- }
-
- if (minutes < 60 * 24 && date.getDate() === new Date().getDate()) {
- const hours = Math.round(minutes / 60);
-
- return hours === 1 ? '1 hour ago' : `${hours} hours ago`;
- }
-
- const yesterday = new Date();
- yesterday.setDate(yesterday.getDate() - 1);
-
- if (date.toDateString() === yesterday.toDateString()) {
- return 'Yesterday';
- }
-
- return date.toLocaleDateString('en-MY', { day: 'numeric', month: 'short' });
- }
-
- /** Where a "Needs attention" item or a pulse button leads. */
- export function actionHref(
- kind: string,
- storeSlug: string | null | undefined,
- productId?: number | null,
- ): string {
- if (kind === 'plan' || !storeSlug) {
- return BillingController.index.url();
- }
-
- if (kind === 'ship') {
- return OrderController.index.url(storeSlug);
- }
-
- if (kind === 'restock' && productId) {
- return ProductController.edit.url({
- slug: storeSlug,
- product: productId,
- });
- }
-
- if (kind === 'restock') {
- return ProductController.index.url(storeSlug);
- }
-
- return StoreController.show.url(storeSlug);
- }
-
- export function prefersReducedMotion(): boolean {
- return (
- typeof window !== 'undefined' &&
- window.matchMedia('(prefers-reduced-motion: reduce)').matches
- );
- }
|