forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWindowAI.ts
More file actions
40 lines (32 loc) · 967 Bytes
/
useWindowAI.ts
File metadata and controls
40 lines (32 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useCallback, useEffect, useState } from "react";
let HAS_WINDOW_AI = false;
const supportsAI = async (): Promise<boolean> => {
if (
typeof window === "undefined" ||
!("ai" in window) ||
!("languageModel" in window.ai) ||
typeof window.ai.languageModel !== "object" ||
!("capabilities" in window.ai.languageModel) ||
typeof window.ai.languageModel.capabilities !== "function"
) {
return false;
}
try {
HAS_WINDOW_AI =
(await window.ai.languageModel.capabilities()).available === "readily";
return HAS_WINDOW_AI;
} catch {
return false;
}
};
export const useWindowAI = (): boolean => {
const [hasAI, setHasAI] = useState<boolean>(HAS_WINDOW_AI);
const checkAI = useCallback(async () => {
const hasWindowAi = await supportsAI();
if (hasWindowAi) setHasAI(true);
}, []);
useEffect(() => {
if (!hasAI) requestAnimationFrame(checkAI);
}, [checkAI, hasAI]);
return hasAI;
};