Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
refactor(library): move Shorts filtering into a tested module
spacesdrive22edefb93863
diff --git a/docs/architecture/frontend/FEATURES.md b/docs/architecture/frontend/FEATURES.mdindex 971cea2..f332722 100644--- a/docs/architecture/frontend/FEATURES.md+++ b/docs/architecture/frontend/FEATURES.md@@ -107,7 +107,8 @@ frontend/src/│ │ └── videoLibrary.js <- Pure channel merge, filter, sort and totals (videoLibrary.test.js)│ ├── analytics/shorts/│ │ ├── ShortsExplorer.jsx <- Page: YouTube Shorts (60 seconds or less)-│ │ └── ShortsFilters.jsx, ShortsGrid.jsx+│ │ ├── ShortsFilters.jsx, ShortsGrid.jsx+│ │ └── shortsLibrary.js <- Pure Shorts selection, filter, sort and description (shortsLibrary.test.js)│ └── analytics/reels/│ ├── ReelsExplorer.jsx <- Page: Instagram reels, posts and videos│ ├── ReelsMetrics.jsx, ReelsFilters.jsx, ReelsGrid.jsxdiff --git a/frontend/src/features/analytics/reels/ReelsExplorer.jsx b/frontend/src/features/analytics/reels/ReelsExplorer.jsxindex c40556b..ed046b3 100644--- a/frontend/src/features/analytics/reels/ReelsExplorer.jsx+++ b/frontend/src/features/analytics/reels/ReelsExplorer.jsx@@ -38,7 +38,12 @@ export default function ReelsExplorer() {);const paged = pageSlice(filtered, page, ITEMS_PER_PAGE);- const resetPage = setter => value => { setter(value); setPage(1); };+ function resetPage(setter) {+ return value => {+ setter(value);+ setPage(1);+ };+ }const filters = { search, accountFilter, typeFilter, tier, sortField };const handlers = {onSearchChange: resetPage(setSearch),diff --git a/frontend/src/features/analytics/shorts/ShortsExplorer.jsx b/frontend/src/features/analytics/shorts/ShortsExplorer.jsxindex 9bfe304..a5d0272 100644--- a/frontend/src/features/analytics/shorts/ShortsExplorer.jsx+++ b/frontend/src/features/analytics/shorts/ShortsExplorer.jsx@@ -7,15 +7,14 @@ import PageHeader from '@/components/PageHeader';import SurfaceBox from '@/components/SurfaceBox';import Pager from '@/components/Pager';import { EmptyState } from '@/components/StateViews';-import { VIEW_TIERS, filterByTier, matchesText, pageSlice } from '@/lib/library';+import { pageSlice } from '@/lib/library';import { useChannelVideos } from '../videos/useChannelVideos';-import { fmtNum } from '../../../utils/formatters';import { cn } from '@/lib/utils';import ShortsFilters from './ShortsFilters';import ShortsGrid from './ShortsGrid';+import { shortsOf, filterAndSortShorts, shortsDescription } from './shortsLibrary.js';const ITEMS_PER_PAGE = 24;-const SHORT_MAX_SECONDS = 60;const SORT_OPTIONS = [{ value: 'viewCount', label: 'Most viewed' },@@ -25,11 +24,6 @@ const SORT_OPTIONS = [{ value: 'publishedAt', label: 'Newest first' },];-function sortValue(item, field) {- if (field === 'publishedAt') return new Date(item.publishedAt).getTime() || 0;- return Number(item[field] ?? 0);-}-export default function ShortsExplorer() {const { videos, channels, loading, reload } = useChannelVideos();const [search, setSearch] = useState('');@@ -38,25 +32,26 @@ export default function ShortsExplorer() {const [sortField, setSortField] = useState('viewCount');const [page, setPage] = useState(1);- const shorts = useMemo(- () => videos.filter(v => v.durationSeconds > 0 && v.durationSeconds <= SHORT_MAX_SECONDS),- [videos],- );+ const shorts = useMemo(() => shortsOf(videos), [videos]);- const filtered = useMemo(() => {- let result = channelFilter === 'all' ? shorts : shorts.filter(v => v.channelId === channelFilter);- result = result.filter(v => matchesText([v.title], search));- result = filterByTier(result, VIEW_TIERS, tier, 'viewCount');- return [...result].sort((a, b) => sortValue(b, sortField) - sortValue(a, sortField));- }, [shorts, channelFilter, search, tier, sortField]);+ const filtered = useMemo(+ () => filterAndSortShorts(shorts, { channelFilter, search, tier, sortField }),+ [shorts, channelFilter, search, tier, sortField],+ );const paged = pageSlice(filtered, page, ITEMS_PER_PAGE);- const resetPage = setter => value => { setter(value); setPage(1); };++ function resetPage(setter) {+ return value => {+ setter(value);+ setPage(1);+ };+ }const header = (<PageHeadertitle="Shorts"- description={loading ? 'Loading Shorts from your channels' : `${fmtNum(shorts.length)} Shorts across ${channels.length} channel${channels.length === 1 ? '' : 's'}`}+ description={shortsDescription(loading, shorts.length, channels.length)}actions={<Button variant="outline" size="sm" onClick={reload} disabled={loading}><RefreshCw data-icon="inline-start" className={cn(loading && 'animate-spin')} />diff --git a/frontend/src/features/analytics/shorts/shortsLibrary.js b/frontend/src/features/analytics/shorts/shortsLibrary.jsnew file mode 100644index 0000000..65c9278--- /dev/null+++ b/frontend/src/features/analytics/shorts/shortsLibrary.js@@ -0,0 +1,25 @@+import { VIEW_TIERS, filterByTier, matchesText } from '../../../lib/library.js';+import { fmtNum } from '../../../utils/formatters.js';++const SHORT_MAX_SECONDS = 60;++export function shortsOf(videos) {+ return videos.filter(video => video.durationSeconds > 0 && video.durationSeconds <= SHORT_MAX_SECONDS);+}++function sortValue(item, field) {+ if (field === 'publishedAt') return new Date(item.publishedAt).getTime() || 0;+ return Number(item[field] ?? 0);+}++export function filterAndSortShorts(shorts, { channelFilter, search, tier, sortField }) {+ let result = channelFilter === 'all' ? shorts : shorts.filter(video => video.channelId === channelFilter);+ result = result.filter(video => matchesText([video.title], search));+ result = filterByTier(result, VIEW_TIERS, tier, 'viewCount');+ return [...result].sort((a, b) => sortValue(b, sortField) - sortValue(a, sortField));+}++export function shortsDescription(loading, shortCount, channelCount) {+ if (loading) return 'Loading Shorts from your channels';+ return `${fmtNum(shortCount)} Shorts across ${channelCount} channel${channelCount === 1 ? '' : 's'}`;+}diff --git a/frontend/src/features/analytics/shorts/shortsLibrary.test.js b/frontend/src/features/analytics/shorts/shortsLibrary.test.jsnew file mode 100644index 0000000..346e1cf--- /dev/null+++ b/frontend/src/features/analytics/shorts/shortsLibrary.test.js@@ -0,0 +1,38 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { shortsOf, filterAndSortShorts, shortsDescription } from './shortsLibrary.js';++const videos = [+ { videoId: 's1', channelId: 'c1', title: 'Quick pasta', durationSeconds: 45, viewCount: 900, likeCount: 5, publishedAt: '2026-02-01T00:00:00Z' },+ { videoId: 's2', channelId: 'c2', title: 'Fast fold', durationSeconds: 60, viewCount: 50000, likeCount: '30', publishedAt: '2026-04-01T00:00:00Z' },+ { videoId: 's3', channelId: 'c1', title: 'Pasta tips', durationSeconds: 12, publishedAt: 'not a date' },+ { videoId: 'l1', channelId: 'c1', title: 'Long pasta', durationSeconds: 61, viewCount: 1 },+ { videoId: 'z1', channelId: 'c1', title: 'Unknown length', durationSeconds: 0, viewCount: 1 },+];++const all = { channelFilter: 'all', search: '', tier: 'all', sortField: 'viewCount' };++test('shorts are videos of 1 to 60 seconds', () => {+ assert.deepEqual(shortsOf(videos).map(video => video.videoId), ['s1', 's2', 's3']);+});++test('shorts sort by the chosen field with missing values as 0', () => {+ const shorts = shortsOf(videos);+ assert.deepEqual(filterAndSortShorts(shorts, all).map(video => video.videoId), ['s2', 's1', 's3']);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, sortField: 'likeCount' }).map(video => video.videoId), ['s2', 's1', 's3']);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, sortField: 'publishedAt' }).map(video => video.videoId), ['s2', 's1', 's3']);+});++test('channel, search and view tier filters combine', () => {+ const shorts = shortsOf(videos);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, channelFilter: 'c1' }).map(video => video.videoId), ['s1', 's3']);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, search: 'PASTA' }).map(video => video.videoId), ['s1', 's3']);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, tier: 'medium' }).map(video => video.videoId), ['s2']);+ assert.deepEqual(filterAndSortShorts(shorts, { ...all, channelFilter: 'c2', search: 'pasta' }), []);+});++test('the description counts shorts and channels', () => {+ assert.equal(shortsDescription(true, 5, 2), 'Loading Shorts from your channels');+ assert.equal(shortsDescription(false, 1200, 1), '1.2K Shorts across 1 channel');+ assert.equal(shortsDescription(false, 0, 3), '0 Shorts across 3 channels');+});diff --git a/frontend/src/features/analytics/videos/VideoExplorer.jsx b/frontend/src/features/analytics/videos/VideoExplorer.jsxindex d39b5eb..9226cf6 100644--- a/frontend/src/features/analytics/videos/VideoExplorer.jsx+++ b/frontend/src/features/analytics/videos/VideoExplorer.jsx@@ -53,7 +53,12 @@ export default function VideoExplorer() {const paged = pageSlice(filtered, page, ITEMS_PER_PAGE);const stats = useMemo(() => videoStatsOf(filtered), [filtered]);- const resetPage = setter => value => { setter(value); setPage(1); };+ function resetPage(setter) {+ return value => {+ setter(value);+ setPage(1);+ };+ }const handleSort = field => setSort(prev => nextSort(prev, field));const clearFilters = () => { setSearch(''); setTier('all'); setChannelFilter('all'); setPage(1); };