import RD_ApplicationT from "@/types/application"; import { ReactDeskConfig } from "@/types/main"; import { useCallback, useEffect, useState } from "react"; import { useProcesses } from "../process"; import { useConfig } from "../config"; import { useWindows } from "../windows"; import { BaseWindowArguments, NewWindowArguments, WindowData } from "../windows/types"; import React from "react"; import { TRANSITIONS_IN_MILLISECONDS } from "@/utils/constants"; export type SyscallContextState = { openAppByName: (name: string) => void; openApp: (appConfig: RD_ApplicationT) => void; }; const useSyscallContextState = (): SyscallContextState => { const { createProcess } = useProcesses(); const { createWindow, maximize } = useWindows(); // const { applications } = useConfig(); const openAppByName = useCallback((name: string) => {}, []); const openApp = useCallback( (appConfig: RD_ApplicationT) => { // Process this app config, and launch a window for it. // First create the process. const { name: appName, icon: appIcon, ...otherAppInfo } = appConfig; const newProcessPID = createProcess({ name: appName, icon: appIcon, }); if ( (!!otherAppInfo.showWindow || otherAppInfo.windowContent) && otherAppInfo.windowContent ) { let Component: React.ComponentType; if (React.isValidElement(otherAppInfo.windowContent)) { // Wrap ReactNode in a component Component = () => otherAppInfo.windowContent as React.ReactElement; } else { Component = otherAppInfo.windowContent as React.ComponentType; } let windowArguments: NewWindowArguments = { icon: appIcon, title: appName, Component, process: { pid: newProcessPID, }, }; const windowSettings: Partial = { maximized: false, }; if (otherAppInfo.windowConfig) { windowArguments = { ...windowArguments, ...otherAppInfo.windowConfig, }; } const newWindowID = createWindow(windowArguments); if (windowSettings.maximized) { requestAnimationFrame(() => { setTimeout(() => { maximize(newWindowID); }, TRANSITIONS_IN_MILLISECONDS.WINDOW + 150); }); } } }, [createProcess, createWindow, maximize] ); return { // Functions: openAppByName, openApp, }; }; export default useSyscallContextState;