Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh committed Aug 13, 2024
1 parent c8c59c1 commit 1670b10
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 29 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"@types/react-dom": "npm:types-react-dom@rc"
},
"devDependencies": {
"dexie": "^4.0.8",
"dexie-react-hooks": "^1.1.7",
"@headlessui/react": "^2.1.2",
"@react-spring/web": "^9.7.4",
"@tailwindcss/forms": "^0.5.7",
Expand All @@ -42,6 +40,8 @@
"apexcharts": "^3.51.0",
"autoprefixer": "^10.4.19",
"compromise": "^14.13.0",
"dexie": "^4.0.8",
"dexie-react-hooks": "^1.1.7",
"eslint": "^8.57.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-import-resolver-vite": "^2.0.1",
Expand Down
44 changes: 42 additions & 2 deletions src/client/hooks/useRefreshers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ky from "ky";
import { useCallback, useState } from "react";

import { loadAllThreadInsightsData } from "@src/lib/breaker";
import { AccessTokenResponse } from "@src/threadsapi/types";

import reply_store from "../reply_store";
Expand All @@ -15,12 +16,17 @@ export const useLast2DaysThreadsRefresher = () => {
const [error, setError] = useState<string | null>(null);

const [isLoggedIn, accessToken] = useIsLoggedIn();
// const refresh = useCacheStore((state) => state.loadUserData);

const caller = useCallback(() => {
async function fetchData(token: AccessTokenResponse) {
setLoading(true);
try {
await Promise.all([thread_store.refreshThreadsLast2Days(kyd, token), reply_store.refreshThreadsLast2Days(kyd, token)]);
await Promise.all([
// refresh(kyd, token),
thread_store.refreshThreadsLast2Days(kyd, token),
reply_store.refreshThreadsLast2Days(kyd, token),
]);
setError(null);
} catch (error) {
console.error(`problem fetching last 2 days threads:`, error);
Expand Down Expand Up @@ -51,7 +57,10 @@ export const useAllThreadsRefresher = () => {
async function fetchData(token: AccessTokenResponse) {
setLoading(true);
try {
await Promise.all([thread_store.loadThreadsData(kyd, token), reply_store.loadUserRepliesData(kyd, token)]);
await Promise.all([
thread_store.loadThreadsData(kyd, token, {}, true),
reply_store.loadUserRepliesData(kyd, token, {}, true),
]);
setError(null);
} catch (error) {
console.error(`problem fetching all threads:`, error);
Expand All @@ -72,6 +81,37 @@ export const useAllThreadsRefresher = () => {
return [caller, isLoading, error] as const;
};

export const useAllInsightsRefresher = () => {
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

const [isLoggedIn, accessToken] = useIsLoggedIn();

const caller = useCallback(() => {
async function fetchData(token: AccessTokenResponse) {
setLoading(true);
try {
await loadAllThreadInsightsData(kyd, token);
setError(null);
} catch (error) {
console.error(`problem fetching all insights:`, error);
setError(`failed to fetch all insights - ${error}`);
alert(`failed to fetch user data - ${error}`);
} finally {
setLoading(false);
}
}

if (isLoggedIn) {
void fetchData(accessToken);
}

return;
}, [isLoggedIn, accessToken, setLoading, setError]);

return [caller, isLoading, error] as const;
};

export const useUserDataRefresher = () => {
const refresh = useCacheStore((state) => state.loadUserData);
const [isLoading, setLoading] = useState(false);
Expand Down
31 changes: 20 additions & 11 deletions src/client/reply_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function makeReplyID(id: string): ReplyID {
return `reply_${id}`;
}

function extractReplyID(id: ReplyID): string {
export function extractReplyID(id: ReplyID): string {
return id.replace(/^reply_/, "").split("_")[0];
}

Expand All @@ -42,7 +42,7 @@ db.version(1).stores({

export { db };

const loadUserRepliesData = async (ky: KyInstance, token: AccessTokenResponse, params?: GetUserThreadsParams) => {
const loadUserRepliesData = async (ky: KyInstance, token: AccessTokenResponse, params?: GetUserThreadsParams, get_insights = false) => {
const promises: Promise<unknown>[] = [];

if (localStorage.getItem("unthread.me/reply_store")) {
Expand Down Expand Up @@ -79,14 +79,13 @@ const loadUserRepliesData = async (ky: KyInstance, token: AccessTokenResponse, p
),
);

for (const thread of data.data) {
const reply_id = makeReplyID(thread.id);
setTimeout(() => {
promises.push(
loadReplyInsightsData(ky, token, reply_id),
// loadReplyRepliesData(ky, token, reply_id)
);
}, 100);
if (get_insights) {
promises.push(
...data.data.map((reply) => {
const id = makeReplyID(reply.id);
return loadReplyInsightsData(ky, token, id);
}),
);
}

if (data.paging?.cursors.after && !params?.limit) {
Expand All @@ -104,6 +103,14 @@ const loadReplyInsightsData = async (ky: KyInstance, token: AccessTokenResponse,
});
};

// const loadAllReplyInsightsData = async (ky: KyInstance, token: AccessTokenResponse) => {
// await db.replies.each(async (reply) => {
// await get_media_insights(ky, token, extractReplyID(reply.reply_id)).then(async (data) => {
// await db.replies.update(reply.reply_id, { insights: data });
// });
// });
// };

// const loadReplyRepliesData = async (ky: KyInstance, token: AccessTokenResponse, id: ReplyID) => {
// await get_conversation(ky, token, extractReplyID(id)).then(async (data) => {
// await db.replies.bulkPut(
Expand Down Expand Up @@ -145,12 +152,14 @@ export default {
loadUserRepliesData,

refreshThreadsLast2Days: async (ky: KyInstance, token: AccessTokenResponse) => {
await loadUserRepliesData(ky, token, { since: `${Math.round((Date.now() - 1000 * 60 * 60 * 24 * 2) / 1000)}` });
await loadUserRepliesData(ky, token, { since: `${Math.round((Date.now() - 1000 * 60 * 60 * 24 * 2) / 1000)}` }, true);
},

clearThreads: () => {
void db.replies.clear();
},
loadReplyInsightsData,
// loadAllReplyInsightsData,
};

/// check if unthread.me/thread_store exists in local storage
Expand Down
29 changes: 22 additions & 7 deletions src/client/thread_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ db.version(2).stores({

export { db };

const loadThreadsData = async (ky: KyInstance, token: AccessTokenResponse, params?: GetUserThreadsParams) => {
const loadThreadsData = async (ky: KyInstance, token: AccessTokenResponse, params?: GetUserThreadsParams, get_insights = false) => {
const promises: Promise<unknown>[] = [];

if (localStorage.getItem("unthread.me/thread_store")) {
Expand All @@ -67,11 +67,12 @@ const loadThreadsData = async (ky: KyInstance, token: AccessTokenResponse, param
),
);

for (const thread of data.data) {
const thread_id = makeThreadID(thread.id);
if (get_insights) {
promises.push(
loadThreadInsightsData(ky, token, thread_id),
// loadThreadRepliesData(ky, token, thread_id)
...data.data.map((thread) => {
const id = makeThreadID(thread.id);
return loadThreadInsightsData(ky, token, id);
}),
);
}

Expand All @@ -84,11 +85,25 @@ const loadThreadsData = async (ky: KyInstance, token: AccessTokenResponse, param
await Promise.all(promises);
};

const loadThreadInsightsData = async (ky: KyInstance, token: AccessTokenResponse, id: ThreadID) => {
export const loadThreadInsightsData = async (ky: KyInstance, token: AccessTokenResponse, id: ThreadID) => {
await get_media_insights(ky, token, extractThreadID(id)).then(async (data) => {
await db.threads.update(id, { insights: data });
});
};

// export const loadAllThreadInsightsData = async (ky: KyInstance, token: AccessTokenResponse) => {
// await db.threads.each(async (thread) => {
// await get_media_insights(ky, token, extractThreadID(thread.thread_id)).then(async (data) => {
// await db.threads.update(thread.thread_id, { insights: data });
// });
// });
// await yo.replies.each(async (reply) => {
// await get_media_insights(ky, token, extractReplyID(reply.reply_id)).then(async (data) => {
// await yo.replies.update(reply.reply_id, { insights: data });
// });
// });
// };

export const loadThreadRepliesData = async (ky: KyInstance, token: AccessTokenResponse, id: ThreadID) => {
await get_conversation(ky, token, extractThreadID(id)).then(async (data) => {
await yo.replies.bulkPut(
Expand Down Expand Up @@ -128,7 +143,7 @@ export default {
loadThreadsData,

refreshThreadsLast2Days: async (ky: KyInstance, token: AccessTokenResponse) => {
await loadThreadsData(ky, token, { since: `${Math.round((Date.now() - 1000 * 60 * 60 * 24 * 2) / 1000)}` });
await loadThreadsData(ky, token, { since: `${Math.round((Date.now() - 1000 * 60 * 60 * 24 * 2) / 1000)}` }, true);
},

clearThreads: () => {
Expand Down
13 changes: 7 additions & 6 deletions src/components/UserProfile2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,19 @@ export default function UserProfile2() {
isLoading: refreshUserDataLoading,
error: refreshUserDataError,
},
{
label: "all threads (slow)",
action: refreshAllThreads,
isLoading: refreshAllThreadsLoading,
error: refreshAllThreadsErr,
},
{
label: "threads last 2 days",
action: refreshLast2DayThreads,
isLoading: refreshLast2DayThreadsLoading,
error: refreshLast2DayThreadsErr,
},

{
label: "all threads (slow)",
action: refreshAllThreads,
isLoading: refreshAllThreadsLoading,
error: refreshAllThreadsErr,
},
];

const engagmentStats = [
Expand Down
3 changes: 3 additions & 0 deletions src/threadsapi/get_media_insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const get_media_insights_with_params = async (
mediaId: string,
params: GetMediaInsightsParams = {},
): Promise<SimplifedMediaMetricTypeMap> => {
if (mediaId.includes("_")) {
mediaId = mediaId.split("_")[1];
}
const searchParams: Record<string, string | number> = {
metric: allMediaMetrics.join(","),
access_token: accessToken.access_token,
Expand Down
2 changes: 1 addition & 1 deletion src/threadsapi/get_user_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const fetch_user_threads_page = async (
if (params?.since) searchParams.since = params.since;
if (params?.until) searchParams.until = params.until;
}
searchParams.limit = 25;
searchParams.limit = 100;

if (cursor) {
searchParams.after = cursor;
Expand Down

0 comments on commit 1670b10

Please sign in to comment.