Skip to content

Commit f461fa7

Browse files
authored
chore: Shared JS/TS Conversions Part 3 (#3500)
1 parent fe14916 commit f461fa7

File tree

9 files changed

+44
-138
lines changed

9 files changed

+44
-138
lines changed

src/services/pull/usePull.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ describe('usePull', () => {
164164
await waitFor(() =>
165165
expect(result.current.data).toEqual({
166166
defaultBranch: 'main',
167-
hasAccess: true,
168167
pull: {
169168
behindBy: 82367894,
170169
behindByCommit: '1798hvs8ofhn',

src/services/pull/usePull.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import Api from 'shared/api'
1717
import { type NetworkErrorObject } from 'shared/api/helpers'
1818
import { UploadTypeEnum } from 'shared/utils/commit'
19-
import { userHasAccess } from 'shared/utils/user'
2019
import A from 'ui/A'
2120

2221
import { PullCompareWithBaseFragment } from './fragments'
@@ -372,10 +371,6 @@ export function usePull({
372371
pull: {
373372
...pull,
374373
},
375-
hasAccess: userHasAccess({
376-
privateRepo: data?.owner?.repository?.private,
377-
isCurrentUserPartOfOrg: data?.owner?.isCurrentUserPartOfOrg,
378-
}),
379374
defaultBranch: data?.owner?.repository?.defaultBranch,
380375
}
381376
}),

src/shared/utils/dates.test.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/shared/utils/dates.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { formatTimeFromSeconds, formatTimeToNow } from './dates'
2+
3+
describe('formatTimeToNow', () => {
4+
it('returns null when date is null', () => {
5+
expect(formatTimeToNow(undefined)).toBe(null)
6+
})
7+
8+
it('returns the correct time format when date is a unix timestamp', () => {
9+
vi.useFakeTimers().setSystemTime(new Date('2025-01-01'))
10+
expect(formatTimeToNow(1715731200)).toBe('8 months ago')
11+
vi.useRealTimers()
12+
})
13+
14+
it('returns the correct time format when date is a iso string', () => {
15+
vi.useFakeTimers().setSystemTime(new Date('2025-01-01'))
16+
expect(formatTimeToNow('2024-09-01')).toBe('4 months ago')
17+
vi.useRealTimers()
18+
})
19+
})
20+
21+
describe('formatTimeFromSeconds', () => {
22+
it('returns "N/A" when totalSeconds is null', () => {
23+
expect(formatTimeFromSeconds(null)).toBe('N/A')
24+
})
25+
26+
it('returns "N/A" when totalSeconds is undefined', () => {
27+
expect(formatTimeFromSeconds(undefined)).toBe('N/A')
28+
})
29+
30+
it('returns "0s" when totalSeconds is 0', () => {
31+
expect(formatTimeFromSeconds(0)).toBe('0s')
32+
})
33+
34+
it('returns the correct time format when totalSeconds is greater than 0', () => {
35+
expect(formatTimeFromSeconds(3661)).toBe('1h 1m 1s')
36+
})
37+
})

src/shared/utils/dates.js renamed to src/shared/utils/dates.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import {
2-
format,
32
formatDistanceToNow,
43
fromUnixTime,
54
intervalToDuration,
65
parseISO,
76
} from 'date-fns'
8-
import { useMemo } from 'react'
97

10-
export function useDateFormatted(date, formatDescription = 'MMMM do yyyy') {
11-
return useMemo(() => {
12-
if (!date) return null
13-
const parser = typeof date === 'string' ? parseISO : fromUnixTime
14-
return format(parser(date), formatDescription)
15-
}, [date, formatDescription])
16-
}
17-
18-
export function formatTimeToNow(date) {
8+
export function formatTimeToNow(date?: string | number | null) {
199
if (!date) return null
2010

2111
const parsedDate =
@@ -25,7 +15,7 @@ export function formatTimeToNow(date) {
2515
})
2616
}
2717

28-
export const formatTimeFromSeconds = (totalSeconds) => {
18+
export const formatTimeFromSeconds = (totalSeconds?: number | null) => {
2919
if (totalSeconds === 0) return '0s'
3020
if (!totalSeconds) return 'N/A'
3121

src/shared/utils/snakeifyKeys.test.js renamed to src/shared/utils/snakeifyKeys.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ describe('snakeifyKeys', () => {
1010

1111
it('else passes through', () => {
1212
expect(snakeifyKeys([1, 2, 3])).toStrictEqual([1, 2, 3])
13+
// @ts-expect-error
1314
expect(snakeifyKeys(1)).toStrictEqual(1)
15+
// @ts-expect-error
1416
expect(snakeifyKeys('test')).toStrictEqual('test')
1517
})
1618
})

src/shared/utils/snakeifyKeys.js renamed to src/shared/utils/snakeifyKeys.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import snakeCase from 'lodash/snakeCase'
22

3-
export function snakeifyKeys(obj = {}) {
3+
export function snakeifyKeys(
4+
obj: Record<string, any> = {}
5+
): Record<string, any> {
46
if (obj !== null && obj.constructor === Object) {
57
return Object.keys(obj).reduce(
68
(result, key) => ({

src/shared/utils/user.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/shared/utils/user.test.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)