From e6c47efcb0bf4e6e05e5e7e0c29655e1ffc79fdf Mon Sep 17 00:00:00 2001 From: Terry Sutton Date: Thu, 22 Feb 2024 15:40:13 -0330 Subject: [PATCH] Add recently generated section --- .../[message_id]/getAssistantMessage.ts | 1 - .../[thread_id]/[message_id]/getMessages.ts | 2 +- apps/database-new/app/actions.ts | 4 +- apps/database-new/app/auth/callback/route.ts | 1 - apps/database-new/app/profile/Threads.tsx | 1 - apps/database-new/app/profile/page.tsx | 6 -- .../components/Auth/LoginForm.tsx | 2 +- .../components/Header/UserDropdown.tsx | 1 - .../components/RecentlyGenerated.tsx | 45 ++++++++++++- apps/database-new/next.config.js | 9 +++ .../migrations/20231122191507_get_started.sql | 32 ++++----- apps/database-new/types/supabase.ts | 67 +++++++++++++++++++ 12 files changed, 138 insertions(+), 33 deletions(-) diff --git a/apps/database-new/app/[thread_id]/[message_id]/getAssistantMessage.ts b/apps/database-new/app/[thread_id]/[message_id]/getAssistantMessage.ts index 5d5de4d165a..71a8fce582f 100644 --- a/apps/database-new/app/[thread_id]/[message_id]/getAssistantMessage.ts +++ b/apps/database-new/app/[thread_id]/[message_id]/getAssistantMessage.ts @@ -1,5 +1,4 @@ import { stripIndent } from 'common-tags' -import { cookies } from 'next/headers' import OpenAI from 'openai' // import is weird, what's up with this? diff --git a/apps/database-new/app/[thread_id]/[message_id]/getMessages.ts b/apps/database-new/app/[thread_id]/[message_id]/getMessages.ts index 3909a2551c5..df8e7bc037b 100644 --- a/apps/database-new/app/[thread_id]/[message_id]/getMessages.ts +++ b/apps/database-new/app/[thread_id]/[message_id]/getMessages.ts @@ -1,5 +1,5 @@ import { createClient } from '@/lib/supabase/server' -import { cookies } from 'next/headers' + import { cache } from 'react' export const getMessages = cache(async (thread_id: string) => { diff --git a/apps/database-new/app/actions.ts b/apps/database-new/app/actions.ts index eb6e47adb83..5c6ced3540d 100644 --- a/apps/database-new/app/actions.ts +++ b/apps/database-new/app/actions.ts @@ -3,7 +3,6 @@ import { createClient } from '@/lib/supabase/server' import { Message } from 'ai' import { revalidatePath } from 'next/cache' -import { cookies } from 'next/headers' import { redirect } from 'next/navigation' import { z } from 'zod' @@ -233,8 +232,7 @@ export async function updateThreadName(prevState: any, formData: FormData) { } } export async function updateThreadVisibility(prevState: any, formData: FormData) { - const cookieStore = cookies() - const supabase = createClient(cookieStore) + const supabase = createClient() try { const schema = z.object({ diff --git a/apps/database-new/app/auth/callback/route.ts b/apps/database-new/app/auth/callback/route.ts index 6790b4af710..935663154dd 100644 --- a/apps/database-new/app/auth/callback/route.ts +++ b/apps/database-new/app/auth/callback/route.ts @@ -1,5 +1,4 @@ import { NextResponse } from 'next/server' -import { cookies } from 'next/headers' import { createClient } from '@/lib/supabase/server' export async function GET(request: Request) { diff --git a/apps/database-new/app/profile/Threads.tsx b/apps/database-new/app/profile/Threads.tsx index e9f24c81434..f90f11d9dba 100644 --- a/apps/database-new/app/profile/Threads.tsx +++ b/apps/database-new/app/profile/Threads.tsx @@ -1,6 +1,5 @@ import { createClient } from '@/lib/supabase/server' import { Database } from '@/types/supabase' -import { cookies } from 'next/headers' import EmptyState from './EmptyState' import Thread from './Thread' diff --git a/apps/database-new/app/profile/page.tsx b/apps/database-new/app/profile/page.tsx index 96046ff681e..dd806909eeb 100644 --- a/apps/database-new/app/profile/page.tsx +++ b/apps/database-new/app/profile/page.tsx @@ -1,6 +1,5 @@ import { createClient } from '@/lib/supabase/server' import { default as dayjs, default as relativeTime } from 'dayjs' -import { cookies } from 'next/headers' import { redirect } from 'next/navigation' import Threads from './Threads' import { Suspense } from 'react' @@ -12,11 +11,6 @@ export const metadata: Metadata = { title: 'database.design | Profile', } -// revalidatePath not working in the create route handler -// force-dynamic to refetch every time if needed -// just a hack for now -// export const dynamic = 'force-dynamic' - const Profile = async () => { return (
diff --git a/apps/database-new/components/Auth/LoginForm.tsx b/apps/database-new/components/Auth/LoginForm.tsx index bb008ad1cb5..0e62fa2ef86 100644 --- a/apps/database-new/components/Auth/LoginForm.tsx +++ b/apps/database-new/components/Auth/LoginForm.tsx @@ -1,6 +1,6 @@ import { Button } from '@ui/components/Button' import { Github } from 'lucide-react' -import { cookies, headers } from 'next/headers' +import { headers } from 'next/headers' import { redirect } from 'next/navigation' import { createClient } from '@/lib/supabase/server' diff --git a/apps/database-new/components/Header/UserDropdown.tsx b/apps/database-new/components/Header/UserDropdown.tsx index 58b3fc2d8a0..a4ed5bded77 100644 --- a/apps/database-new/components/Header/UserDropdown.tsx +++ b/apps/database-new/components/Header/UserDropdown.tsx @@ -1,6 +1,5 @@ import { createClient } from '@/lib/supabase/server' import { User2 } from 'lucide-react' -import { cookies } from 'next/headers' import { Suspense } from 'react' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from 'ui' import AvatarDropdown from './AvatarDropdown' diff --git a/apps/database-new/components/RecentlyGenerated.tsx b/apps/database-new/components/RecentlyGenerated.tsx index 40f2897a598..31b9cf3da07 100644 --- a/apps/database-new/components/RecentlyGenerated.tsx +++ b/apps/database-new/components/RecentlyGenerated.tsx @@ -1,8 +1,49 @@ -const RecentlyGenerated = () => { +import { createClient } from '@/lib/supabase/server' +import Image from 'next/image' +import { Suspense } from 'react' +import { GenericSkeletonLoader } from './Shimmer' +import Link from 'next/link' +const RecentlyGenerated = async () => { + const supabase = createClient() + const { data: threads } = await supabase + .from('profile_threads') + .select() + .eq('is_public', true) + .order('created_at', { ascending: false }) + + // @todo + // these probably need to be picked randomly somehow so + // the same user doesn't show up every time if they've + // generated a bunch of threads + return (

Recently generated

-

show from previous day to give ppl time to make their generation private

+ }> +
+
    + {threads?.map((thread) => ( +
  • + + {thread.user_avatar_url && ( + avatar + )} + {thread.thread_title} + +
  • + ))} +
+
+
) } diff --git a/apps/database-new/next.config.js b/apps/database-new/next.config.js index 9de7ef8a94c..6ef9cca017e 100644 --- a/apps/database-new/next.config.js +++ b/apps/database-new/next.config.js @@ -1,5 +1,14 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + images: { + remotePatterns: [ + { + protocol: 'https', + hostname: 'avatars.githubusercontent.com', + port: '', + }, + ], + }, transpilePackages: ['ui', 'ui-patterns'], webpack: (config, { dev, isServer, webpack, nextRuntime }) => { config.module.rules.push({ diff --git a/apps/database-new/supabase/migrations/20231122191507_get_started.sql b/apps/database-new/supabase/migrations/20231122191507_get_started.sql index 8ef59fcbdc4..77ac5125eb2 100644 --- a/apps/database-new/supabase/migrations/20231122191507_get_started.sql +++ b/apps/database-new/supabase/migrations/20231122191507_get_started.sql @@ -21,20 +21,20 @@ create table if not exists public.messages ( constraint messages_thread_id_fkey foreign key (thread_id) references threads (id) on update cascade on delete cascade, constraint messages_user_id_fkey foreign key (user_id) references auth.users (id) on update cascade on delete cascade ); +create or replace view profile_threads as +select distinct on (m.thread_id) + m.thread_id, + m.message_id, + m.user_id, + m.created_at, + t.thread_title, + t.is_public, + u.raw_user_meta_data->>'avatar_url' as user_avatar_url +from + public.messages m +join public.threads t on m.thread_id = t.id +join auth.users u on t.user_id = u.id +order by + m.thread_id, + m.created_at asc; -CREATE -OR REPLACE VIEW profile_threads AS -SELECT - DISTINCT ON (m.thread_id) m.thread_id, - m.message_id, - m.user_id, - m.created_at, - t.thread_title, - t.is_public - -FROM - public.messages m - JOIN public.threads t ON m.thread_id = t.id -ORDER BY - m.thread_id, - m.created_at ASC; \ No newline at end of file diff --git a/apps/database-new/types/supabase.ts b/apps/database-new/types/supabase.ts index 815718179dd..93236fc7cf5 100644 --- a/apps/database-new/types/supabase.ts +++ b/apps/database-new/types/supabase.ts @@ -48,6 +48,20 @@ export interface Database { referencedRelation: "threads" referencedColumns: ["id"] }, + { + foreignKeyName: "messages_thread_id_fkey" + columns: ["thread_id"] + isOneToOne: false + referencedRelation: "threads_with_avatars" + referencedColumns: ["thread_id"] + }, + { + foreignKeyName: "messages_thread_id_fkey" + columns: ["thread_id"] + isOneToOne: false + referencedRelation: "threads_with_emails" + referencedColumns: ["thread_id"] + }, { foreignKeyName: "messages_user_id_fkey" columns: ["user_id"] @@ -98,6 +112,7 @@ export interface Database { message_id: string | null thread_id: string | null thread_title: string | null + user_avatar_url: string | null user_id: string | null } Relationships: [ @@ -108,6 +123,20 @@ export interface Database { referencedRelation: "threads" referencedColumns: ["id"] }, + { + foreignKeyName: "messages_thread_id_fkey" + columns: ["thread_id"] + isOneToOne: false + referencedRelation: "threads_with_avatars" + referencedColumns: ["thread_id"] + }, + { + foreignKeyName: "messages_thread_id_fkey" + columns: ["thread_id"] + isOneToOne: false + referencedRelation: "threads_with_emails" + referencedColumns: ["thread_id"] + }, { foreignKeyName: "messages_user_id_fkey" columns: ["user_id"] @@ -117,6 +146,44 @@ export interface Database { } ] } + threads_with_avatars: { + Row: { + created_at: string | null + is_public: boolean | null + thread_id: string | null + thread_title: string | null + user_avatar_url: string | null + user_id: string | null + } + Relationships: [ + { + foreignKeyName: "threads_user_id_fkey" + columns: ["user_id"] + isOneToOne: false + referencedRelation: "users" + referencedColumns: ["id"] + } + ] + } + threads_with_emails: { + Row: { + created_at: string | null + is_public: boolean | null + thread_id: string | null + thread_title: string | null + user_email: string | null + user_id: string | null + } + Relationships: [ + { + foreignKeyName: "threads_user_id_fkey" + columns: ["user_id"] + isOneToOne: false + referencedRelation: "users" + referencedColumns: ["id"] + } + ] + } } Functions: { [_ in never]: never