Skip to content

Commit

Permalink
working on recently viewed
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezryr committed Apr 15, 2024
1 parent f7a55cb commit 7de2046
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 47 deletions.
42 changes: 39 additions & 3 deletions src/app/(tabs)/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import styles from './styles';
import Icon from '../../../../assets/icons';
import ContentCard from '../../../components/ContentCard/ContentCard';
import PreviewCard from '../../../components/PreviewCard/PreviewCard';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { fetchUsername } from '../../../queries/profiles';
import {
fetchFeaturedStoriesDescription,
fetchFeaturedStoryPreviews,
fetchNewStories,
fetchRecommendedStories,
} from '../../../queries/stories';
import { StoryCard, StoryPreview } from '../../../queries/types';
import { StoryCard, StoryPreview, Story } from '../../../queries/types';
import globalStyles from '../../../styles/globalStyles';
import { useSession } from '../../../utils/AuthContext';

Expand All @@ -23,37 +24,72 @@ function HomeScreen() {
const [username, setUsername] = useState('');
const [loading, setLoading] = useState(true);
const [featuredStories, setFeaturedStories] = useState<StoryPreview[]>([]);
const [recentlyViewed, setRecentlyViewed] = useState<StoryPreview[]>([]);
const [featuredStoriesDescription, setFeaturedStoriesDescription] =
useState<string>('');
const [recommendedStories, setRecommendedStories] = useState<StoryCard[]>([]);
const [newStories, setNewStories] = useState<StoryCard[]>([]);

const setRecentStory = async (recentStories: StoryPreview[]) => {
try {
const jsonValue = JSON.stringify(recentStories);
await AsyncStorage.setItem('GWN_RECENT_STORIES_ARRAY', jsonValue);
} catch (error) {
console.log(error);
}
};

useEffect(() => {
const getRecentStory = async () => {
try {
const jsonValue = await AsyncStorage.getItem(
'GWN_RECENT_STORIES_ARRAY',
);
return jsonValue != null ? JSON.parse(jsonValue) : [];
} catch (error) {
console.log(error);
}
};

(async () => {
const [
usernameResponse,
featuredStoryResponse,
featuredStoryDescriptionResponse,
recommendedStoriesResponse,
newStoriesResponse,
recentStoryResponse,
] = await Promise.all([
fetchUsername(user?.id).catch(() => ''),
fetchFeaturedStoryPreviews().catch(() => []),
fetchFeaturedStoriesDescription().catch(() => ''),
fetchRecommendedStories().catch(() => []),
fetchRecommendedStories(recentlyViewed).catch(() => []),
fetchNewStories().catch(() => []),
getRecentStory(),
]);

setUsername(usernameResponse);
setFeaturedStories(featuredStoryResponse);
setFeaturedStoriesDescription(featuredStoryDescriptionResponse);
setRecommendedStories(recommendedStoriesResponse);
setNewStories(newStoriesResponse);
setRecentlyViewed(recentStoryResponse);
})().finally(() => {
setLoading(false);
async () => {
const recommendedStoriesResponse = await fetchRecommendedStories(
recentlyViewed,
).catch(() => []);
setRecommendedStories(recommendedStoriesResponse);
};
});
}, [user]);

// useEffect(() => {
// (async () => {

// })
// }, [recentlyViewed])

return (
<SafeAreaView
style={[globalStyles.container, { marginLeft: -8, marginRight: -32 }]}
Expand Down
42 changes: 0 additions & 42 deletions src/queries/recommendations.js

This file was deleted.

35 changes: 33 additions & 2 deletions src/queries/stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Story, StoryPreview, StoryCard } from './types';
import AsyncStorage from '@react-native-async-storage/async-storage';
import supabase from '../utils/supabase';
import { useState } from 'react';

export async function fetchAllStoryPreviews(): Promise<StoryPreview[]> {
const { data, error } = await supabase.rpc('fetch_all_story_previews');
Expand Down Expand Up @@ -57,8 +59,37 @@ export async function fetchFeaturedStoriesDescription(): Promise<string> {
}
}

export async function fetchRecommendedStories(): Promise<StoryCard[]> {
const { data, error } = await supabase.rpc('fetch_recommended_stories');
export async function fetchRecommendedStories(
recentlyViewed: StoryCard[],
): Promise<StoryCard[]> {
const recentlyViewedID = recentlyViewed[0].id; //change to take in multiple stories

const getStoryEmbedding = async () => {
const { data } = await supabase
.from('stories')
.select('embedding')
.eq('id', recentlyViewedID);

if (error) {
console.log(error);
throw new Error(
`An error occured when trying to fetch embeddings: ${error.details}`,
);
} else {
if (data) return data[0].embedding as number;
}
};

const embedding = await getStoryEmbedding();

const { data, error } = await supabase.rpc(
'fetch_users_recommended_stories',
{
query_embedding: embedding,
match_threshold: 0.0,
match_count: 5,
},
);

if (error) {
console.log(error);
Expand Down

0 comments on commit 7de2046

Please sign in to comment.