<commit_before>// @flow

import * as React from 'react'
import {StyleSheet, ScrollView} from 'react-native'
import {reportNetworkProblem} from '@frogpond/analytics'
import {TabBarIcon} from '@frogpond/navigation-tabs'
import * as defaultData from '../../../../docs/webcams.json'
import {Column} from '@frogpond/layout'
import {partitionByIndex} from '../../../lib/partition-by-index'
import type {Webcam} from './types'
import {StreamThumbnail} from './thumbnail'
import {API} from '@frogpond/api'
import {Viewport} from '@frogpond/viewport'

const webcamsUrl = API('/webcams')

type Props = {}

type State = {
	webcams: Array<Webcam>,
	loading: boolean,
}

export class WebcamsView extends React.PureComponent<Props, State> {
	static navigationOptions = {
		tabBarLabel: 'Webcams',
		tabBarIcon: TabBarIcon('videocam'),
	}

	state = {
		webcams: defaultData.data,
		loading: false,
	}

	componentDidMount() {
		this.fetchData()
	}

	fetchData = async () => {
		this.setState(() => ({loading: true}))

		let {data: webcams} = await fetchJson(webcamsUrl).catch(err => {
			reportNetworkProblem(err)
			return defaultData
		})

		if (process.env.NODE_ENV === 'development') {
			webcams = defaultData.data
		}

		this.setState(() => ({webcams, loading: false}))
	}

	render() {
		const columns = partitionByIndex(this.state.webcams)

		return (
			<Viewport
				render={({width}) => (
					<ScrollView contentContainerStyle={styles.container}>
						{columns.map((contents, i) => (
							<Column key={i} style={styles.column}>
								{contents.map(webcam => (
									<StreamThumbnail
										key={webcam.name}
										viewportWidth={width}
										webcam={webcam}
									/>
								))}
							</Column>
						))}
					</ScrollView>
				)}
			/>
		)
	}
}

const styles = StyleSheet.create({
	container: {
		padding: 5,<commit_msg>adapt Webcams for caching api<commit_after>// @flow

import * as React from 'react'
import {StyleSheet, ScrollView} from 'react-native'
import {TabBarIcon} from '@frogpond/navigation-tabs'
import {Column} from '@frogpond/layout'
import {partitionByIndex} from '../../../lib/partition-by-index'
import type {Webcam} from './types'
import {StreamThumbnail} from './thumbnail'
import {API} from '@frogpond/api'
import {fetchCachedApi, type CacheResult} from '@frogpond/cache'
import {Viewport} from '@frogpond/viewport'

type WebcamsCache = CacheResult<?Array<Webcam>>
const getBundledData = () =>
	Promise.resolve(require('../../../../docs/webcams.json'))
const fetchWebcams = (forReload?: boolean): WebcamsCache =>
	fetchCachedApi(API('/webcams'), {getBundledData, forReload})

type Props = {}

type State = {
	webcams: Array<Webcam>,
	loading: boolean,
}

export class WebcamsView extends React.PureComponent<Props, State> {
	static navigationOptions = {
		tabBarLabel: 'Webcams',
		tabBarIcon: TabBarIcon('videocam'),
	}

	state = {
		webcams: [],
		loading: false,
	}

	componentDidMount() {
		this.fetchData()
	}

	fetchData = async () => {
		this.setState(() => ({loading: true}))

		let {value} = await fetchWebcams()

		this.setState(() => ({webcams: value || [], loading: false}))
	}

	render() {
		const columns = partitionByIndex(this.state.webcams)

		return (
			<Viewport
				render={({width}) => (
					<ScrollView contentContainerStyle={styles.container}>
						{columns.map((contents, i) => (
							<Column key={i} style={styles.column}>
								{contents.map(webcam => (
									<StreamThumbnail
										key={webcam.name}
										viewportWidth={width}
										webcam={webcam}
									/>
								))}
							</Column>
						))}
					</ScrollView>
				)}
			/>
		)
	}
}

const styles = StyleSheet.create({
	container: {
		padding: 5,
{'fraction_non_alphanumeric': 0.16080129940443963, 'fraction_numerical': 0.0005414185165132648, 'mean_word_length': 3.473365617433414, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4877321', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1231, 'n_words': 260}