aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-06-05 18:02:32 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-06-05 18:02:32 -0500
commita96cc204c4df5d695f23f952dc150b55be2d1f40 (patch)
tree77476959b40fba8d83a1587cd7596ae5a3e45733 /src
parenta67d368b3f6970b3c8f674606a1df9ad70f72062 (diff)
downloadslskd-a96cc204c4df5d695f23f952dc150b55be2d1f40.tar.gz
add localStorage
Diffstat (limited to 'src')
-rw-r--r--src/web/src/components/Users/Users.js33
-rw-r--r--src/web/src/config.js6
2 files changed, 31 insertions, 8 deletions
diff --git a/src/web/src/components/Users/Users.js b/src/web/src/components/Users/Users.js
index 99bd96a1..bd2c6bd2 100644
--- a/src/web/src/components/Users/Users.js
+++ b/src/web/src/components/Users/Users.js
@@ -1,17 +1,18 @@
-import React, { useState, useEffect, useLayoutEffect } from 'react';
-
+import React, { useState, useEffect, useLayoutEffect, useRef } from 'react';
import {
Item,
Segment,
Input
} from 'semantic-ui-react';
-import User from './User';
+import User from './User';
import { getInfo, getStatus, getEndpoint } from '../../lib/peers';
+import { activeUserInfoKey } from '../../config';
import './Users.css';
const Users = (props) => {
+ const inputRef = useRef();
const [user, setUser] = useState();
const [usernameInput, setUsernameInput] = useState();
const [selectedUsername, setSelectedUsername] = useState(undefined);
@@ -19,15 +20,22 @@ const Users = (props) => {
useEffect(() => {
document.addEventListener("keyup", keyUp, false);
- }, []);
+
+ const storedUsername = localStorage.getItem(activeUserInfoKey);
+
+ if (storedUsername !== undefined) {
+ setSelectedUsername(storedUsername);
+ setInputText(storedUsername);
+ }
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
useLayoutEffect(() => {
document.removeEventListener('keyup', keyUp, false);
- }, []);
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
const fetchUser = async () => {
- if (selectedUsername === undefined) {
+ if (!selectedUsername) {
return;
}
@@ -40,6 +48,7 @@ const Users = (props) => {
getEndpoint({ username: selectedUsername })
]);
+ localStorage.setItem(activeUserInfoKey, selectedUsername);
setUser({ ...info.data, ...status.data, ...endpoint.data });
setStatus({ fetching: false, error: undefined });
} catch (error) {
@@ -51,8 +60,19 @@ const Users = (props) => {
}, [selectedUsername]);
const clear = () => {
+ localStorage.removeItem(activeUserInfoKey);
setSelectedUsername(undefined);
setUser(undefined);
+ setInputText('');
+ setInputFocus();
+ }
+
+ const setInputText = (text) => {
+ inputRef.current.inputRef.current.value = text;
+ }
+
+ const setInputFocus = () => {
+ inputRef.current.focus();
}
const keyUp = (e) => e.key === 'Escape' ? clear() : '';
@@ -65,6 +85,7 @@ const Users = (props) => {
size='big'
loading={fetching}
disabled={fetching}
+ ref={inputRef}
className='users-input'
placeholder="Username"
onChange={(e) => setUsernameInput(e.target.value)}
diff --git a/src/web/src/config.js b/src/web/src/config.js
index f59fbcd8..4d6522e3 100644
--- a/src/web/src/config.js
+++ b/src/web/src/config.js
@@ -2,12 +2,14 @@ const baseUrl = process.env.NODE_ENV === 'production' ? 'api/v0' : 'http://local
const tokenKey = 'slskd-token';
const tokenPassthroughValue = 'n/a';
const activeChatKey = 'slskd-active-chat';
-const activeRoomKey = 'sslskd-active-room';
+const activeRoomKey = 'slskd-active-room';
+const activeUserInfoKey = 'slskd-active-user';
export {
baseUrl,
tokenKey,
tokenPassthroughValue,
activeChatKey,
- activeRoomKey
+ activeRoomKey,
+ activeUserInfoKey
};