-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
301 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { asyncThunkCreator, buildCreateSlice } from "@reduxjs/toolkit"; | ||
|
||
type AppId = string; | ||
export type AppInfo = { | ||
id: AppId; | ||
name: string; | ||
icon: string; | ||
exePath: string; | ||
hidden?: boolean; | ||
}; | ||
type State = { | ||
status: "idle" | "loading" | "failed"; | ||
info: Record<AppId, AppInfo>; | ||
}; | ||
|
||
const initialState: State = { | ||
status: "idle", | ||
info: {}, | ||
}; | ||
|
||
export const appSlice = buildCreateSlice({ | ||
creators: { asyncThunk: asyncThunkCreator }, | ||
})({ | ||
name: "app", | ||
initialState, | ||
reducers: (create) => ({ | ||
read: create.asyncThunk( | ||
async () => { | ||
const { ipcRenderer } = require("electron"); | ||
const apps: AppInfo[] = await ipcRenderer.invoke("read-apps"); | ||
return apps; | ||
}, | ||
{ | ||
pending: (state) => { | ||
state.status = "loading"; | ||
}, | ||
fulfilled: (state, action) => { | ||
state.status = "idle"; | ||
state.info = action.payload | ||
.sort((a, b) => (a.id < b.id ? -1 : 1)) | ||
.reduce((s, app) => { | ||
return { | ||
...s, | ||
[app.id]: app, | ||
}; | ||
}, state.info); | ||
}, | ||
rejected: (state) => { | ||
state.status = "failed"; | ||
}, | ||
}, | ||
), | ||
addTemp: create.reducer<AppInfo>((state, { payload }) => { | ||
if (payload) { | ||
state.info[payload.id] = payload; // TODO: Remove it after session closed | ||
state.info[payload.id]!.hidden = true; | ||
} | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as rr from "react-redux"; | ||
import { combineSlices, configureStore } from "@reduxjs/toolkit"; | ||
import { appSlice } from "./app"; | ||
import { sessionSlice } from "./session"; | ||
|
||
const reducer = combineSlices(appSlice, sessionSlice); | ||
export const store = configureStore({ reducer }); | ||
|
||
export type Dispatch = (typeof store)["dispatch"]; | ||
|
||
export const useDispatch = | ||
rr.useDispatch.withTypes<(typeof store)["dispatch"]>(); | ||
export const useSelector = | ||
rr.useSelector.withTypes<ReturnType<typeof reducer>>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { asyncThunkCreator, buildCreateSlice } from "@reduxjs/toolkit"; | ||
|
||
type SessionId = string; | ||
type PageId = string; | ||
export type PageInfo = { | ||
description: string; | ||
devtoolsFrontendUrl: string; | ||
id: PageId; | ||
title: string; | ||
type: "node" | "page" | "webview"; | ||
url: string; | ||
}; | ||
type SessionInfo = { | ||
appId: string; | ||
pages: Record<string, PageInfo>; | ||
log: string; | ||
nodePort: number; | ||
windowPort: number; | ||
}; | ||
type State = Record<SessionId, SessionInfo>; | ||
|
||
const initialState: State = {}; | ||
|
||
export const sessionSlice = buildCreateSlice({ | ||
creators: { asyncThunk: asyncThunkCreator }, | ||
})({ | ||
name: "session", | ||
initialState, | ||
reducers: (create) => ({ | ||
add: create.reducer<{ | ||
sessionId: SessionId; | ||
appId: string; | ||
nodePort: number; | ||
windowPort: number; | ||
}>((state, { payload }) => { | ||
state[payload.sessionId] = { | ||
appId: payload.appId, | ||
nodePort: payload.nodePort, | ||
windowPort: payload.windowPort, | ||
pages: {}, | ||
log: "", | ||
}; | ||
}), | ||
remove: create.reducer<SessionId>((state, { payload }) => { | ||
delete state[payload]; | ||
}), | ||
updatePages: create.asyncThunk( | ||
async ({ ports, sessionId }: { ports: number[]; sessionId: string }) => { | ||
const { ipcRenderer } = require("electron"); | ||
const pages: PageInfo[] = await ipcRenderer.invoke( | ||
"fetch-pages", | ||
ports, | ||
); | ||
return { sessionId, pages }; | ||
}, | ||
{ | ||
fulfilled: (state, { payload: { sessionId, pages } }) => { | ||
state[sessionId]!.pages = {}; | ||
pages | ||
.sort((a, b) => (a.id < b.id ? -1 : 1)) | ||
.forEach((page) => { | ||
state[sessionId]!.pages[page.id] = page; | ||
}); | ||
}, | ||
}, | ||
), | ||
updateLog: create.reducer<{ sessionId: string; text: string }>( | ||
(state, { payload }) => { | ||
state[payload.sessionId]!.log += payload.text; | ||
}, | ||
), | ||
}), | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.