Skip to content

Commit

Permalink
feat: inbox set read and unread
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Oct 3, 2024
1 parent 6da197c commit 6da8bce
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 20 deletions.
14 changes: 12 additions & 2 deletions apps/renderer/src/hooks/biz/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,22 @@ export const useUnCollect = (entry: Nullable<CombinedEntryModel>) => {
export const useRead = () =>
useMutation({
mutationFn: async (entry: Nullable<CombinedEntryModel>) =>
entry && entryActions.markRead(entry.feeds.id, entry.entries.id, true),
entry &&
entryActions.markRead({
feedId: entry.feeds.id,
entryId: entry.entries.id,
read: true,
}),
})
export const useUnread = () =>
useMutation({
mutationFn: async (entry: Nullable<CombinedEntryModel>) =>
entry && entryActions.markRead(entry.feeds.id, entry.entries.id, false),
entry &&
entryActions.markRead({
feedId: entry.feeds.id,
entryId: entry.entries.id,
read: false,
}),
})

export const useEntryActions = ({
Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/src/modules/entry-column/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export function batchMarkRead(ids: string[]) {

if (batchLikeIds.length > 0) {
for (const [feedId, id] of batchLikeIds) {
entryActions.markRead(feedId, id, true)
entryActions.markRead({ feedId, entryId: id, read: true })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/src/modules/entry-column/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function EntryColumnImpl() {
const feedId = activeEntry?.feedId
if (!feedId) return

entryActions.markRead(feedId, activeEntryId, true)
entryActions.markRead({ feedId, entryId: activeEntryId, read: true })
}, [activeEntry?.feedId, activeEntryId, isCollection, isPendingEntry])

const isInteracted = useRef(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const EntryItemWrapper: FC<
if (!document.hasFocus()) return
if (asRead) return

entryActions.markRead(entry.feedId, entry.entries.id, true)
entryActions.markRead({ feedId: entry.feedId, entryId: entry.entries.id, read: true })
},
233,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const EntryListHeader: FC<{

const unreadOnly = useGeneralSettingKey("unreadOnly")

const { feedId, entryId, view } = routerParams
const { feedId, entryId, view, listId } = routerParams

const headerTitle = useFeedHeaderTitle()
const os = getOS()
Expand All @@ -58,13 +58,13 @@ export const EntryListHeader: FC<{
</div>
</div>
)
const { mutateAsync: refreshFeed, isPending } = useRefreshFeedMutation(routerParams.feedId)
const { mutateAsync: refreshFeed, isPending } = useRefreshFeedMutation(feedId)

const user = useWhoami()
const isOnline = useIsOnline()

const feed = useFeedById(routerParams.feedId)
const isList = feed?.type === "list"
const feed = useFeedById(feedId)
const isList = !!listId

const titleStyleBasedView = ["pl-12", "pl-7", "pl-7", "pl-7", "px-5", "pl-12"]

Expand Down
9 changes: 7 additions & 2 deletions apps/renderer/src/store/entry/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ export const getFilteredFeedIds = (feedIds: string[], filter?: EntryFilter) => {
}

const unread = create({
fetcher: async (ids: [FeedId, EntryId][]) => {
await apiClient.reads.$post({ json: { entryIds: ids.map((i) => i[1]) } })
fetcher: async (ids: ([FeedId, EntryId, boolean] | [FeedId, EntryId])[]) => {
await apiClient.reads.$post({
json: {
entryIds: ids.map((i) => i[1]),
isInbox: ids[0][2],
},
})

return []
},
Expand Down
10 changes: 6 additions & 4 deletions apps/renderer/src/store/entry/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ class EntryActions {
}))
}

async markRead(feedId: string, entryId: string, read: boolean) {
const currentIsRead = get().flatMapEntries[entryId]?.read
async markRead({ feedId, entryId, read }: { feedId: string; entryId: string; read: boolean }) {
const entry = get().flatMapEntries[entryId]
const isInbox = entry?.entries && "inboxHandle" in entry.entries

if (read && currentIsRead) {
if (read && entry?.read) {
return
}

Expand All @@ -362,11 +363,12 @@ class EntryActions {
// Send api request
async () => {
if (read) {
await internal_batchMarkRead([feedId, entryId])
await internal_batchMarkRead([feedId, entryId, isInbox])
} else {
await apiClient.reads.$delete({
json: {
entryId,
isInbox,
},
})
}
Expand Down
5 changes: 0 additions & 5 deletions apps/renderer/src/store/inbox/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { FeedViewType } from "~/lib/enum"
import type { InboxModel } from "~/models"

import { useInboxStore } from "./store"

export const useInboxById = (inboxId: Nullable<string>): InboxModel | null =>
useInboxStore((state) => (inboxId ? state.inboxes[inboxId] : null))

export const useInboxByView = (view: FeedViewType) => {
return useInboxStore((state) => (view === 0 ? Object.values(state.inboxes) : []))
}
27 changes: 27 additions & 0 deletions packages/shared/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,22 @@ declare const inboxesEntries: drizzle_orm_pg_core.PgTableWithColumns<{
baseColumn: never;
generated: undefined;
}, {}, {}>;
read: drizzle_orm_pg_core.PgColumn<{
name: "read";
tableName: "inboxes_entries";
dataType: "boolean";
columnType: "PgBoolean";
data: boolean;
driverParam: boolean;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
};
dialect: "pg";
}>;
Expand Down Expand Up @@ -2245,6 +2261,7 @@ declare const inboxesEntriesOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<
[key: string]: string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | any | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null;
} | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | (string | number | boolean | any | any | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null)[] | null>>;
inboxHandle: z.ZodString;
read: z.ZodNullable<z.ZodBoolean>;
}, "media" | "attachments" | "extra">, {
attachments: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
url: z.ZodString;
Expand Down Expand Up @@ -2327,6 +2344,7 @@ declare const inboxesEntriesOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<
authorAvatar: string | null;
insertedAt: string;
publishedAt: string;
read: boolean | null;
inboxHandle: string;
media?: {
type: "photo" | "video";
Expand Down Expand Up @@ -2363,6 +2381,7 @@ declare const inboxesEntriesOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<
authorAvatar: string | null;
insertedAt: string;
publishedAt: string;
read: boolean | null;
inboxHandle: string;
media?: {
type: "photo" | "video";
Expand Down Expand Up @@ -2415,6 +2434,7 @@ declare const inboxesEntriesInsertOpenAPISchema: z.ZodObject<z.objectUtil.extend
authorAvatar: z.ZodOptional<z.ZodNullable<z.ZodString>>;
insertedAt: z.ZodString;
publishedAt: z.ZodString;
read: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
inboxHandle: z.ZodString;
}, "id" | "media" | "attachments" | "extra" | "insertedAt" | "publishedAt" | "inboxHandle">, {
attachments: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
Expand Down Expand Up @@ -2519,6 +2539,7 @@ declare const inboxesEntriesInsertOpenAPISchema: z.ZodObject<z.objectUtil.extend
} | null | undefined;
authorUrl?: string | null | undefined;
authorAvatar?: string | null | undefined;
read?: boolean | null | undefined;
}, {
guid: string;
publishedAt: string;
Expand Down Expand Up @@ -2552,6 +2573,7 @@ declare const inboxesEntriesInsertOpenAPISchema: z.ZodObject<z.objectUtil.extend
} | null | undefined;
authorUrl?: string | null | undefined;
authorAvatar?: string | null | undefined;
read?: boolean | null | undefined;
}>;
declare const inboxesEntriesRelations: drizzle_orm.Relations<"inboxes_entries", {
inboxes: drizzle_orm.One<"inboxes", true>;
Expand Down Expand Up @@ -4219,6 +4241,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
} | null | undefined;
authorUrl?: string | null | undefined;
authorAvatar?: string | null | undefined;
read?: boolean | null | undefined;
};
};
output: {
Expand Down Expand Up @@ -5018,6 +5041,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
input: {
json: {
entryIds: string[];
isInbox?: boolean | undefined;
};
};
output: {
Expand All @@ -5030,6 +5054,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
input: {
json: {
entryId: string;
isInbox?: boolean | undefined;
};
};
output: {
Expand Down Expand Up @@ -5365,6 +5390,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
authorAvatar: string | null;
insertedAt: string;
publishedAt: string;
read: boolean | null;
inboxHandle: string;
media?: {
type: "photo" | "video";
Expand Down Expand Up @@ -5448,6 +5474,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
authorAvatar: string | null;
insertedAt: string;
publishedAt: string;
read: boolean | null;
inboxHandle: string;
media?: {
type: "photo" | "video";
Expand Down

0 comments on commit 6da8bce

Please sign in to comment.