feat: start of dm overview page
This commit is contained in:
parent
67da0a72db
commit
5d2ce7705c
14 changed files with 442 additions and 34 deletions
29
src/App.tsx
29
src/App.tsx
|
@ -1,7 +1,8 @@
|
|||
import { type Component } from "solid-js";
|
||||
import { Route } from "@solidjs/router";
|
||||
|
||||
import { Home, Overview } from "./pages";
|
||||
import { allThreadsOverviewQuery } from "./db";
|
||||
import { DmId, GroupId, Home, Overview } from "./pages";
|
||||
|
||||
import "./app.css";
|
||||
|
||||
|
@ -16,7 +17,31 @@ const App: Component = () => {
|
|||
path="/overview"
|
||||
component={Overview}
|
||||
/>
|
||||
<Route path="/thread/:threadid" />
|
||||
<Route
|
||||
path="/dm/:dmid"
|
||||
component={DmId}
|
||||
/>
|
||||
<Route
|
||||
path="/group/:groupid"
|
||||
component={GroupId}
|
||||
/>
|
||||
<Route
|
||||
path="/test"
|
||||
component={() => {
|
||||
console.time("first");
|
||||
console.log(allThreadsOverviewQuery());
|
||||
void allThreadsOverviewQuery().then((result) => {
|
||||
console.log(result);
|
||||
console.timeEnd("first");
|
||||
console.time("second");
|
||||
void allThreadsOverviewQuery().then((result) => {
|
||||
console.log(result);
|
||||
console.timeEnd("second");
|
||||
});
|
||||
});
|
||||
return "";
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -36,6 +36,10 @@ import {
|
|||
ScatterController,
|
||||
Tooltip,
|
||||
} from "chart.js";
|
||||
import ChartDeferred from "chartjs-plugin-deferred";
|
||||
import ChartZoom from "chartjs-plugin-zoom";
|
||||
|
||||
Chart.register(ChartDeferred, ChartZoom);
|
||||
|
||||
interface TypedChartProps {
|
||||
data: ChartData;
|
||||
|
|
73
src/db.ts
73
src/db.ts
|
@ -1,11 +1,12 @@
|
|||
import { type Accessor, createMemo, createSignal, DEV, type Setter } from "solid-js";
|
||||
|
||||
import { Kysely, type NotNull } from "kysely";
|
||||
import { Kysely, type NotNull, sql } from "kysely";
|
||||
import type { DB } from "kysely-codegen";
|
||||
import { SqlJsDialect } from "kysely-wasm";
|
||||
import initSqlJS, { type Database } from "sql.js";
|
||||
|
||||
import wasmURL from "./assets/sql-wasm.wasm?url";
|
||||
import { cached } from "./lib/db-cache";
|
||||
|
||||
export const SELF_ID = 2;
|
||||
|
||||
|
@ -13,7 +14,8 @@ export const SQL = await initSqlJS({
|
|||
locateFile: () => wasmURL,
|
||||
});
|
||||
|
||||
let rawDb: Accessor<Database | undefined>, setRawDb: Setter<Database | undefined>;
|
||||
let rawDb: Accessor<Database | undefined> = () => undefined,
|
||||
setRawDb: Setter<Database | undefined> = () => undefined;
|
||||
|
||||
if (DEV) {
|
||||
const file = await import("./assets/database.sqlite?url").then((result) => {
|
||||
|
@ -22,9 +24,9 @@ if (DEV) {
|
|||
|
||||
const testDb = new SQL.Database(new Uint8Array(file));
|
||||
|
||||
[rawDb, setRawDb] = createSignal<Database>(testDb);
|
||||
[rawDb, setRawDb] = createSignal<Database | undefined>(testDb);
|
||||
} else {
|
||||
[rawDb, setRawDb] = createSignal<Database>();
|
||||
[rawDb, setRawDb] = createSignal<Database | undefined>();
|
||||
}
|
||||
|
||||
export { rawDb as db, setRawDb as setDb };
|
||||
|
@ -51,13 +53,13 @@ const kyselyDb = createMemo(() => {
|
|||
});
|
||||
});
|
||||
|
||||
export const threadOverviewQuery = kyselyDb()
|
||||
const allThreadsOverviewQueryRaw = kyselyDb()
|
||||
.selectFrom("thread")
|
||||
.innerJoin(
|
||||
(eb) =>
|
||||
eb
|
||||
.selectFrom("message")
|
||||
.select(["thread_id", kyselyDb().fn.countAll().as("message_count")])
|
||||
.select((eb) => ["message.thread_id", eb.fn.countAll().as("message_count")])
|
||||
.where((eb) => {
|
||||
return eb.and([eb("message.body", "is not", null), eb("message.body", "is not", "")]);
|
||||
})
|
||||
|
@ -82,18 +84,67 @@ export const threadOverviewQuery = kyselyDb()
|
|||
thread_id: NotNull;
|
||||
archived: NotNull;
|
||||
message_count: number;
|
||||
}>();
|
||||
}>()
|
||||
.compile();
|
||||
|
||||
console.log(threadOverviewQuery.compile());
|
||||
export const allThreadsOverviewQuery = cached(() => kyselyDb().executeQuery(allThreadsOverviewQueryRaw));
|
||||
|
||||
export const overallSentMessagesQuery = (recipientId: number) =>
|
||||
const overallSentMessagesQueryRaw = (recipientId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("message")
|
||||
.select(kyselyDb().fn.countAll().as("message_count"))
|
||||
.select((eb) => eb.fn.countAll().as("messageCount"))
|
||||
.where((eb) =>
|
||||
eb.and([
|
||||
eb("message.from_recipient_id", "=", recipientId),
|
||||
eb("message.body", "is not", null),
|
||||
eb("message.body", "!=", ""),
|
||||
]),
|
||||
);
|
||||
)
|
||||
.executeTakeFirst();
|
||||
|
||||
export const overallSentMessagesQuery = cached(overallSentMessagesQueryRaw);
|
||||
|
||||
const dmPartnerRecipientQueryRaw = (dmId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("recipient")
|
||||
.select(["recipient._id", "recipient.system_joined_name", "recipient.profile_joined_name"])
|
||||
.innerJoin("thread", "recipient._id", "thread.recipient_id")
|
||||
.where((eb) => eb.and([eb("thread._id", "=", dmId), eb("recipient._id", "!=", SELF_ID)]))
|
||||
.$narrowType<{
|
||||
_id: number;
|
||||
}>()
|
||||
.executeTakeFirst();
|
||||
|
||||
export const dmPartnerRecipientQuery = cached(dmPartnerRecipientQueryRaw);
|
||||
|
||||
const dmOverviewQueryRaw = (dmId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("message")
|
||||
.select((eb) => [
|
||||
sql<Date>`DATE(datetime(message.date_sent / 1000, 'unixepoch'))`.as("message_date"),
|
||||
eb.fn.countAll().as("message_count"),
|
||||
])
|
||||
.groupBy("message_date")
|
||||
.orderBy("message_date asc")
|
||||
.where("thread_id", "=", dmId)
|
||||
.execute();
|
||||
|
||||
export const dmOverviewQuery = cached(dmOverviewQueryRaw);
|
||||
|
||||
const threadSentMessagesPerPersonOverviewQueryRaw = (dmId: number) =>
|
||||
kyselyDb()
|
||||
.selectFrom("message")
|
||||
.select((eb) => [
|
||||
"from_recipient_id",
|
||||
sql<Date>`DATE(datetime(message.date_sent / 1000, 'unixepoch'))`.as("message_date"),
|
||||
eb.fn.countAll().as("message_count"),
|
||||
])
|
||||
.groupBy(["from_recipient_id", "message_date"])
|
||||
.orderBy(["message_date"])
|
||||
.where((eb) => eb.and([eb("body", "is not", null), eb("body", "!=", ""), eb("thread_id", "=", dmId)]))
|
||||
.$narrowType<{
|
||||
message_count: number;
|
||||
}>()
|
||||
.execute();
|
||||
|
||||
export const dmSentMessagesPerPersonOverviewQuery = cached(threadSentMessagesPerPersonOverviewQueryRaw);
|
||||
|
|
118
src/lib/db-cache.ts
Normal file
118
src/lib/db-cache.ts
Normal file
|
@ -0,0 +1,118 @@
|
|||
import { createEffect, on } from "solid-js";
|
||||
|
||||
const DATABASE_HASH_PREFIX = "database";
|
||||
|
||||
// clear the cache on new session so that selecting a different database does not result in wrong cache entries
|
||||
const clearDbCache = () => {
|
||||
for (let i = 0, len = localStorage.length; i < len; i++) {
|
||||
const key = localStorage.key(i);
|
||||
|
||||
if (key?.startsWith(DATABASE_HASH_PREFIX)) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// https://stackoverflow.com/a/7616484
|
||||
const hashString = (str: string) => {
|
||||
let hash = 0,
|
||||
i,
|
||||
chr;
|
||||
if (str.length === 0) return hash;
|
||||
for (i = 0; i < str.length; i++) {
|
||||
chr = str.charCodeAt(i);
|
||||
hash = (hash << 5) - hash + chr;
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
return hash;
|
||||
};
|
||||
|
||||
const HASH_STORE_KEY = "database_hash";
|
||||
|
||||
// cannot import `db` the normal way because this file is imported in ~/db.ts before the initialisation of `db` has happened
|
||||
queueMicrotask(() => {
|
||||
void import("~/db").then(({ db }) => {
|
||||
createEffect(
|
||||
on(db, (currentDb) => {
|
||||
if (currentDb) {
|
||||
const newHash = hashString(new TextDecoder().decode(currentDb.export())).toString();
|
||||
|
||||
const oldHash = localStorage.getItem(HASH_STORE_KEY);
|
||||
|
||||
console.log(newHash, oldHash);
|
||||
|
||||
if (newHash !== oldHash) {
|
||||
clearDbCache();
|
||||
|
||||
localStorage.setItem(HASH_STORE_KEY, newHash);
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
class LocalStorageCacheAdapter {
|
||||
keys = new Set<string>(Object.keys(localStorage).filter((key) => key.startsWith(this.prefix)));
|
||||
prefix = "database";
|
||||
|
||||
#createKey(cacheName: string, key: string): string {
|
||||
return `${this.prefix}-${cacheName}-${key}`;
|
||||
}
|
||||
|
||||
set(cacheName: string, key: string, value: unknown) {
|
||||
const fullKey = this.#createKey(cacheName, key);
|
||||
this.keys.add(fullKey);
|
||||
|
||||
localStorage.setItem(fullKey, JSON.stringify(value));
|
||||
}
|
||||
|
||||
has(cacheName: string, key: string): boolean {
|
||||
return this.keys.has(this.#createKey(cacheName, key));
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
|
||||
get<R>(cacheName: string, key: string): R | undefined {
|
||||
const item = localStorage.getItem(this.#createKey(cacheName, key));
|
||||
if (item) {
|
||||
return JSON.parse(item) as R;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cache = new LocalStorageCacheAdapter();
|
||||
|
||||
export const cached = <T, R, TT>(fn: (...args: T[]) => R, self?: ThisType<TT>): ((...args: T[]) => R) => {
|
||||
const cacheName = hashString(fn.toString()).toString();
|
||||
|
||||
// important to return a promise on follow-up calls even if the data is immediately available
|
||||
let isPromise: boolean;
|
||||
|
||||
return (...args: T[]) => {
|
||||
const cacheKey = JSON.stringify(args);
|
||||
|
||||
const cachedValue = cache.get<R>(cacheName, cacheKey);
|
||||
|
||||
if (cachedValue) {
|
||||
return (isPromise ? Promise.resolve(cachedValue) : cachedValue) as R;
|
||||
}
|
||||
|
||||
let newValue: R;
|
||||
|
||||
if (self) {
|
||||
newValue = fn.apply(self, args);
|
||||
} else {
|
||||
newValue = fn(...args);
|
||||
}
|
||||
|
||||
const promisified = Promise.resolve(newValue);
|
||||
|
||||
isPromise = promisified == newValue;
|
||||
|
||||
void promisified.then((result) => {
|
||||
cache.set(cacheName, cacheKey, result);
|
||||
});
|
||||
|
||||
return newValue;
|
||||
};
|
||||
};
|
9
src/lib/random.ts
Normal file
9
src/lib/random.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
export const generateRandomString = (length: number) => {
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
let result = "";
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
result += characters[randomIndex];
|
||||
}
|
||||
return result;
|
||||
};
|
|
@ -28,17 +28,3 @@ WHERE
|
|||
r._id = 4
|
||||
AND message.body IS NOT NULL
|
||||
AND message.body != ''`;
|
||||
|
||||
// select all chats / groups with details
|
||||
`SELECT
|
||||
thread.recipient_id,
|
||||
thread.active,
|
||||
recipient.profile_joined_name,
|
||||
recipient.system_joined_name,
|
||||
groups.title
|
||||
FROM
|
||||
thread
|
||||
JOIN recipient ON thread.recipient_id = recipient._id
|
||||
LEFT JOIN groups ON recipient._id = groups.recipient_id
|
||||
WHERE (SELECT 1 FROM message WHERE message.thread_id = thread._id AND message.body IS NOT NULL)
|
||||
`;
|
||||
|
|
144
src/pages/dm/dm-id.tsx
Normal file
144
src/pages/dm/dm-id.tsx
Normal file
|
@ -0,0 +1,144 @@
|
|||
import { type Accessor, type Component, createResource, Show } from "solid-js";
|
||||
import type { RouteSectionProps } from "@solidjs/router";
|
||||
|
||||
import { type ChartData } from "chart.js";
|
||||
|
||||
import { LineChart } from "~/components/ui/charts";
|
||||
|
||||
import { dmPartnerRecipientQuery, dmSentMessagesPerPersonOverviewQuery, SELF_ID } from "~/db";
|
||||
|
||||
export const DmId: Component<RouteSectionProps> = (props) => {
|
||||
const dmId = () => Number(props.params.dmid);
|
||||
|
||||
const [dmPartner] = createResource(async () => {
|
||||
const dmPartner = await dmPartnerRecipientQuery(dmId());
|
||||
|
||||
if (dmPartner) {
|
||||
return {
|
||||
id: dmPartner._id,
|
||||
name: /* can be empty string */ !dmPartner.system_joined_name
|
||||
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
dmPartner.profile_joined_name!
|
||||
: dmPartner.system_joined_name,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
const [dmMessagesPerPerson] = createResource(() => dmSentMessagesPerPersonOverviewQuery(dmId()));
|
||||
|
||||
const dmMessages = () => {
|
||||
return dmMessagesPerPerson()?.reduce<
|
||||
{
|
||||
date: Date;
|
||||
totalMessages: number;
|
||||
[key: number]: number;
|
||||
}[]
|
||||
>((prev, curr) => {
|
||||
const existingDate = prev.find(({ date }) => date === curr.message_date);
|
||||
if (existingDate) {
|
||||
existingDate[curr.from_recipient_id] = curr.message_count;
|
||||
|
||||
existingDate.totalMessages += curr.message_count;
|
||||
} else {
|
||||
prev.push({
|
||||
date: curr.message_date,
|
||||
totalMessages: curr.message_count,
|
||||
[curr.from_recipient_id]: curr.message_count,
|
||||
});
|
||||
}
|
||||
|
||||
return prev;
|
||||
}, []);
|
||||
};
|
||||
|
||||
const recipients = () => {
|
||||
const currentDmPartner = dmPartner();
|
||||
|
||||
if (currentDmPartner) {
|
||||
return [
|
||||
{ recipientId: currentDmPartner.id, name: currentDmPartner.name },
|
||||
{
|
||||
recipientId: SELF_ID,
|
||||
name: "You",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
recipientId: SELF_ID,
|
||||
name: "You",
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const dateChartData: Accessor<ChartData<"line"> | undefined> = () => {
|
||||
const currentDmMessages = dmMessages();
|
||||
const currentRecipients = recipients();
|
||||
|
||||
if (currentDmMessages) {
|
||||
return {
|
||||
labels: currentDmMessages.map((row) => row.date),
|
||||
datasets: [
|
||||
{
|
||||
label: "Total number of messages",
|
||||
data: currentDmMessages.map((row) => row.totalMessages),
|
||||
borderWidth: 2,
|
||||
},
|
||||
...currentDmMessages.reduce<{ id: number; label: string; data: number[] }[]>(
|
||||
(prev, curr) => {
|
||||
for (const recipient of currentRecipients) {
|
||||
prev.find(({ id }) => id === recipient.recipientId)?.data.push(curr[recipient.recipientId] ?? 0);
|
||||
}
|
||||
|
||||
return prev;
|
||||
},
|
||||
currentRecipients.map((recipient) => {
|
||||
return {
|
||||
id: recipient.recipientId,
|
||||
label: `Number of messages from ${recipient.name.toString()}`,
|
||||
data: [],
|
||||
borderWidth: 2,
|
||||
};
|
||||
}),
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Show when={dateChartData()}>
|
||||
{(currentDateChartData) => (
|
||||
<div class="max-h-96">
|
||||
<LineChart
|
||||
options={{
|
||||
normalized: true,
|
||||
aspectRatio: 2,
|
||||
plugins: {
|
||||
zoom: {
|
||||
pan: {
|
||||
enabled: true,
|
||||
mode: "xy",
|
||||
},
|
||||
zoom: {
|
||||
wheel: {
|
||||
enabled: true,
|
||||
},
|
||||
pinch: {
|
||||
enabled: true,
|
||||
},
|
||||
mode: "xy",
|
||||
},
|
||||
},
|
||||
},
|
||||
}}
|
||||
data={currentDateChartData()}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Show>
|
||||
);
|
||||
};
|
||||
|
||||
export default DmId;
|
8
src/pages/group/group-id.tsx
Normal file
8
src/pages/group/group-id.tsx
Normal file
|
@ -0,0 +1,8 @@
|
|||
import type { Component } from "solid-js";
|
||||
import type { RouteSectionProps } from "@solidjs/router";
|
||||
|
||||
export const GroupId: Component<RouteSectionProps> = (props) => {
|
||||
const groupId = () => Number(props.params.groupid);
|
||||
};
|
||||
|
||||
export default GroupId;
|
|
@ -2,4 +2,7 @@ import { lazy } from "solid-js";
|
|||
|
||||
export { Home } from "./home";
|
||||
|
||||
export const GroupId = lazy(() => import("./group/group-id"));
|
||||
export const DmId = lazy(() => import("./dm/dm-id"));
|
||||
|
||||
export const Overview = lazy(() => import("./overview"));
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
import { type Component, createResource, Show } from "solid-js";
|
||||
import { type Component, createEffect, createResource, Show } from "solid-js";
|
||||
import type { RouteSectionProps } from "@solidjs/router";
|
||||
|
||||
import { overallSentMessagesQuery, SELF_ID, threadOverviewQuery } from "~/db";
|
||||
import { allThreadsOverviewQuery, overallSentMessagesQuery, SELF_ID } from "~/db";
|
||||
|
||||
import { OverviewTable, type RoomOverview } from "./overview-table";
|
||||
|
||||
export const Overview: Component<RouteSectionProps> = () => {
|
||||
const [allSelfSentMessagesCount] = createResource(() => overallSentMessagesQuery(SELF_ID).executeTakeFirstOrThrow());
|
||||
const [allSelfSentMessagesCount] = createResource(() => overallSentMessagesQuery(SELF_ID));
|
||||
|
||||
const [roomOverview] = createResource<RoomOverview[]>(async () => {
|
||||
return (await threadOverviewQuery.execute()).map((row) => {
|
||||
return (await allThreadsOverviewQuery()).rows.map((row) => {
|
||||
const isGroup = row.title !== null;
|
||||
|
||||
console.log(row);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const name = (
|
||||
isGroup
|
||||
|
@ -33,13 +35,18 @@ export const Overview: Component<RouteSectionProps> = () => {
|
|||
});
|
||||
});
|
||||
|
||||
createEffect(() => {
|
||||
console.log(roomOverview());
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>All messages: {allSelfSentMessagesCount()?.message_count as number}</p>
|
||||
<p>All messages: {allSelfSentMessagesCount()?.messageCount as number}</p>
|
||||
<Show
|
||||
when={!roomOverview.loading}
|
||||
fallback="Loading..."
|
||||
>
|
||||
{/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */}
|
||||
<OverviewTable data={roomOverview()!} />;
|
||||
</Show>
|
||||
</div>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { type Component, createSignal, For, Match, Show, Switch } from "solid-js";
|
||||
import { useNavigate } from "@solidjs/router";
|
||||
|
||||
import {
|
||||
type ColumnFiltersState,
|
||||
|
@ -37,7 +38,7 @@ export interface RoomOverview {
|
|||
|
||||
const columnHelper = createColumnHelper<RoomOverview>();
|
||||
|
||||
const archivedFilterFn: FilterFn<RoomOverview> = (row, columnId, filterValue) => {
|
||||
const archivedFilterFn: FilterFn<RoomOverview> = (row, _columnId, filterValue) => {
|
||||
if (filterValue === true) {
|
||||
return true;
|
||||
}
|
||||
|
@ -241,6 +242,8 @@ export const OverviewTable = (props: OverviewTableProps) => {
|
|||
},
|
||||
});
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div class="flex flex-row items-center gap-x-4">
|
||||
|
@ -304,8 +307,14 @@ export const OverviewTable = (props: OverviewTableProps) => {
|
|||
<For each={table.getRowModel().rows}>
|
||||
{(row) => (
|
||||
<TableRow
|
||||
class="[&:last-of-type>td:first-of-type]:rounded-bl-md [&:last-of-type>td:last-of-type]:rounded-br-md"
|
||||
class="cursor-pointer [&:last-of-type>td:first-of-type]:rounded-bl-md [&:last-of-type>td:last-of-type]:rounded-br-md"
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
onClick={() => {
|
||||
const threadId = row.original.threadId;
|
||||
const isGroup = row.original.isGroup;
|
||||
|
||||
navigate(`/${isGroup ? "group" : "dm"}/${threadId.toString()}`);
|
||||
}}
|
||||
>
|
||||
<For each={row.getVisibleCells()}>
|
||||
{(cell) => (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue