|
1 | 1 | <script lang="ts"> |
2 | | - import { getUTCOffset, timeFromNow, toLocaleDateTime } from '$lib/helpers/date'; |
| 2 | + import { type ComponentProps } from 'svelte'; |
3 | 3 | import { capitalize } from '$lib/helpers/string'; |
| 4 | + import { getUTCOffset, timeFromNow, toLocaleDateTime } from '$lib/helpers/date'; |
4 | 5 | import { Badge, InteractiveText, Layout, Popover, Typography } from '@appwrite.io/pink-svelte'; |
5 | | - import type { ComponentProps } from 'svelte'; |
6 | 6 |
|
7 | 7 | export let time: string = ''; |
8 | | - export let placement: ComponentProps<Popover>['placement'] = 'bottom-end'; |
| 8 | + export let placement: ComponentProps<Popover>['placement'] = 'bottom'; |
9 | 9 |
|
10 | 10 | function timeDifference(dateString: string): string { |
11 | 11 | const SECONDS_IN_MINUTE = 60; |
|
24 | 24 | { label: 'month', value: Math.floor(diffInSeconds / SECONDS_IN_MONTH) }, |
25 | 25 | { label: 'day', value: Math.floor(diffInSeconds / SECONDS_IN_DAY) % 30 }, |
26 | 26 | { label: 'hour', value: Math.floor(diffInSeconds / SECONDS_IN_HOUR) % 24 }, |
27 | | - { label: 'minute', value: Math.floor(diffInSeconds / SECONDS_IN_MINUTE) % 60 } |
| 27 | + { label: 'minute', value: Math.floor(diffInSeconds / SECONDS_IN_MINUTE) % 60 }, |
| 28 | + { label: 'second', value: diffInSeconds % 60 } |
28 | 29 | ]; |
29 | 30 |
|
30 | | - const formattedTime = timeParts |
31 | | - .filter((unit) => unit.value > 0) |
32 | | - .slice(0, 3) |
| 31 | + const month = timeParts[0].value; |
| 32 | + const day = timeParts[1].value; |
| 33 | + const hour = timeParts[2].value; |
| 34 | + const second = timeParts[4].value; |
| 35 | +
|
| 36 | + let outputParts: { label: string; value: number }[]; |
| 37 | +
|
| 38 | + if (month === 0 && day === 0 && hour === 0 && second > 0) { |
| 39 | + // Seconds exist but no months, days, hours — show minutes + seconds |
| 40 | + outputParts = timeParts.slice(3).filter((unit) => unit.value > 0); |
| 41 | + } else { |
| 42 | + // Normal logic — top 3 non-zero units, no seconds. |
| 43 | + outputParts = timeParts |
| 44 | + .filter((unit) => unit.label !== 'second' && unit.value > 0) |
| 45 | + .slice(0, 3); |
| 46 | + } |
| 47 | +
|
| 48 | + const formattedTime = outputParts |
33 | 49 | .map((unit) => `${unit.value} ${unit.label}${unit.value > 1 ? 's' : ''}`) |
34 | 50 | .join(', '); |
35 | 51 |
|
36 | 52 | return formattedTime ? `${formattedTime} ago` : 'Just now'; |
37 | 53 | } |
38 | 54 |
|
| 55 | + let isMouseOverTooltip = false; |
| 56 | + function hidePopover(hideTooltip: () => void, timeout = true) { |
| 57 | + if (!timeout) { |
| 58 | + isMouseOverTooltip = false; |
| 59 | + return hideTooltip(); |
| 60 | + } |
| 61 | +
|
| 62 | + setTimeout(() => { |
| 63 | + if (!isMouseOverTooltip) { |
| 64 | + hideTooltip(); |
| 65 | + } |
| 66 | + }, 50); |
| 67 | + } |
| 68 | +
|
39 | 69 | $: timeToString = time ? timeDifference(time) : 'Invalid time'; |
40 | 70 | </script> |
41 | 71 |
|
42 | | -<Popover let:toggle {placement} portal> |
43 | | - <button on:mouseenter={(e) => toggle(e)}> |
| 72 | +<Popover let:show let:hide {placement} portal> |
| 73 | + <button on:mouseenter={() => setTimeout(show, 100)} on:mouseleave={() => hidePopover(hide)}> |
44 | 74 | <slot>{capitalize(timeFromNow(time))}</slot> |
45 | 75 | </button> |
46 | 76 |
|
47 | | - <Layout.Stack gap="m" alignContent="flex-start" slot="tooltip"> |
48 | | - <Typography.Text color="--fgcolor-neutral-tertiary" variant="m-400" |
49 | | - >{timeToString}</Typography.Text> |
| 77 | + <div |
| 78 | + let:hide |
| 79 | + slot="tooltip" |
| 80 | + role="tooltip" |
| 81 | + style:padding-top="1rem" |
| 82 | + style:margin-top="-1rem" |
| 83 | + style:padding-bottom="1rem" |
| 84 | + style:margin-bottom="-1rem" |
| 85 | + on:mouseenter={() => (isMouseOverTooltip = true)} |
| 86 | + on:mouseleave={() => hidePopover(hide, false)}> |
| 87 | + <Layout.Stack gap="s" alignContent="flex-start"> |
| 88 | + <!-- `Raw time` as per design --> |
| 89 | + <Typography.Caption color="--fgcolor-neutral-tertiary" variant="400"> |
| 90 | + {timeToString} |
| 91 | + </Typography.Caption> |
50 | 92 |
|
51 | | - <Layout.Stack gap="xxs"> |
52 | | - <Layout.Stack direction="row" justifyContent="space-between"> |
53 | | - <Typography.Text variant="m-400"> |
| 93 | + <!-- `Absolute time` as per design --> |
| 94 | + <Layout.Stack gap="xxs"> |
| 95 | + <Layout.Stack direction="row" justifyContent="space-between"> |
54 | 96 | <InteractiveText |
| 97 | + isVisible |
55 | 98 | variant="copy" |
56 | 99 | text={toLocaleDateTime(time, 'UTC')} |
57 | | - isVisible /> |
58 | | - </Typography.Text> |
59 | | - <Badge variant="secondary" content="UTC" size="xs" /> |
60 | | - </Layout.Stack> |
| 100 | + value={new Date(toLocaleDateTime(time, 'UTC')).toISOString()} /> |
61 | 101 |
|
62 | | - <Layout.Stack direction="row" justifyContent="space-between"> |
63 | | - <Typography.Text variant="m-400"> |
64 | | - <InteractiveText variant="copy" text={toLocaleDateTime(time)} isVisible /> |
65 | | - </Typography.Text> |
| 102 | + <Badge variant="secondary" content="UTC" size="xs" /> |
| 103 | + </Layout.Stack> |
66 | 104 |
|
67 | | - <Badge variant="secondary" content="UTC{getUTCOffset()}" size="xs" /> |
| 105 | + <Layout.Stack direction="row" justifyContent="space-between"> |
| 106 | + <InteractiveText |
| 107 | + isVisible |
| 108 | + variant="copy" |
| 109 | + text={toLocaleDateTime(time)} |
| 110 | + value={new Date(toLocaleDateTime(time)).toISOString()} /> |
| 111 | + <Badge variant="secondary" content="UTC{getUTCOffset()}" size="xs" /> |
| 112 | + </Layout.Stack> |
68 | 113 | </Layout.Stack> |
69 | 114 | </Layout.Stack> |
70 | | - </Layout.Stack> |
| 115 | + </div> |
71 | 116 | </Popover> |
0 commit comments