diff --git a/.env.production b/.env.production index 82c3985..220a7e6 100644 --- a/.env.production +++ b/.env.production @@ -23,3 +23,7 @@ NEXT_PUBLIC_SENTRY_DSN= # Logging ingestion # LOGTAIL_SOURCE_TOKEN= ######## [END] SENSITIVE DATA + +DATABASE_URL='/var/data/db1.db' +CLOUD_URL="https://api-gateway-electron.onrender.com" + diff --git a/src/app/[locale]/(auth)/api/fetch/route.ts b/src/app/[locale]/(auth)/api/fetch/route.ts new file mode 100644 index 0000000..8276123 --- /dev/null +++ b/src/app/[locale]/(auth)/api/fetch/route.ts @@ -0,0 +1,35 @@ +import { NextResponse } from 'next/server'; + +import { logger } from '@/libs/Logger'; + +// import env variables + +export const POST = async (request: Request) => { + const { id } = await request.json(); + const { CLOUD_URL, DATABASE_URL } = process.env; + + const resp = await fetch(`${CLOUD_URL}/fetch`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: id.toString(), + dbPath: DATABASE_URL, + }), + }); + logger.info('resp:', resp); + const data = await resp.json(); + + try { + logger.info(`A new fetch has been created ${JSON.stringify(data)}`); + + return NextResponse.json({ + data, + }); + } catch (error) { + logger.error(error, 'An error occurred while creating a search'); + + return NextResponse.json({}, { status: 500 }); + } +}; diff --git a/src/app/[locale]/(auth)/api/search/route.ts b/src/app/[locale]/(auth)/api/search/route.ts index c1e23b5..6022b4e 100644 --- a/src/app/[locale]/(auth)/api/search/route.ts +++ b/src/app/[locale]/(auth)/api/search/route.ts @@ -2,17 +2,20 @@ import { NextResponse } from 'next/server'; import { logger } from '@/libs/Logger'; +// import env variables + export const POST = async (request: Request) => { - const { query, dbPath } = await request.json(); + const { query } = await request.json(); + const { CLOUD_URL, DATABASE_URL } = process.env; - const resp = await fetch('https://api-gateway-electron.onrender.com/search', { + const resp = await fetch(`${CLOUD_URL}/search`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query, - dbPath, + dbPath: DATABASE_URL, }), }); diff --git a/src/components/Entries.tsx b/src/components/Entries.tsx index 938ffa5..5096ba8 100644 --- a/src/components/Entries.tsx +++ b/src/components/Entries.tsx @@ -24,10 +24,10 @@ const Entries = ({ searchResults, onDelve }: EntriesProps) => {
{searchResults.map((entry: any) => ( { : '' } onDelve={onDelve} - hasAliases={'_display' in entry.metadata} + hasAliases={'aliasData' in entry} /> ))}
diff --git a/src/components/Entry.tsx b/src/components/Entry.tsx index 675aae4..d2e9884 100644 --- a/src/components/Entry.tsx +++ b/src/components/Entry.tsx @@ -27,6 +27,8 @@ const Entry = ({ imageUrl = '', hasImage = false, onDelve = (_: string) => {}, + aliases = [], + selectedIndex = -1, }) => { const [hasAliases] = useState(initialHasAliases); // const [hasCollections] = useState(initialHasCollections); @@ -72,37 +74,52 @@ const Entry = ({

Aliases:

+ {/* { + "id": "2449", + "data": "me at a hot ones interview", + "metadata": { + "title": "bingus ✩ on X: \"https://t.co/G8jSTYplMv\" / X", + "author": "https://x.com/aliaoftheblade/status/1822602088427487358", + "alias_ids": [ + 2450, + 2451, + 2517 + ], + "s_id": 2449 + }, + "createdAt": "2024-08-12T01:56:14.569Z", + "updatedAt": "2024-08-12T01:56:14.569Z", + "aliasData": [ + "dune meme", + "hot ones meme", + "hot ones dune" + ], + "selectedIndex": 1 +} +map over aliasData and display each alias in a list and put a (*) next to the selected index +and turn each into a link that searches for that alias +*/}
diff --git a/src/components/SearchBox.tsx b/src/components/SearchBox.tsx index f46d024..67c3e41 100644 --- a/src/components/SearchBox.tsx +++ b/src/components/SearchBox.tsx @@ -1,3 +1,5 @@ +/* eslint-disable no-console */ + 'use client'; import { useState } from 'react'; @@ -5,9 +7,29 @@ import { useState } from 'react'; import Entries from './Entries'; const SearchBox = () => { - const [searchResults, setSearchResults] = useState([]); + const [searchResults, setSearchResults] = useState([]); const [textAreaValue, setTextAreaValue] = useState(''); + const fetchByID = async (id: string) => { + try { + const response = await fetch('/api/fetch', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id, + }), + }); + const data = await response.json(); + + return data; + } catch (error) { + console.error('Error fetching entry by ID:', error); + return {}; + } + }; + const fetchSearchResults = async (query: string) => { try { const response = await fetch('/api/search', { @@ -17,9 +39,6 @@ const SearchBox = () => { }, body: JSON.stringify({ query, - dbPath: - // '/Users/bram/Dropbox/PARA/Projects/api-gateway-local-build/api-gateway-electron/yourcommonbase.db', - '/var/data/db1.db', }), }); console.log('response:', response); @@ -36,7 +55,98 @@ const SearchBox = () => { } }); - setSearchResults(updatedData); + // for each entry, fetch the if metadata has a parent ID fetch the parent entry and its aliases and then fetch them + /* + ex: + { + "title": "Your Commonbase Dashboard", + "author": "", + "_display": "right after chq talk kind of depressed was looking fwd to this and a bit empty now its over in terms of next goal. also afraid of medicine that i promised myself id start taking", + "parent_id": 2575 +} + + fetchByID(2575).then((parentEntry) => { + "title": "Your Commonbase Dashboard", + "author": "", + "alias_ids": [ + 2580 + ] +}).then((parentEntry) => { + let aliasData = []; + for (let i = 0; i < parentEntry.alias_ids.length; i++) { + fetchByID(parentEntry.alias_ids[i]); + aliasData.push(aliasData.data); + } + + updatedMetadataWAliases = { + ...parentEntry, + aliasData: [...], + selectedIndex: (index of the alias entry that was in the first step) + */ + + const updatedDataWithAliases = await Promise.all( + updatedData.map(async (entry: any) => { + if (entry.metadata.parent_id) { + try { + let selectedIndex = -1; + + // Fetch parent entry by parent_id + const parentEntryRes = await fetchByID(entry.metadata.parent_id); + const parentEntry = parentEntryRes.data; + + // Parse parent metadata + const parentMetadataJSON = JSON.parse(parentEntry.metadata); + parentEntry.metadata = parentMetadataJSON; + + // Find the index of the current entry in the parent's alias_ids + if ( + parentMetadataJSON.alias_ids && + parentMetadataJSON.alias_ids.includes(Number(entry.id)) + ) { + selectedIndex = parentMetadataJSON.alias_ids.indexOf( + Number(entry.id), + ); + } + + // Fetch all alias entries by alias_ids + const aliasData = await Promise.all( + parentMetadataJSON.alias_ids.map(async (aliasId: string) => { + try { + const aliasEntryRes = await fetchByID(aliasId); + const aliasEntry = aliasEntryRes.data; + return aliasEntry.data; + } catch (aliasFetchError) { + console.error( + `Error fetching alias entry with ID ${aliasId}:`, + aliasFetchError, + ); + throw aliasFetchError; + } + }), + ); + + // Return the combined entry with parent and alias data + return { + ...parentEntry, + aliasData, + similarity: entry.similarity, + selectedIndex, + }; + } catch (parentFetchError) { + console.error( + `Error fetching parent entry with ID ${entry.metadata.parent_id}:`, + parentFetchError, + ); + throw parentFetchError; + } + } + + return entry; + }), + ); + + console.log('Setting search results:', updatedDataWithAliases); + setSearchResults(updatedDataWithAliases); } catch (error) { console.error('Error fetching search results:', error); } diff --git a/src/libs/Env.ts b/src/libs/Env.ts index 4da332e..911a64f 100644 --- a/src/libs/Env.ts +++ b/src/libs/Env.ts @@ -7,6 +7,7 @@ export const Env = createEnv({ CLERK_SECRET_KEY: z.string().min(1), DATABASE_URL: z.string().optional(), LOGTAIL_SOURCE_TOKEN: z.string().optional(), + CLOUD_URL: z.string().optional(), }, client: { NEXT_PUBLIC_APP_URL: z.string().optional(), @@ -26,5 +27,6 @@ export const Env = createEnv({ process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, NEXT_PUBLIC_CLERK_SIGN_IN_URL: process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL, NODE_ENV: process.env.NODE_ENV, + CLOUD_URL: process.env.CLOUD_URL, }, });