Skip to content

Commit

Permalink
Order servers, trim server names
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Mar 28, 2024
1 parent 3b14cfb commit 0c6c7a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/context/serversContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type protocolMap } from '../minecraft/utils'
export interface Server {
version: keyof typeof protocolMap
address: string
order: number
}

export type Servers = Record<string, Server>
Expand Down
35 changes: 23 additions & 12 deletions src/screens/ServerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ const ServerScreen = (props: Props): JSX.Element => {
const editServer = (
serverName: string,
version: keyof typeof protocolMap,
address: string
address: string,
order?: number
): void => {
const edit = typeof editServerDialogOpen === 'string'
const lastOrder = Object.values(servers).reduce(
(max, e) => (e.order > max ? e.order : max),
0
)
const newOrder =
order ?? // the lengths we will go to avoid conditionals...
((edit || undefined) && servers[editServerDialogOpen as string].order) ??
lastOrder + 1
const newServers = { ...servers }
if (edit) delete newServers[editServerDialogOpen]
newServers[serverName] = { version, address }
newServers[serverName.trim()] = { version, address, order: newOrder }
setServers(newServers)
setPingResponses({})
}
Expand Down Expand Up @@ -151,16 +160,18 @@ const ServerScreen = (props: Props): JSX.Element => {
}
>
<View style={globalStyle.outerView}>
{Object.keys(servers).map(server => (
<ServerDisplay
key={server}
ping={pingResponses[servers[server].address]}
server={server}
darkMode={darkMode}
connectToServer={connectToServer}
openEditServerDialog={openEditServerDialog}
/>
))}
{Object.keys(servers)
.sort((a, b) => (servers[a].order ?? 0) - (servers[b].order ?? 0))
.map(server => (
<ServerDisplay
key={server}
ping={pingResponses[servers[server].address]}
server={server}
darkMode={darkMode}
connectToServer={connectToServer}
openEditServerDialog={openEditServerDialog}
/>
))}
</View>
</ScrollView>
</>
Expand Down

0 comments on commit 0c6c7a5

Please sign in to comment.