Skip to content

Commit 3176ad5

Browse files
mCodexCopilot
andcommitted
refactor: optimize useHasSecret and useSecretItem hooks with useCallback
Co-authored-by: Copilot <copilot@github.com>
1 parent d563d7e commit 3176ad5

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/__tests__/__mocks__/fixtures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const buildTestMetadata = (
2727
* @internal
2828
*/
2929
export const buildTestItem = (
30-
overrides: Partial<SensitiveInfoItem> & {
30+
overrides: Partial<Omit<SensitiveInfoItem, 'metadata'>> & {
3131
readonly metadata?: MetadataOverrides
3232
} = {}
3333
): SensitiveInfoItem => {

src/hooks/useHasSecret.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useCallback } from 'react'
12
import { hasItem } from '../core/storage'
23
import type { SensitiveInfoOptions } from '../sensitive-info.nitro'
34
import type { AsyncState } from './types'
@@ -21,8 +22,12 @@ export function useHasSecret(
2122
key: string,
2223
options?: UseHasSecretOptions
2324
): UseHasSecretResult {
25+
const runner = useCallback(
26+
(request: SensitiveInfoOptions) => hasItem(key, request),
27+
[key]
28+
)
2429
return useAsyncQuery<boolean, UseHasSecretOptions>(
25-
(request) => hasItem(key, request),
30+
runner,
2631
DEFAULTS,
2732
'useHasSecret.evaluate',
2833
options,

src/hooks/useSecretItem.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useCallback } from 'react'
12
import { getItem } from '../core/storage'
23
import type {
34
SensitiveInfoItem,
@@ -30,8 +31,12 @@ export function useSecretItem(
3031
key: string,
3132
options?: UseSecretItemOptions
3233
): UseSecretItemResult {
34+
const runner = useCallback(
35+
(request: SensitiveInfoOptions) => getItem(key, request),
36+
[key]
37+
)
3338
return useAsyncQuery<SensitiveInfoItem, UseSecretItemOptions>(
34-
(request) => getItem(key, request),
39+
runner,
3540
DEFAULTS,
3641
'useSecretItem.fetch',
3742
options,

0 commit comments

Comments
 (0)