feat: local caching for dev

This commit is contained in:
Cory Dransfeldt 2024-11-16 17:02:32 -08:00
parent c70fc72952
commit 522d8ca48a
No known key found for this signature in database
21 changed files with 375 additions and 326 deletions

View file

@ -7,6 +7,12 @@ export default defineConfig({
adapter: cloudflare(),
integrations: [react()],
vite: {
build: {
sourcemap: false,
},
optimizeDeps: {
include: ["@tabler/icons-react"],
},
resolve: {
alias: {
"@cdransf": "node_modules/@cdransf",

View file

@ -49,7 +49,7 @@ const pagination = {
</time>
</div>
<h3>
<a href={post.slug}>{post.title}</a>
<a href={post.url}>{post.title}</a>
</h3>
<p set:html={md(post.description)}></p>
</article>

View file

@ -1,24 +1,24 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedActivity = null;
export async function fetchActivity() {
try {
const { data, error } = await supabase
.from('optimized_all_activity')
.select('feed');
if (import.meta.env.MODE === "development" && cachedActivity)
return cachedActivity;
if (error) {
console.error('Error fetching activity data:', error);
return [];
}
const { data, error } = await supabase
.from("optimized_all_activity")
.select("feed");
const [{ feed } = {}] = data || [];
return feed?.filter((item) => item.feed !== null) || [];
} catch (error) {
console.error('Unexpected error fetching activity data:', error);
return [];
}
}
if (error) return [];
const [{ feed } = {}] = data || [];
const filteredFeed = feed?.filter((item) => item.feed !== null) || [];
if (import.meta.env.MODE === "development") cachedActivity = filteredFeed;
return filteredFeed;
};

View file

@ -5,39 +5,34 @@ const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedAlbumReleases = null;
export async function fetchAlbumReleases() {
try {
const today = DateTime.utc().startOf('day').toSeconds();
if (import.meta.env.MODE === 'development' && cachedAlbumReleases) return cachedAlbumReleases;
const { data, error } = await supabase
.from('optimized_album_releases')
.select('*');
const today = DateTime.utc().startOf('day').toSeconds();
if (error) {
console.error('Error fetching album releases:', error);
return { all: [], upcoming: [] };
}
const { data, error } = await supabase
.from('optimized_album_releases')
.select('*');
const all = data
.map((album) => {
const releaseDate = DateTime.fromSeconds(album.release_timestamp)
.toUTC()
.startOf('day');
if (error) return { all: [], upcoming: [] };
return {
...album,
description: album.artist.description,
date: releaseDate.toLocaleString(DateTime.DATE_FULL),
timestamp: releaseDate.toSeconds(),
};
})
.sort((a, b) => a.timestamp - b.timestamp);
const all = data
.map((album) => {
const releaseDate = DateTime.fromSeconds(album.release_timestamp).toUTC().startOf('day');
return {
...album,
description: album.artist.description,
date: releaseDate.toLocaleString(DateTime.DATE_FULL),
timestamp: releaseDate.toSeconds(),
};
})
.sort((a, b) => a.timestamp - b.timestamp);
const upcoming = all.filter((album) => album.release_timestamp > today);
const upcoming = all.filter((album) => album.release_timestamp > today);
return { all, upcoming };
} catch (error) {
console.error('Unexpected error processing album releases:', error);
return { all: [], upcoming: [] };
}
}
if (import.meta.env.MODE === 'development') cachedAlbumReleases = { all, upcoming };
return { all, upcoming };
};

View file

@ -1,38 +1,40 @@
import { createClient } from '@supabase/supabase-js';
import { parseCountryField } from '@utils/helpers.js';
import { createClient } from "@supabase/supabase-js";
import { parseCountryField } from "@utils/helpers.js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
export async function fetchArtists(){
let cachedArtists = null;
export async function fetchArtists() {
if (import.meta.env.MODE === "development" && cachedArtists)
return cachedArtists;
const PAGE_SIZE = 1000;
let artists = [];
let rangeStart = 0;
while (true) {
const { data, error } = await supabase
.from('optimized_artists')
.select('*')
.from("optimized_artists")
.select("*")
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching artists:', error);
break;
}
if (error) break;
// Process and concatenate artists data
artists = artists.concat(
data.map((artist) => ({
...artist,
country: parseCountryField(artist['country']),
country: parseCountryField(artist["country"]),
}))
);
// Break if no more data
if (data.length < PAGE_SIZE) break;
rangeStart += PAGE_SIZE;
}
if (import.meta.env.MODE === "development") cachedArtists = artists;
return artists;
};
};

View file

@ -1,22 +1,28 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedBlogroll = null;
export async function fetchBlogroll() {
if (import.meta.env.MODE === "development" && cachedBlogroll)
return cachedBlogroll;
const { data, error } = await supabase
.from('authors')
.select('*')
.eq('blogroll', true)
.order('name', { ascending: true });
.from("authors")
.select("*")
.eq("blogroll", true)
.order("name", { ascending: true });
if (error) {
console.error('Error fetching authors for the blogroll:', error);
return [];
}
if (error) return [];
return data.sort((a, b) =>
const sortedData = data.sort((a, b) =>
a.name.toLowerCase().localeCompare(b.name.toLowerCase())
);
};
if (import.meta.env.MODE === "development") cachedBlogroll = sortedData;
return sortedData;
};

View file

@ -1,25 +1,26 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedBooks = null;
export async function fetchBooks() {
if (import.meta.env.MODE === "development" && cachedBooks) return cachedBooks;
const PAGE_SIZE = 1000;
let books = [];
let rangeStart = 0;
while (true) {
const { data, error } = await supabase
.from('optimized_books')
.select('*')
.order('date_finished', { ascending: false })
.from("optimized_books")
.select("*")
.order("date_finished", { ascending: false })
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching books:', error);
break;
}
if (error) break;
books = books.concat(data);
if (data.length < PAGE_SIZE) break;
@ -39,12 +40,17 @@ export async function fetchBooks() {
const sortedByYear = Object.values(years).filter((year) => year.value > 2017);
const currentYear = new Date().getFullYear();
const booksForCurrentYear =
sortedByYear.find((yearGroup) => yearGroup.value === currentYear)?.data || [];
sortedByYear.find((yearGroup) => yearGroup.value === currentYear)?.data ||
[];
return {
const result = {
all: books,
years: sortedByYear,
currentYear: booksForCurrentYear,
feed: books.filter((book) => book.feed),
};
};
if (import.meta.env.MODE === "development") cachedBooks = result;
return result;
};

View file

@ -1,32 +1,38 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedConcerts = null;
export async function fetchConcertsData() {
if (import.meta.env.MODE === "development" && cachedConcerts)
return cachedConcerts;
const PAGE_SIZE = 1000;
let concerts = [];
let rangeStart = 0;
while (true) {
const { data, error } = await supabase
.from('optimized_concerts')
.select('*')
.from("optimized_concerts")
.select("*")
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching concerts:', error);
break;
}
if (error) break;
concerts = concerts.concat(data);
if (data.length < PAGE_SIZE) break;
rangeStart += PAGE_SIZE;
}
return concerts.map((concert) => ({
const result = concerts.map((concert) => ({
...concert,
artist: concert.artist || { name: concert.artist_name_string, url: null },
}));
};
if (import.meta.env.MODE === "development") cachedConcerts = result;
return result;
};

View file

@ -1,18 +1,19 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
export async function fetchGenres() {
const { data, error } = await supabase
.from('optimized_genres')
.select('*');
let cachedGenres = null;
if (error) {
console.error('Error fetching genres with artists:', error);
return [];
}
export async function fetchGenres() {
if (import.meta.env.MODE === "development" && cachedGenres)
return cachedGenres;
const { data, error } = await supabase.from("optimized_genres").select("*");
if (error) return [];
if (import.meta.env.MODE === "development") cachedGenres = data;
return data;
};
};

View file

@ -1,19 +1,23 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedGlobals = null;
export async function fetchGlobals() {
const isDev = import.meta.env.MODE === "development";
if (isDev && cachedGlobals) return cachedGlobals;
const { data, error } = await supabase
.from('optimized_globals')
.select('*')
.from("optimized_globals")
.select("*")
.single();
if (error) {
console.error('Error fetching globals:', error);
return {};
}
if (error) return {};
if (isDev) cachedGlobals = data;
return data;
}
};

View file

@ -1,31 +1,33 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedLinks = null;
export async function fetchLinks() {
if (import.meta.env.MODE === "development" && cachedLinks) return cachedLinks;
const PAGE_SIZE = 1000;
let links = [];
let page = 0;
let fetchMore = true;
while (fetchMore) {
const { data, error } = await supabase
.from('optimized_links')
.select('*')
.from("optimized_links")
.select("*")
.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE - 1);
if (error) {
console.error('Error fetching links:', error);
return links;
}
if (error) return links;
if (data.length < PAGE_SIZE) fetchMore = false;
links = links.concat(data);
page++;
}
if (import.meta.env.MODE === "development") cachedLinks = links;
return links;
};
};

View file

@ -1,25 +1,27 @@
import { createClient } from '@supabase/supabase-js';
import { DateTime } from 'luxon';
import { createClient } from "@supabase/supabase-js";
import { DateTime } from "luxon";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedMovies = null;
export async function fetchMovies() {
if (import.meta.env.MODE === "development" && cachedMovies)
return cachedMovies;
const PAGE_SIZE = 1000;
let movies = [];
let rangeStart = 0;
while (true) {
const { data, error } = await supabase
.from('optimized_movies')
.select('*')
.from("optimized_movies")
.select("*")
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching movies:', error);
break;
}
if (error) break;
movies = movies.concat(data);
if (data.length < PAGE_SIZE) break;
@ -27,16 +29,22 @@ export async function fetchMovies() {
}
const year = DateTime.now().year;
const favoriteMovies = movies.filter(movie => movie.favorite);
const favoriteMovies = movies.filter((movie) => movie.favorite);
const recentlyWatchedMovies = movies.filter(
movie => movie.last_watched && year - DateTime.fromISO(movie.last_watched).year <= 3
(movie) =>
movie.last_watched &&
year - DateTime.fromISO(movie.last_watched).year <= 3
);
return {
const result = {
movies,
watchHistory: movies.filter(movie => movie.last_watched),
watchHistory: movies.filter((movie) => movie.last_watched),
recentlyWatched: recentlyWatchedMovies,
favorites: favoriteMovies.sort((a, b) => a.title.localeCompare(b.title)),
feed: movies.filter(movie => movie.feed),
feed: movies.filter((movie) => movie.feed),
};
};
if (import.meta.env.MODE === "development") cachedMovies = result;
return result;
};

View file

@ -1,11 +1,17 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedMusicData = null;
export async function fetchMusicData() {
if (import.meta.env.MODE === "development" && cachedMusicData)
return cachedMusicData;
const PAGE_SIZE = 1000;
const fetchDataFromView = async (viewName) => {
let rows = [];
let rangeStart = 0;
@ -13,18 +19,12 @@ export async function fetchMusicData() {
while (true) {
const { data, error } = await supabase
.from(viewName)
.select('*')
.select("*")
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error(`Error fetching data from view ${viewName}:`, error);
break;
}
if (data.length === 0) break;
if (error || data.length === 0) break;
rows = [...rows, ...data];
if (data.length < PAGE_SIZE) break;
rangeStart += PAGE_SIZE;
}
@ -32,48 +32,51 @@ export async function fetchMusicData() {
return rows;
};
try {
const [
recentTracks,
weekTracks,
weekArtists,
weekAlbums,
weekGenres,
monthTracks,
monthArtists,
monthAlbums,
monthGenres,
] = await Promise.all([
fetchDataFromView('recent_tracks'),
fetchDataFromView('week_tracks'),
fetchDataFromView('week_artists'),
fetchDataFromView('week_albums'),
fetchDataFromView('week_genres'),
fetchDataFromView('month_tracks'),
fetchDataFromView('month_artists'),
fetchDataFromView('month_albums'),
fetchDataFromView('month_genres'),
]);
const [
recentTracks,
weekTracks,
weekArtists,
weekAlbums,
weekGenres,
monthTracks,
monthArtists,
monthAlbums,
monthGenres,
] = await Promise.all([
fetchDataFromView("recent_tracks"),
fetchDataFromView("week_tracks"),
fetchDataFromView("week_artists"),
fetchDataFromView("week_albums"),
fetchDataFromView("week_genres"),
fetchDataFromView("month_tracks"),
fetchDataFromView("month_artists"),
fetchDataFromView("month_albums"),
fetchDataFromView("month_genres"),
]);
return {
recent: recentTracks,
week: {
tracks: weekTracks,
artists: weekArtists,
albums: weekAlbums,
genres: weekGenres,
totalTracks: weekTracks.reduce((acc, track) => acc + track.plays, 0).toLocaleString('en-US'),
},
month: {
tracks: monthTracks,
artists: monthArtists,
albums: monthAlbums,
genres: monthGenres,
totalTracks: monthTracks.reduce((acc, track) => acc + track.plays, 0).toLocaleString('en-US'),
},
};
} catch (error) {
console.error('Error fetching and processing music data:', error);
return {};
}
};
const result = {
recent: recentTracks,
week: {
tracks: weekTracks,
artists: weekArtists,
albums: weekAlbums,
genres: weekGenres,
totalTracks: weekTracks
.reduce((acc, track) => acc + track.plays, 0)
.toLocaleString("en-US"),
},
month: {
tracks: monthTracks,
artists: monthArtists,
albums: monthAlbums,
genres: monthGenres,
totalTracks: monthTracks
.reduce((acc, track) => acc + track.plays, 0)
.toLocaleString("en-US"),
},
};
if (import.meta.env.MODE === "development") cachedMusicData = result;
return result;
};

View file

@ -1,39 +1,39 @@
import { createClient } from '@supabase/supabase-js'
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedNavigation = null;
export async function fetchNavigation() {
const { data, error } = await supabase
.from('optimized_navigation')
.select('*')
if (import.meta.env.MODE === "development" && cachedNavigation)
return cachedNavigation;
if (error) {
console.error('Error fetching navigation data:', error)
return {}
}
const { data, error } = await supabase
.from("optimized_navigation")
.select("*");
if (error) return {};
const menu = data.reduce((acc, item) => {
const menuItem = {
title: item['title'] || item['page_title'],
permalink: item['permalink'] || item ['page_permalink'],
icon: item['icon'],
sort: item['sort']
}
title: item["title"] || item["page_title"],
permalink: item["permalink"] || item["page_permalink"],
icon: item["icon"],
sort: item["sort"],
};
if (!acc[item['menu_location']]) {
acc[item['menu_location']] = [menuItem]
} else {
acc[item['menu_location']].push(menuItem)
}
if (!acc[item["menu_location"]]) acc[item["menu_location"]] = [menuItem];
else acc[item["menu_location"]].push(menuItem);
return acc
}, {})
return acc;
}, {});
Object.keys(menu).forEach(location => {
menu[location].sort((a, b) => a['sort'] - b['sort'])
})
Object.keys(menu).forEach((location) => {
menu[location].sort((a, b) => a["sort"] - b["sort"]);
});
return menu
}
if (import.meta.env.MODE === "development") cachedNavigation = menu;
return menu;
};

View file

@ -1,24 +1,31 @@
import { createClient } from '@supabase/supabase-js'
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedNowPlaying = null;
export async function fetchNowPlaying() {
if (import.meta.env.MODE === "development" && cachedNowPlaying)
return cachedNowPlaying;
const { data, error } = await supabase
.from('optimized_latest_listen')
.select('*')
.single()
.from("optimized_latest_listen")
.select("*")
.single();
if (error) return {};
if (error) {
console.error('Error fetching the latest track:', error)
return {}
}
const genreEmoji = data.genre_emoji;
const emoji = data.artist_emoji || genreEmoji;
const genreEmoji = data.genre_emoji
const emoji = data.artist_emoji || genreEmoji
const result = {
content: `${emoji || "🎧"} ${
data.track_name
} by <a href="https://coryd.dev${data.url}">${data.artist_name}</a>`,
};
return {
content: `${emoji || '🎧'} ${data.track_name} by <a href="https://coryd.dev${data.url}">${data.artist_name}</a>`,
}
}
if (import.meta.env.MODE === "development") cachedNowPlaying = result;
return result;
};

View file

@ -1,16 +1,18 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
export async function fetchPages() {
const { data, error } = await supabase.from('optimized_pages').select('*');
let cachedPages = null;
if (error) {
console.error('Error fetching pages:', error);
return [];
}
export async function fetchPages() {
if (import.meta.env.MODE === "development" && cachedPages) return cachedPages;
const { data, error } = await supabase.from("optimized_pages").select("*");
if (error) return [];
if (import.meta.env.MODE === "development") cachedPages = data;
return data;
}
};

View file

@ -1,32 +1,35 @@
import { createClient } from '@supabase/supabase-js'
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env['SUPABASE_URL']
const SUPABASE_KEY = import.meta.env['SUPABASE_KEY']
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const PAGE_SIZE = 1000
const SUPABASE_URL = import.meta.env["SUPABASE_URL"];
const SUPABASE_KEY = import.meta.env["SUPABASE_KEY"];
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
let cachedPosts = null;
export async function fetchAllPosts() {
let posts = []
let page = 0
let fetchMore = true
if (import.meta.env.MODE === "development" && cachedPosts) return cachedPosts;
const PAGE_SIZE = 1000;
let posts = [];
let page = 0;
let fetchMore = true;
while (fetchMore) {
const { data, error } = await supabase
.from('optimized_posts')
.select('*')
.order('date', { ascending: false })
.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE - 1)
.from("optimized_posts")
.select("*")
.order("date", { ascending: false })
.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE - 1);
if (error) {
console.error('Error fetching posts:', error)
return posts
}
if (error) return posts;
if (data.length < PAGE_SIZE) fetchMore = false
if (data.length < PAGE_SIZE) fetchMore = false;
posts = posts.concat(data)
page++
posts = posts.concat(data);
page++;
}
return posts
}
if (import.meta.env.MODE === "development") cachedPosts = posts;
return posts;
};

View file

@ -1,29 +1,35 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 500;
let cachedRobots = null;
export async function fetchAllRobots() {
if (import.meta.env.MODE === "development" && cachedRobots)
return cachedRobots;
const PAGE_SIZE = 500;
let robots = [];
let from = 0;
while (true) {
const { data, error } = await supabase
.from('robots')
.select('user_agent')
.from("robots")
.select("user_agent")
.range(from, from + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching robot data:', error);
return [];
}
if (error) return [];
robots = robots.concat(data);
if (data.length < PAGE_SIZE) break;
from += PAGE_SIZE;
}
return robots.map((robot) => robot['user_agent']).sort();
};
const result = robots.map((robot) => robot["user_agent"]).sort();
if (import.meta.env.MODE === "development") cachedRobots = result;
return result;
};

View file

@ -1,19 +0,0 @@
import { createClient } from '@supabase/supabase-js'
const SUPABASE_URL = import.meta.env.SUPABASE_URL
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
export async function fetchSearchIndex() {
const { data, error } = await supabase
.from('optimized_search_index')
.select('search_index')
if (error) {
console.error('Error fetching search index data:', error)
return []
}
const [{ search_index } = {}] = data
return search_index || []
}

View file

@ -1,20 +1,24 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
export default async function fetchSyndication() {
const { data, error } = await supabase
.from('optimized_syndication')
.select('syndication');
let cachedSyndication = null;
if (error) {
console.error('Error fetching syndication data:', error);
return [];
}
export default async function fetchSyndication() {
if (import.meta.env.MODE === "development" && cachedSyndication)
return cachedSyndication;
const { data, error } = await supabase
.from("optimized_syndication")
.select("syndication");
if (error) return [];
const [{ syndication } = {}] = data;
const result = syndication?.filter((item) => item.syndication !== null) || [];
return syndication?.filter((item) => item.syndication !== null) || [];
}
if (import.meta.env.MODE === "development") cachedSyndication = result;
return result;
};

View file

@ -1,46 +1,53 @@
import { createClient } from '@supabase/supabase-js';
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
let cachedShows = null;
export const fetchShows = async () => {
if (import.meta.env.MODE === "development" && cachedShows) return cachedShows;
const PAGE_SIZE = 1000;
let shows = [];
let rangeStart = 0;
while (true) {
const { data, error } = await supabase
.from('optimized_shows')
.select('*')
.from("optimized_shows")
.select("*")
.range(rangeStart, rangeStart + PAGE_SIZE - 1);
if (error) {
console.error('Error fetching shows:', error);
break;
}
if (error) break;
shows = shows.concat(data);
if (data.length < PAGE_SIZE) break;
rangeStart += PAGE_SIZE;
}
const watchedShows = shows.filter(show => show['last_watched_at'] !== null);
const episodes = watchedShows.map(show => ({
title: show['episode']['title'],
year: show['year'],
formatted_episode: show['episode']['formatted_episode'],
url: show['episode']['url'],
image: show['episode']['image'],
backdrop: show['episode']['backdrop'],
last_watched_at: show['episode']['last_watched_at'],
grid: show['grid'],
type: 'tv'
const watchedShows = shows.filter((show) => show["last_watched_at"] !== null);
const episodes = watchedShows.map((show) => ({
title: show["episode"]["title"],
year: show["year"],
formatted_episode: show["episode"]["formatted_episode"],
url: show["episode"]["url"],
image: show["episode"]["image"],
backdrop: show["episode"]["backdrop"],
last_watched_at: show["episode"]["last_watched_at"],
grid: show["grid"],
type: "tv",
}));
return {
const result = {
shows,
recentlyWatched: episodes.slice(0, 225),
favorites: shows.filter(show => show.favorite).sort((a, b) => a.title.localeCompare(b.title)),
favorites: shows
.filter((show) => show.favorite)
.sort((a, b) => a.title.localeCompare(b.title)),
};
};
if (import.meta.env.MODE === "development") cachedShows = result;
return result;
};