mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix: one status tile list for both admin cards; clear the copied timer on unmount
- The mail import's status tiles (which, labels, tones) were written out in the desktop and the mobile card alike; statTiles in the shared model holds them, each card keeps its own look. - The journey share link set 'copied' back two seconds after a copy even when the dialog had closed, which also broke a client test run (window gone at teardown). useCopied, the trip share's hook, moves to hooks/ and serves both; it clears its timer on unmount. - PlanLlmConfig is not exported: only the union is used outside.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// FE-HOOK-COPIED-001 to FE-HOOK-COPIED-003
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('../utils/clipboard', () => ({ copyText: vi.fn(async () => true) }));
|
||||
|
||||
import { copyText } from '../utils/clipboard';
|
||||
import { useCopied } from './useCopied';
|
||||
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
describe('useCopied', () => {
|
||||
it('FE-HOOK-COPIED-001: shows copied for two seconds, a second copy starting the time over', async () => {
|
||||
const { result } = renderHook(() => useCopied());
|
||||
await act(() => result.current.copy('https://x'));
|
||||
expect(result.current.copied).toBe(true);
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1500);
|
||||
});
|
||||
await act(() => result.current.copy('https://x'));
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1500);
|
||||
});
|
||||
expect(result.current.copied).toBe(true);
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(500);
|
||||
});
|
||||
expect(result.current.copied).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-HOOK-COPIED-002: nothing to copy or a refused clipboard shows nothing', async () => {
|
||||
const { result } = renderHook(() => useCopied());
|
||||
await act(() => result.current.copy(null));
|
||||
expect(copyText).not.toHaveBeenCalledWith(null);
|
||||
vi.mocked(copyText).mockResolvedValueOnce(false);
|
||||
await act(() => result.current.copy('https://x'));
|
||||
expect(result.current.copied).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-HOOK-COPIED-003: unmounting clears the timer, so no state is set afterwards', async () => {
|
||||
const { result, unmount } = renderHook(() => useCopied());
|
||||
await act(() => result.current.copy('https://x'));
|
||||
unmount();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { copyText } from '../utils/clipboard';
|
||||
|
||||
/**
|
||||
* Copy text and show "Copied" for two seconds, the timer restarting on every
|
||||
* copy. The timer is cleared on unmount, so a dialog closed right after a copy
|
||||
* sets no state on a component that is gone.
|
||||
*/
|
||||
export function useCopied(ms = 2000) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
},
|
||||
[]
|
||||
);
|
||||
const copy = async (text: string | null) => {
|
||||
if (!text || !(await copyText(text))) return;
|
||||
setCopied(true);
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
timer.current = setTimeout(() => setCopied(false), ms);
|
||||
};
|
||||
return { copied, copy };
|
||||
}
|
||||
Reference in New Issue
Block a user