summaryrefslogtreecommitdiff
path: root/app/src/pages/categorize.tsx
blob: d91d8c142dad15d46a83835c9695abc3529e9bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as Bun from "bun";
import { Suspense } from "react";
import { TwitterApiService, TwitterBookmark } from "../lib/twitter-api";
import {
  userCategories,
  type CategorizationResponse,
} from "../lib/categorization";
import { LLMService } from "../lib/llm-service";
import ProcessedBookmark from "../components/cat/Entry";

interface CategorizePageProps {
  bookmarks: Awaited<ReturnType<TwitterApiService["fetchAllBookmarks"]>>;
  currentBookmarkIndex: number;
  categorization?: CategorizationResponse;
  error?: string;
}

async function CategorizationFetcher({
  bookmarks,
  currentIndex,
}: {
  bookmarks: TwitterBookmark[];
  currentIndex: number;
}) {
  if (currentIndex >= bookmarks.length) {
    return (
      <div className="text-center py-12">
        <h2 className="text-2xl font-bold text-gray-900 mb-4">
          All Bookmarks Categorized!
        </h2>
        <p className="text-gray-600 mb-6">
          You've successfully categorized all your bookmarks.
        </p>
        <a
          href="/"
          className="px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
        >
          Back to Bookmarks
        </a>
      </div>
    );
  }

  const bookmark = bookmarks[currentIndex];
  const llmRes = await callLLM(bookmark!);
  if ("error" in llmRes)
    return (
      <div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg">
        Error categorizing bookmark: {llmRes.error}
      </div>
    );
  return (
    <div className="">
      <ProcessedBookmark
        bookmark={bookmark!}
        categorization={llmRes.ok}
        currentIndex={currentIndex}
        totalCount={bookmarks.length}
      />
    </div>
  );
}

function LoadingSpinner() {
  return (
    <div className="flex items-center justify-center py-12">
      <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
      <span className="ml-3 text-gray-600">
        Loading your Twitter bookmarks...
      </span>
    </div>
  );
}

export default async function CategorizePage(props: any) {
  // console.log({ props });
  const params = new URLSearchParams(props.query);
  const currentIndex = Number(params.get("idx") || "0");
  const cookie = Bun.env.TWATTER_COKI;

  if (!cookie) {
    return (
      <div className="text-red-600 text-center py-12">
        Missing Twitter cookie configuration
      </div>
    );
  }
  const twbookmarks = await getTwData(cookie);
  if ("error" in twbookmarks) {
    return (
      <div className="text-red-600 text-center py-12">
        Error fetching Twatter bookmarks
      </div>
    );
  }
  // const currentIndex = parseInt(searchParams.index || '0', 10);
  const totalCount = twbookmarks.ok.length;

  return (
    <div className="min-h-screen bg-gray-50 py-8">
      <div className="max-w-4xl mx-auto px-4">
        <title>Categorize Bookmarks - SORMARK</title>

        <div className="mb-8">
          <h1 className="text-4xl font-bold tracking-tight mb-4">
            Categorize Bookmarks
          </h1>
          <p className="text-lg text-gray-600">
            Review and categorize your Twitter bookmarks one by one
          </p>
        </div>

        <Suspense fallback={<LoadingSpinner />}>
          <>
            <div className="bg-blue-600 text-white p-4">
              <p className="text-blue-100 mt-1">
                Bookmark {currentIndex + 1} of {totalCount}
              </p>
              <div className="w-full bg-blue-800 rounded-full h-2 mt-2">
                <div
                  className="bg-white h-2 rounded-full transition-all duration-300"
                  style={{
                    width: `${((currentIndex + 1) / totalCount) * 100}%`,
                  }}
                ></div>
              </div>
            </div>
            <CategorizationFetcher
              bookmarks={twbookmarks.ok}
              currentIndex={currentIndex}
            />
          </>
        </Suspense>
      </div>
    </div>
  );
}

import bmarks from "../lib/testData.json";
async function getTwData(cookie: string) {
  try {
    // const twitterService = new TwitterApiService(cookie);
    // const bookmarks = await twitterService.fetchAllBookmarks();
    // return { ok: bookmarks };
    return { ok: bmarks } as any;
  } catch (error) {
    return { error: `${error}` };
  }
}

async function callLLM(bookmark: TwitterBookmark) {
  const apiKey = Bun.env.GEMINI_API_KEY!;
  try {
    const llmService = new LLMService(apiKey);

    const categorization = await llmService.categorizeBookmark({
      bookmark,
      userCategories,
    });

    return { ok: categorization };
  } catch (error) {
    return { error: `${error}` };
  }
}

export const getConfig = async () => {
  return {
    render: "dynamic",
  } as const;
};