updates for suggestions

This commit is contained in:
sawka 2024-09-05 22:15:14 -07:00
parent a22b1cb9f5
commit 975b77e728

View File

@ -453,7 +453,23 @@ const ChangeConnectionBlockModal = React.memo(
const connection = blockData?.meta?.connection; const connection = blockData?.meta?.connection;
const connStatusAtom = getConnStatusAtom(connection); const connStatusAtom = getConnStatusAtom(connection);
const connStatus = jotai.useAtomValue(connStatusAtom); const connStatus = jotai.useAtomValue(connStatusAtom);
const [suggestions, setSuggestions] = React.useState<SuggestionsType[]>([]); const [connList, setConnList] = React.useState<Array<string>>([]);
React.useEffect(() => {
if (!changeConnModalOpen) {
setConnList([]);
return;
}
const prtn = WshServer.ConnListCommand({ timeout: 2000 });
prtn.then((connList) => {
setConnList(connList ?? []);
}).catch((e) => console.log("unable to load conn list from backend. using blank list: ", e));
}, [changeConnModalOpen]);
React.useEffect(() => {
console.log("connSelected is: ", connSelected);
}, [connSelected]);
const changeConnection = React.useCallback( const changeConnection = React.useCallback(
async (connName: string) => { async (connName: string) => {
if (connName == "") { if (connName == "") {
@ -482,21 +498,15 @@ const ChangeConnectionBlockModal = React.memo(
}, },
[blockId, blockData] [blockId, blockData]
); );
React.useEffect(() => {
const loadFromBackend = async () => {
let connList: Array<string>;
try {
connList = await WshServer.ConnListCommand({ timeout: 2000 });
} catch (e) {
console.log("unable to load conn list from backend. using blank list: ", e);
}
if (!connList) {
connList = [];
}
let createNew: boolean = true; let createNew: boolean = true;
let showLocal: boolean = true;
let showReconnect: boolean = true;
if (connSelected == "") { if (connSelected == "") {
createNew = false; createNew = false;
} else {
showLocal = false;
showReconnect = false;
} }
const filteredList: Array<string> = []; const filteredList: Array<string> = [];
for (const conn of connList) { for (const conn of connList) {
@ -536,7 +546,7 @@ const ChangeConnectionBlockModal = React.memo(
console.log("added to priority items"); console.log("added to priority items");
priorityItems.push(newConnectionSuggestion); priorityItems.push(newConnectionSuggestion);
} }
if (connStatus.status == "disconnected" || connStatus.status == "error") { if (showReconnect && (connStatus.status == "disconnected" || connStatus.status == "error")) {
priorityItems.push(reconnectSuggestion); priorityItems.push(reconnectSuggestion);
} }
const prioritySuggestions: SuggestionConnectionScope = { const prioritySuggestions: SuggestionConnectionScope = {
@ -546,8 +556,10 @@ const ChangeConnectionBlockModal = React.memo(
const localName = getUserName() + "@" + getHostName(); const localName = getUserName() + "@" + getHostName();
const localSuggestion: SuggestionConnectionScope = { const localSuggestion: SuggestionConnectionScope = {
headerText: "Local", headerText: "Local",
items: [ items: [],
{ };
if (showLocal) {
localSuggestion.items.push({
status: "connected", status: "connected",
icon: "laptop", icon: "laptop",
iconColor: "var(--grey-text-color)", iconColor: "var(--grey-text-color)",
@ -558,9 +570,8 @@ const ChangeConnectionBlockModal = React.memo(
changeConnection(""); changeConnection("");
globalStore.set(changeConnModalAtom, false); globalStore.set(changeConnModalAtom, false);
}, },
}, });
], }
};
const remoteItems = filteredList.map((connName) => { const remoteItems = filteredList.map((connName) => {
const item: SuggestionConnectionItem = { const item: SuggestionConnectionItem = {
status: "connected", status: "connected",
@ -576,20 +587,17 @@ const ChangeConnectionBlockModal = React.memo(
items: remoteItems, items: remoteItems,
}; };
let out: Array<SuggestionsType> = []; let suggestions: Array<SuggestionsType> = [];
if (prioritySuggestions.items.length > 0) { if (prioritySuggestions.items.length > 0) {
out.push(prioritySuggestions); suggestions.push(prioritySuggestions);
} }
if (localSuggestion.items.length > 0) { if (localSuggestion.items.length > 0) {
out.push(localSuggestion); suggestions.push(localSuggestion);
} }
if (remoteSuggestions.items.length > 0) { if (remoteSuggestions.items.length > 0) {
out.push(remoteSuggestions); suggestions.push(remoteSuggestions);
} }
setSuggestions(out);
};
loadFromBackend();
}, [connStatus, setSuggestions, connSelected, changeConnection]);
const handleTypeAheadKeyDown = React.useCallback( const handleTypeAheadKeyDown = React.useCallback(
(waveEvent: WaveKeyboardEvent): boolean => { (waveEvent: WaveKeyboardEvent): boolean => {
if (keyutil.checkKeyPressed(waveEvent, "Enter")) { if (keyutil.checkKeyPressed(waveEvent, "Enter")) {
@ -607,9 +615,7 @@ const ChangeConnectionBlockModal = React.memo(
}, },
[changeConnModalAtom, viewModel, blockId, connSelected] [changeConnModalAtom, viewModel, blockId, connSelected]
); );
React.useEffect(() => {
console.log("connSelected is: ", connSelected);
}, [connSelected]);
if (!changeConnModalOpen) { if (!changeConnModalOpen) {
return null; return null;
} }