25 lines
1.7 KiB
TypeScript
25 lines
1.7 KiB
TypeScript
|
|
import React,{useRef,useState} from 'react';
|
|||
|
|
import {View} from 'react-native';
|
|||
|
|
import {Button,Card,Field,Heading,Note,s} from '../ui';
|
|||
|
|
import {developmentServer} from '../core/server-address';
|
|||
|
|
|
|||
|
|
export function ServerConnection({address,onSave}:{address:string;onSave:(value:string)=>Promise<void>}) {
|
|||
|
|
const [editing,setEditing]=useState(false),[value,setValue]=useState(address),[busy,setBusy]=useState(false),[error,setError]=useState('');
|
|||
|
|
const lock=useRef(false);
|
|||
|
|
async function save(){
|
|||
|
|
if(lock.current)return;
|
|||
|
|
lock.current=true;
|
|||
|
|
setBusy(true);setError('');
|
|||
|
|
try{await onSave(developmentServer(value));setEditing(false);}catch(e){setError((e as Error).message);}finally{lock.current=false;setBusy(false);}
|
|||
|
|
}
|
|||
|
|
return <Card><Heading>开发服务</Heading><Note>{address}</Note>
|
|||
|
|
{!editing?<Button title="设置服务地址" secondary onPress={()=>{setValue(address);setError('');setEditing(true);}}/>:<>
|
|||
|
|
<Field label="服务地址(IP / 端口)" value={value} onChange={setValue} disabled={busy} maxLength={240}/>
|
|||
|
|
<Note>例如 192.168.28.20:8080;通过网页代理时可填 192.168.28.20:8088/dev-api。手机和电脑需在同一内网。</Note>
|
|||
|
|
<Note>保存前会检查连接。更换服务后需重新登录,未提交内容不会迁移;不会把当前登录凭证发到新服务。</Note>
|
|||
|
|
{!!error&&<Note>{error}</Note>}<View style={s.row}><Button title="检查连接并保存" busy={busy} onPress={()=>void save()}/><Button title="取消" secondary disabled={busy} onPress={()=>setEditing(false)}/></View>
|
|||
|
|
</>}
|
|||
|
|
{/127\.0\.0\.1|localhost|\[::1\]/.test(address)&&<Note>当前地址指向设备自身;手机访问电脑服务时,请填写电脑的内网 IP。</Note>}
|
|||
|
|
</Card>;
|
|||
|
|
}
|