version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export const LOOKUP_DEBOUNCE_MS = 800;
|
||||
|
||||
export function useDebouncedValue<T>(value: T, delayMs: number): T {
|
||||
const [debounced, setDebounced] = useState(value);
|
||||
|
||||
useEffect(() => {
|
||||
const id = setTimeout(() => setDebounced(value), delayMs);
|
||||
return () => clearTimeout(id);
|
||||
}, [value, delayMs]);
|
||||
|
||||
return debounced;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
export type ResourceStatus = "loading" | "done" | "error";
|
||||
|
||||
export interface ResourceState<T> {
|
||||
status: ResourceStatus;
|
||||
data?: T;
|
||||
error?: unknown;
|
||||
}
|
||||
|
||||
const cache = new Map<string, unknown>();
|
||||
const inFlight = new Map<string, Promise<unknown>>();
|
||||
|
||||
function cachedHit<T>(key: string | null): T | undefined {
|
||||
if (key != null && cache.has(key)) return cache.get(key) as T;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function dedupedFetch<T>(key: string, fetcher: () => Promise<T>): Promise<T> {
|
||||
if (cache.has(key)) return Promise.resolve(cache.get(key) as T);
|
||||
|
||||
const running = inFlight.get(key) as Promise<T> | undefined;
|
||||
if (running) return running;
|
||||
|
||||
const promise = fetcher()
|
||||
.then((data) => {
|
||||
cache.set(key, data);
|
||||
return data;
|
||||
})
|
||||
.finally(() => {
|
||||
inFlight.delete(key);
|
||||
});
|
||||
|
||||
inFlight.set(key, promise);
|
||||
return promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a resource once per key, sharing the cache and in-flight requests
|
||||
* across all mounts. Reports the fetched (or cached) value through `onData`
|
||||
* so owners can hoist the result into their own state.
|
||||
*/
|
||||
export function useResource<T>(
|
||||
key: string | null,
|
||||
fetcher: (() => Promise<T>) | null,
|
||||
onData?: (data: T) => void
|
||||
): ResourceState<T> {
|
||||
const fetcherRef = useRef(fetcher);
|
||||
const onDataRef = useRef(onData);
|
||||
useEffect(() => {
|
||||
fetcherRef.current = fetcher;
|
||||
onDataRef.current = onData;
|
||||
});
|
||||
|
||||
const [state, setState] = useState<ResourceState<T>>(() => {
|
||||
const hit = cachedHit<T>(key);
|
||||
return hit !== undefined ? { status: "done", data: hit } : { status: "loading" };
|
||||
});
|
||||
|
||||
const [prevKey, setPrevKey] = useState<string | null>(key);
|
||||
if (key !== prevKey) {
|
||||
setPrevKey(key);
|
||||
const hit = cachedHit<T>(key);
|
||||
setState(hit !== undefined ? { status: "done", data: hit } : { status: "loading" });
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!key || !fetcherRef.current) return;
|
||||
|
||||
if (cache.has(key)) {
|
||||
onDataRef.current?.(cache.get(key) as T);
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
const run = fetcherRef.current;
|
||||
|
||||
dedupedFetch(key, run)
|
||||
.then((data) => {
|
||||
if (cancelled) return;
|
||||
setState({ status: "done", data });
|
||||
onDataRef.current?.(data);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (cancelled) return;
|
||||
setState({ status: "error", error: err });
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [key]);
|
||||
|
||||
return state;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { translateText } from "@/lib/api";
|
||||
import type { ApiError } from "@/lib/api";
|
||||
import type { Fetched, Language, TranslationResult } from "@/types/api";
|
||||
|
||||
export const TRANSLATION_DEBOUNCE_MS = 800;
|
||||
|
||||
const CACHE_LIMIT = 50;
|
||||
|
||||
const resultCache = new Map<string, Fetched<TranslationResult>>();
|
||||
|
||||
interface TranslationState {
|
||||
key: string;
|
||||
result?: Fetched<TranslationResult>;
|
||||
error?: unknown;
|
||||
}
|
||||
|
||||
export function useTranslation(
|
||||
text: string,
|
||||
from: Language,
|
||||
to: Language
|
||||
): {
|
||||
translatedText: string;
|
||||
result: Fetched<TranslationResult> | null;
|
||||
isLoading: boolean;
|
||||
error: ApiError | null;
|
||||
} {
|
||||
const [state, setState] = useState<TranslationState | null>(null);
|
||||
|
||||
const trimmed = text.trim();
|
||||
const key = trimmed ? `${from}:${to}:${trimmed}` : null;
|
||||
const initialKeyRef = useRef(key);
|
||||
const cached = key ? resultCache.get(key) : undefined;
|
||||
const settled = cached ?? (state?.key === key ? state.result : undefined);
|
||||
const stateError = state?.key === key ? state.error : undefined;
|
||||
|
||||
const isLoading = key !== null && !cached && !settled && !stateError;
|
||||
|
||||
useEffect(() => {
|
||||
if (!key || cached || stateError) return;
|
||||
|
||||
const controller = new AbortController();
|
||||
|
||||
const delayMs = key === initialKeyRef.current ? 0 : TRANSLATION_DEBOUNCE_MS;
|
||||
|
||||
const timeoutId = setTimeout(async () => {
|
||||
try {
|
||||
const data = await translateText(
|
||||
{ text: trimmed, from, to },
|
||||
controller.signal
|
||||
);
|
||||
|
||||
if (controller.signal.aborted) return;
|
||||
|
||||
if (data.translations.length > 0) {
|
||||
const enriched: Fetched<TranslationResult> = {
|
||||
...data,
|
||||
fetched_at: new Date().toISOString(),
|
||||
};
|
||||
resultCache.set(key, enriched);
|
||||
if (resultCache.size > CACHE_LIMIT) {
|
||||
resultCache.delete(resultCache.keys().next().value!);
|
||||
}
|
||||
setState({ key, result: enriched });
|
||||
} else {
|
||||
setState({ key });
|
||||
}
|
||||
} catch (err) {
|
||||
if (!controller.signal.aborted) {
|
||||
console.error("Translation failed:", err);
|
||||
setState({ key, error: err });
|
||||
}
|
||||
}
|
||||
}, delayMs);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeoutId);
|
||||
controller.abort();
|
||||
};
|
||||
}, [key, cached, stateError, trimmed, from, to]);
|
||||
|
||||
return {
|
||||
translatedText: settled?.translations.join("") ?? "",
|
||||
result: settled ?? null,
|
||||
isLoading,
|
||||
error: (stateError as ApiError) ?? null,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user