Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { useCallback, useEffect, useRef, useState } from 'react';
import { ActivityIndicator, Alert, Platform, Share, StyleSheet, View } from 'react-native';
import { WebView, type WebViewNavigation } from 'react-native-webview';
import * as Clipboard from 'expo-clipboard';
import { useTranslation } from 'react-i18next';
import { Text } from '@/components/AppText';
import { useTheme } from '@/theme';
import { fontWeight, lineHeight, typeScale } from '@/theme/tokens';
import { HtmlBrowserChrome } from './HtmlBrowserChrome';
import type { HtmlBrowserChromeProps } from './HtmlBrowserChrome.types';
import { normalizeBrowserAddress, allowWebsiteNavigation } from './browserAddress';
import { useHtmlBrowserViewport } from './useHtmlBrowserViewport';
import { browserViewportScrollScript, type BrowserInsets } from './htmlBrowserViewport';
export interface WebsiteVisit { url: string; snapshotClosed: Promise<void> }
/** 「已复制网址」提示停留时长,与文件预览 / 文件浏览的复制提示一致。 */
const COPY_NOTICE_DISMISS_MS = 2500;
/** Separate native lifetime and ephemeral cookie store from the file preview. */
export function HtmlWebsiteBrowser({ visit, insets, chrome, onReturn }: {
visit: WebsiteVisit;
insets: BrowserInsets;
chrome: Pick<HtmlBrowserChromeProps, 'top' | 'bottom' | 'left' | 'right' | 'standalone' | 'onClose'>;
onReturn(): void;
}) {
const { t } = useTranslation();
const { colors } = useTheme();
const webView = useRef<WebView>(null);
const [ready, setReady] = useState(false);
const [failed, setFailed] = useState(false);
const [navigation, setNavigation] = useState<WebViewNavigation | null>(null);
const [source, setSource] = useState({ uri: visit.url });
const [rendererEpoch, setRendererEpoch] = useState(0);
// WKWebView paints white until its first load ends; each renderer starts hidden over the themed page.
const [paintedEpoch, setPaintedEpoch] = useState<number | null>(null);
const rendererGone = useRef(false);
const pendingAddress = useRef<{ url: string; previous: string[] } | null>(null);
const address = navigation?.url ?? source.uri;
const live = useRef(true);
const { viewRef, measure, script, viewportStyle, obscuredContentInsets } = useHtmlBrowserViewport(insets);
const [notice, setNotice] = useState<string | null>(null);
const noticeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => () => { if (noticeTimer.current) clearTimeout(noticeTimer.current); }, []);
const copyAddress = useCallback(async () => {
await Clipboard.setStringAsync(address);
if (!live.current) return;
setNotice(t('files.preview.browserCopiedAddress'));
if (noticeTimer.current) clearTimeout(noticeTimer.current);
noticeTimer.current = setTimeout(() => setNotice(null), COPY_NOTICE_DISMISS_MS);
}, [address, t]);
useEffect(() => {
live.current = true;
void visit.snapshotClosed.then(() => { if (live.current) setReady(true); }, () => { if (live.current) setFailed(true); });
return () => { live.current = false; };
}, [visit.snapshotClosed]);
useEffect(() => { if (script && ready) webView.current?.injectJavaScript(script); }, [script, ready]);
const navigate = (input: string) => {
const url = normalizeBrowserAddress(input);
if (!url) { Alert.alert(t('files.preview.browserInvalidAddress')); return false; }
pendingAddress.current = { url, previous: [address, source.uri] };
setNavigation(null);
setFailed(false);
setSource({ uri: url });
if (rendererGone.current) { rendererGone.current = false; setRendererEpoch(value => value + 1); }
else if (url === address && ready) webView.current?.reload();
return true;
};
return <View style={[styles.fill, { backgroundColor: colors.surface }]}>
<View ref={viewRef} onLayout={measure} collapsable={false} style={[styles.fill, viewportStyle]}>
<View style={styles.viewport}>
{ready && <WebView
key={rendererEpoch}
ref={webView}
testID="filePreview.website"
source={source}
incognito
originWhitelist={['*']}
onShouldStartLoadWithRequest={request => allowWebsiteNavigation(request.url)}
setSupportMultipleWindows={false}
allowFileAccess={false}
mediaCapturePermissionGrantType="deny"
automaticallyAdjustContentInsets={false}
contentInsetAdjustmentBehavior="never"
obscuredContentInsets={obscuredContentInsets}
contentInset={obscuredContentInsets}
injectedJavaScriptBeforeContentLoaded={script}
injectedJavaScript={script}
onLoadStart={event => {
if (!live.current) return;
if (event.nativeEvent.url === pendingAddress.current?.url) pendingAddress.current = null;
setFailed(false);
}}
onLoadEnd={() => {
if (!live.current) return;
setPaintedEpoch(rendererEpoch);
if (script) webView.current?.injectJavaScript(script);
}}
onScroll={({ nativeEvent: { contentOffset } }) => {
if (live.current) webView.current?.injectJavaScript(browserViewportScrollScript(contentOffset.x, contentOffset.y));
}}
onNavigationStateChange={state => {
if (!live.current || !allowWebsiteNavigation(state.url)) return;
const pending = pendingAddress.current;
if (pending && state.url !== pending.url && pending.previous.includes(state.url)) return;
pendingAddress.current = null;
setNavigation(state);
// Both native platforms skip loading when this is already the WebView's URL.
setSource(old => old.uri === state.url ? old : { uri: state.url });
}}
onError={() => { if (live.current) setFailed(true); }}
onContentProcessDidTerminate={() => { if (live.current) { rendererGone.current = true; setFailed(true); } }}
onRenderProcessGone={() => { if (live.current) { rendererGone.current = true; setFailed(true); } }}
style={[styles.fill, { backgroundColor: colors.surface }, paintedEpoch !== rendererEpoch && styles.unpainted]}
/>}
{!ready && !failed && <View style={styles.center}><ActivityIndicator color={colors.textSecondary} /></View>}
{failed && <View pointerEvents="none" style={[styles.center, { backgroundColor: colors.surface }]}>
<Text style={[styles.failedText, { color: colors.errorText }]}>{t('files.preview.browserWebsiteFailed')}</Text>
</View>}
</View>
</View>
<HtmlBrowserChrome {...chrome}
title={address} path={address} address={address} website
source={false} busy={false} notice={notice} loading={!failed && (!ready || !navigation || navigation.loading)}
canGoBack canGoForward={navigation?.canGoForward === true}
onNavigate={navigate}
onBack={() => navigation?.canGoBack ? webView.current?.goBack() : onReturn()}
onForward={() => webView.current?.goForward()}
onReload={() => {
if (rendererGone.current) { rendererGone.current = false; setFailed(false); setRendererEpoch(value => value + 1); }
else webView.current?.reload();
}}
onToggleSource={onReturn}
onCopyPath={() => { void copyAddress().catch(() => {}); }}
onShare={() => { void Share.share(Platform.OS === 'ios' ? { url: address } : { message: address }).catch(() => {}); }}
onAddToTask={() => {}}
/>
</View>;
}
const styles = StyleSheet.create({
fill: { flex: 1 },
viewport: { flex: 1, overflow: 'hidden' },
unpainted: { opacity: 0 },
center: { ...StyleSheet.absoluteFill, alignItems: 'center', justifyContent: 'center' },
// 报错说明(成句的话):footnote 13/18 400;字色 errorText 在调用处按主题注入。
failedText: { fontSize: typeScale.footnote, lineHeight: lineHeight.caption, fontWeight: fontWeight.regular },
});