CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | import React, { useMemo } from 'react';
import {
View,
Text,
ScrollView,
StyleSheet,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { type RouteProp } from '@react-navigation/native';
import { colors } from '../theme/colors';
import { fontSizes, fonts } from '../theme/typography';
import { useFileContent } from '../hooks/useRepo';
import { LoadingSpinner } from '../components/LoadingSpinner';
import { ErrorState } from '../components/ErrorState';
import { type MainStackParamList } from '../navigation/types';
type Props = {
route: RouteProp<MainStackParamList, 'FileViewer'>;
};
// Minimal keyword syntax highlighting via regex replacement.
// Returns an array of {text, color} segments for a single line.
type Segment = { text: string; color: string };
const KEYWORDS: Record<string, RegExp> = {
keyword: /\b(const|let|var|function|class|if|else|for|while|return|import|export|default|from|async|await|try|catch|throw|new|typeof|instanceof|void|null|undefined|true|false|extends|implements|interface|type|enum|namespace|module|declare|abstract|public|private|protected|static|readonly|override|def|fn|pub|use|mod|struct|impl|match|where|self|super|trait|yield|in|of|do|switch|case|break|continue|pass|and|or|not|is|as|with|lambda|del|global|nonlocal|assert|finally|raise|except|elif)\b/g,
string: /(["'`])(?:\\.|(?!\1)[^\\])*\1/g,
comment: /(\/\/[^\n]*|#[^\n]*|\/\*[\s\S]*?\*\/)/g,
number: /\b(\d+\.?\d*)\b/g,
};
function tokenizeLine(line: string, lang: string): Segment[] {
// Skip highlighting for binary or very long lines
if (line.length > 500) return [{ text: line, color: colors.text }];
// Collect all token ranges
const ranges: Array<{ start: number; end: number; color: string }> = [];
function addRanges(regex: RegExp, color: string) {
regex.lastIndex = 0;
let m: RegExpExecArray | null;
while ((m = regex.exec(line)) !== null) {
ranges.push({ start: m.index, end: m.index + m[0].length, color });
}
}
addRanges(new RegExp(KEYWORDS.comment.source, 'g'), colors.textMuted);
addRanges(new RegExp(KEYWORDS.string.source, 'g'), colors.green);
addRanges(new RegExp(KEYWORDS.keyword.source, 'g'), colors.accent);
addRanges(new RegExp(KEYWORDS.number.source, 'g'), colors.yellow);
if (ranges.length === 0) return [{ text: line, color: colors.text }];
// Sort by start, resolve overlaps
ranges.sort((a, b) => a.start - b.start);
const segments: Segment[] = [];
let cursor = 0;
for (const r of ranges) {
if (r.start < cursor) continue; // overlapping — skip
if (r.start > cursor) {
segments.push({ text: line.slice(cursor, r.start), color: colors.text });
}
segments.push({ text: line.slice(r.start, r.end), color: r.color });
cursor = r.end;
}
if (cursor < line.length) {
segments.push({ text: line.slice(cursor), color: colors.text });
}
return segments;
}
function getExtension(path: string): string {
const parts = path.split('.');
return parts.length > 1 ? parts[parts.length - 1].toLowerCase() : '';
}
const CODE_EXTS = new Set([
'ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs',
'py', 'rb', 'go', 'rs', 'c', 'cpp', 'cc', 'h', 'hpp',
'java', 'kt', 'swift', 'cs', 'php', 'sh', 'bash', 'zsh',
'toml', 'yaml', 'yml', 'json', 'md', 'sql', 'graphql',
'css', 'scss', 'sass', 'html', 'xml', 'svelte', 'vue',
]);
export function FileViewerScreen({ route }: Props) {
const { owner, repo, path, ref } = route.params;
const { file, loading, error } = useFileContent(owner, repo, path, ref);
const ext = getExtension(path);
const isCode = CODE_EXTS.has(ext);
const content = useMemo(() => {
if (!file) return '';
if (file.encoding === 'base64') {
try {
return atob(file.content.replace(/\n/g, ''));
} catch {
return file.content;
}
}
return file.content;
}, [file]);
const lines = useMemo(() => content.split('\n'), [content]);
if (loading) return <LoadingSpinner fullScreen />;
if (error || !file) return <ErrorState message={error || 'File not found'} />;
const isBinary = !isCode && file.size > 0;
return (
<SafeAreaView style={styles.safe} edges={['top', 'bottom']}>
{/* File info bar */}
<View style={styles.infoBar}>
<Text style={styles.fileName} numberOfLines={1}>{path.split('/').pop()}</Text>
<Text style={styles.fileMeta}>{formatSize(file.size)} · {lines.length} lines</Text>
</View>
{isBinary ? (
<View style={styles.binaryWrap}>
<Text style={styles.binaryText}>Binary file — preview not available</Text>
<Text style={styles.binarySize}>{formatSize(file.size)}</Text>
</View>
) : (
<ScrollView horizontal showsHorizontalScrollIndicator style={styles.hScroll}>
<ScrollView style={styles.vScroll}>
<View style={styles.codeWrap}>
{lines.map((line, idx) => {
const segments = isCode ? tokenizeLine(line, ext) : [{ text: line, color: colors.text }];
return (
<View key={idx} style={styles.codeLine}>
<Text style={styles.lineNo}>{idx + 1}</Text>
<Text style={styles.lineContent}>
{segments.map((seg, si) => (
<Text key={si} style={{ color: seg.color }}>
{seg.text}
</Text>
))}
</Text>
</View>
);
})}
</View>
</ScrollView>
</ScrollView>
)}
</SafeAreaView>
);
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
}
const styles = StyleSheet.create({
safe: {
flex: 1,
backgroundColor: colors.bg,
},
infoBar: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingHorizontal: 16,
paddingVertical: 10,
borderBottomWidth: 1,
borderBottomColor: colors.border,
backgroundColor: colors.bgSecondary,
},
fileName: {
color: colors.text,
fontSize: fontSizes.sm,
fontFamily: fonts.mono,
fontWeight: '600',
flex: 1,
marginRight: 12,
},
fileMeta: {
color: colors.textMuted,
fontSize: fontSizes.xs,
},
hScroll: {
flex: 1,
},
vScroll: {
flex: 1,
},
codeWrap: {
padding: 8,
paddingBottom: 40,
},
codeLine: {
flexDirection: 'row',
minHeight: 20,
},
lineNo: {
width: 36,
color: colors.textMuted,
fontSize: fontSizes.xs,
fontFamily: fonts.mono,
textAlign: 'right',
paddingRight: 12,
lineHeight: 20,
userSelect: 'none',
},
lineContent: {
fontSize: fontSizes.xs,
fontFamily: fonts.mono,
lineHeight: 20,
color: colors.text,
},
binaryWrap: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 32,
},
binaryText: {
color: colors.textMuted,
fontSize: fontSizes.base,
marginBottom: 8,
},
binarySize: {
color: colors.textMuted,
fontSize: fontSizes.sm,
},
});
|