subtracks/app/screens/ServerView.tsx
austinried 081251061d
Library store refactor (#76)
* start of music store refactor

moving stuff into a state cache
better separate it from view logic

* added paginated list/album list

* reworked fetchAlbumList to remove ui state

refactored home screen to use new method
i broke playing songs somehow, JS thread goes into a loop

* don't reset parts manually, do it all at once

* fixed perf issue related to too many rerenders

rerenders were caused by strict equality check on object/array picks
switched artistInfo to new store
updated zustand and fixed deprecation warnings

* update typescript

and use workspace tsc version for vscode

* remove old artistInfo

* switched to new playlist w/songs

removed more unused stuff

* remove unused + (slightly) rework search

* refactor star

* use only original/large imges for covers/artist

fix view artist from context menu
add loading indicators to song list and artist views (show info we have right away)

* set starred/unstar assuming it works

and correct state on error

* reorg, remove old music slice files

* added back fix for song cover art

* sort artists by localCompare name

* update licenses

* fix now playing background grey bar

* update react-native-gesture-handler

for node-fetch security alert

* fix another gradient height grey bar issue

* update licenses again

* remove thumbnail cache

* rename to remove "Library" from methods

* Revert "remove thumbnail cache"

This reverts commit e0db4931f11bbf4cd8e73102d06505c6ae85f4a6.

* use ids for lists, pull state later

* Revert "use only original/large imges for covers/artist"

This reverts commit c9aea9065ce6ebe3c8b09c10dd74d4de153d76fd.

* deep equal ListItem props for now

this needs a bigger refactor

* use immer as middleware

* refactor api client to use string method

hoping to use this for requestKey/deduping next

* use thumbnails in list items

* Revert "refactor api client to use string method"

This reverts commit 234326135b7af96cb91b941e7ca515f45c632556.

* rename/cleanup

* store servers by id

* get rid of settings selectors

* renames for clarity

remove unused estimateContentLength setting

* remove trackplayer selectors

* fix migration for library filter settings

* fixed shuffle order reporting wrong track/queue

* removed the other selectors

* don't actually need es6/react for our state

* fix slow artist sort on star

localeCompare is too slow for large lists
2022-03-28 13:30:57 +09:00

291 lines
7.9 KiB
TypeScript

import Button from '@app/components/Button'
import GradientScrollView from '@app/components/GradientScrollView'
import { Server } from '@app/models/settings'
import { useStore, useStoreDeep } from '@app/state/store'
import colors from '@app/styles/colors'
import font from '@app/styles/font'
import toast from '@app/util/toast'
import { useNavigation } from '@react-navigation/native'
import md5 from 'md5'
import React, { useCallback, useState } from 'react'
import { StyleSheet, Text, TextInput, View, ViewStyle } from 'react-native'
import { v4 as uuidv4 } from 'uuid'
import SettingsSwitch from '@app/components/SettingsSwitch'
const PASSWORD_PLACEHOLDER = 'PASSWORD_PLACEHOLDER'
const ServerView: React.FC<{
id?: string
}> = ({ id }) => {
const navigation = useNavigation()
const activeServerId = useStore(store => store.settings.activeServerId)
const servers = useStoreDeep(store => store.settings.servers)
const addServer = useStore(store => store.addServer)
const updateServer = useStore(store => store.updateServer)
const removeServer = useStore(store => store.removeServer)
const server = id ? servers[id] : undefined
const pingServer = useStore(store => store.pingServer)
const [address, setAddress] = useState(server?.address || '')
const [username, setUsername] = useState(server?.username || '')
const [usePlainPassword, setUsePlainPassword] = useState(server?.usePlainPassword ?? false)
const [password, setPassword] = useState(
server?.usePlainPassword ? server.plainPassword || '' : server?.token ? PASSWORD_PLACEHOLDER : '',
)
const [testing, setTesting] = useState(false)
const [removing, setRemoving] = useState(false)
const [saving, setSaving] = useState(false)
const validate = useCallback(() => {
return !!address && !!username && !!password
}, [address, username, password])
const canRemove = useCallback(() => {
return id && Object.keys(servers).length > 1 && activeServerId !== id
}, [id, servers, activeServerId])
const exit = useCallback(() => {
if (navigation.canGoBack()) {
navigation.goBack()
} else {
navigation.navigate('main')
}
}, [navigation])
const createServer = useCallback<() => Server>(() => {
if (usePlainPassword) {
return {
id: server?.id || uuidv4(),
usePlainPassword,
plainPassword: password,
address,
username,
}
}
let token: string
let salt: string
if (server && !server.usePlainPassword && password === PASSWORD_PLACEHOLDER) {
salt = server.salt
token = server.token
} else {
salt = uuidv4()
token = md5(password + salt)
}
return {
id: server?.id || uuidv4(),
address,
username,
usePlainPassword,
salt,
token,
}
}, [usePlainPassword, server, address, username, password])
const save = useCallback(() => {
if (!validate()) {
return
}
setSaving(true)
const update = createServer()
const waitForSave = async () => {
try {
if (id) {
updateServer(update)
} else {
await addServer(update)
}
exit()
} catch (err) {
setSaving(false)
}
}
waitForSave()
}, [addServer, createServer, exit, id, updateServer, validate])
const remove = useCallback(() => {
if (!canRemove()) {
return
}
setRemoving(true)
const waitForRemove = async () => {
try {
await removeServer(id as string)
exit()
} catch (err) {
setRemoving(false)
}
}
waitForRemove()
}, [canRemove, exit, id, removeServer])
const togglePlainPassword = useCallback(
(value: boolean) => {
setUsePlainPassword(value)
if (value) {
if (server && server.usePlainPassword) {
setPassword(server.plainPassword)
} else if (server) {
setPassword('')
}
} else {
if (server && !server.usePlainPassword) {
setPassword(PASSWORD_PLACEHOLDER)
}
}
},
[server],
)
const test = useCallback(() => {
setTesting(true)
const potential = createServer()
const ping = async () => {
const res = await pingServer(potential)
if (res) {
toast(`Connection to ${potential.address} OK!`)
} else {
toast(`Connection to ${potential.address} failed, check settings or server`)
}
setTesting(false)
}
ping()
}, [createServer, pingServer])
const disableControls = useCallback(() => {
return !validate() || testing || removing || saving
}, [validate, testing, removing, saving])
const formatAddress = useCallback(() => {
let addressFormatted = address.trim()
if (addressFormatted.endsWith('/')) {
addressFormatted = addressFormatted.substr(0, addressFormatted.length - 1)
}
if (addressFormatted.length > 0 && !addressFormatted.includes(':/')) {
addressFormatted = `http://${addressFormatted}`
}
setAddress(addressFormatted)
}, [address])
const deleteStyle: ViewStyle = {
display: canRemove() ? 'flex' : 'none',
}
return (
<GradientScrollView style={styles.scroll} contentContainerStyle={styles.scrollContentContainer}>
<View style={styles.content}>
<Text style={styles.inputTitle}>Address</Text>
<TextInput
style={styles.input}
placeholderTextColor="grey"
selectionColor={colors.text.secondary}
textContentType="URL"
placeholder="http://demo.navidrome.org"
autoCorrect={false}
autoCapitalize="none"
value={address}
onChangeText={setAddress}
onBlur={formatAddress}
/>
<Text style={styles.inputTitle}>Username</Text>
<TextInput
style={styles.input}
placeholderTextColor="grey"
selectionColor={colors.text.secondary}
textContentType="username"
autoComplete="username"
importantForAutofill="yes"
autoCapitalize="none"
placeholder="demo"
value={username}
onChangeText={setUsername}
/>
<Text style={styles.inputTitle}>Password</Text>
<TextInput
style={styles.input}
placeholderTextColor="grey"
selectionColor={colors.text.secondary}
textContentType="password"
autoComplete="password"
autoCapitalize="none"
importantForAutofill="yes"
secureTextEntry={true}
placeholder="demo"
value={password}
onChangeText={setPassword}
/>
<SettingsSwitch
title="Force plain text password"
subtitle={
usePlainPassword
? 'Send password in plain text (legacy, make sure your connection is secure!)'
: 'Send password as token + salt'
}
value={usePlainPassword}
setValue={togglePlainPassword}
/>
<Button
disabled={disableControls()}
style={styles.button}
title="Test Connection"
buttonStyle="hollow"
onPress={test}
/>
<Button
disabled={disableControls()}
style={[styles.button, styles.delete, deleteStyle]}
title="Delete"
onPress={remove}
/>
<Button disabled={disableControls()} style={styles.button} title="Save" onPress={save} />
</View>
</GradientScrollView>
)
}
const styles = StyleSheet.create({
scroll: {
flex: 1,
},
scrollContentContainer: {
// paddingTop: StatusBar.currentHeight,
},
content: {
paddingHorizontal: 20,
},
inputTitle: {
fontFamily: font.semiBold,
fontSize: 16,
color: colors.text.primary,
marginTop: 10,
},
input: {
borderBottomWidth: 1.5,
borderColor: colors.text.primary,
fontFamily: font.regular,
fontSize: 16,
color: colors.text.primary,
marginBottom: 26,
},
button: {
marginTop: 16,
},
delete: {
backgroundColor: 'red',
},
})
export default ServerView