diff --git a/src/context/serversContext.ts b/src/context/serversContext.ts index 218c106..f4da10e 100644 --- a/src/context/serversContext.ts +++ b/src/context/serversContext.ts @@ -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 diff --git a/src/screens/ServerScreen.tsx b/src/screens/ServerScreen.tsx index 416c638..e11bf94 100644 --- a/src/screens/ServerScreen.tsx +++ b/src/screens/ServerScreen.tsx @@ -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({}) } @@ -151,16 +160,18 @@ const ServerScreen = (props: Props): JSX.Element => { } > - {Object.keys(servers).map(server => ( - - ))} + {Object.keys(servers) + .sort((a, b) => (servers[a].order ?? 0) - (servers[b].order ?? 0)) + .map(server => ( + + ))}