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,114 +498,106 @@ const ChangeConnectionBlockModal = React.memo(
}, },
[blockId, blockData] [blockId, blockData]
); );
React.useEffect(() => {
const loadFromBackend = async () => {
let connList: Array<string>;
try { let createNew: boolean = true;
connList = await WshServer.ConnListCommand({ timeout: 2000 }); let showLocal: boolean = true;
} catch (e) { let showReconnect: boolean = true;
console.log("unable to load conn list from backend. using blank list: ", e); if (connSelected == "") {
} createNew = false;
if (!connList) { } else {
connList = []; showLocal = false;
} showReconnect = false;
let createNew: boolean = true; }
if (connSelected == "") { const filteredList: Array<string> = [];
createNew = false; for (const conn of connList) {
} if (conn === connSelected) {
const filteredList: Array<string> = []; createNew = false;
for (const conn of connList) { }
if (conn === connSelected) { if (conn.includes(connSelected)) {
createNew = false; filteredList.push(conn);
} }
if (conn.includes(connSelected)) { }
filteredList.push(conn); // priority handles special suggestions when necessary
} // for instance, when reconnecting
} const newConnectionSuggestion: SuggestionConnectionItem = {
// priority handles special suggestions when necessary status: "connected",
// for instance, when reconnecting icon: "plus",
const newConnectionSuggestion: SuggestionConnectionItem = { iconColor: "var(--conn-icon-color)",
status: "connected", label: `${connSelected} (New Connection)`,
icon: "plus", value: "",
iconColor: "var(--conn-icon-color)", onSelect: (_: string) => {
label: `${connSelected} (New Connection)`, changeConnection(connSelected);
value: "", globalStore.set(changeConnModalAtom, false);
onSelect: (_: string) => { },
changeConnection(connSelected); };
globalStore.set(changeConnModalAtom, false); const reconnectSuggestion: SuggestionConnectionItem = {
}, status: "connected",
}; icon: "arrow-right-arrow-left",
const reconnectSuggestion: SuggestionConnectionItem = { iconColor: "var(--conn-icon-color)",
status: "connected", label: `Reconnect to ${connStatus.connection}`,
icon: "arrow-right-arrow-left", value: "",
iconColor: "var(--conn-icon-color)", onSelect: async (_: string) => {
label: `Reconnect to ${connStatus.connection}`, const prtn = WshServer.ConnConnectCommand(connStatus.connection, { timeout: 60000 });
value: "", prtn.catch((e) => console.log("error reconnecting", connStatus.connection, e));
onSelect: async (_: string) => { },
const prtn = WshServer.ConnConnectCommand(connStatus.connection, { timeout: 60000 }); };
prtn.catch((e) => console.log("error reconnecting", connStatus.connection, e)); const priorityItems: Array<SuggestionConnectionItem> = [];
}, if (createNew) {
}; console.log("added to priority items");
const priorityItems: Array<SuggestionConnectionItem> = []; priorityItems.push(newConnectionSuggestion);
if (createNew) { }
console.log("added to priority items"); if (showReconnect && (connStatus.status == "disconnected" || connStatus.status == "error")) {
priorityItems.push(newConnectionSuggestion); priorityItems.push(reconnectSuggestion);
} }
if (connStatus.status == "disconnected" || connStatus.status == "error") { const prioritySuggestions: SuggestionConnectionScope = {
priorityItems.push(reconnectSuggestion); headerText: "",
} items: priorityItems,
const prioritySuggestions: SuggestionConnectionScope = { };
headerText: "", const localName = getUserName() + "@" + getHostName();
items: priorityItems, const localSuggestion: SuggestionConnectionScope = {
}; headerText: "Local",
const localName = getUserName() + "@" + getHostName(); items: [],
const localSuggestion: SuggestionConnectionScope = { };
headerText: "Local", if (showLocal) {
items: [ localSuggestion.items.push({
{ status: "connected",
status: "connected", icon: "laptop",
icon: "laptop", iconColor: "var(--grey-text-color)",
iconColor: "var(--grey-text-color)", value: "",
value: "", label: localName,
label: localName, // TODO: need to specify user name and host name
// TODO: need to specify user name and host name onSelect: (_: string) => {
onSelect: (_: string) => { changeConnection("");
changeConnection(""); globalStore.set(changeConnModalAtom, false);
globalStore.set(changeConnModalAtom, false); },
}, });
}, }
], const remoteItems = filteredList.map((connName) => {
}; const item: SuggestionConnectionItem = {
const remoteItems = filteredList.map((connName) => { status: "connected",
const item: SuggestionConnectionItem = { icon: "arrow-right-arrow-left",
status: "connected", iconColor: "var(--conn-icon-color)",
icon: "arrow-right-arrow-left", value: connName,
iconColor: "var(--conn-icon-color)", label: connName,
value: connName,
label: connName,
};
return item;
});
const remoteSuggestions: SuggestionConnectionScope = {
headerText: "Remote",
items: remoteItems,
};
let out: Array<SuggestionsType> = [];
if (prioritySuggestions.items.length > 0) {
out.push(prioritySuggestions);
}
if (localSuggestion.items.length > 0) {
out.push(localSuggestion);
}
if (remoteSuggestions.items.length > 0) {
out.push(remoteSuggestions);
}
setSuggestions(out);
}; };
loadFromBackend(); return item;
}, [connStatus, setSuggestions, connSelected, changeConnection]); });
const remoteSuggestions: SuggestionConnectionScope = {
headerText: "Remote",
items: remoteItems,
};
let suggestions: Array<SuggestionsType> = [];
if (prioritySuggestions.items.length > 0) {
suggestions.push(prioritySuggestions);
}
if (localSuggestion.items.length > 0) {
suggestions.push(localSuggestion);
}
if (remoteSuggestions.items.length > 0) {
suggestions.push(remoteSuggestions);
}
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;
} }