-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: local db init v2 Signed-off-by: Innei <i@innei.in> * feat: hydrate Signed-off-by: Innei <i@innei.in> * feat: setting Signed-off-by: Innei <i@innei.in> * fix: read color in dark mode Signed-off-by: Innei <i@innei.in> * chore: cleanup Signed-off-by: Innei <i@innei.in> * update Signed-off-by: Innei <i@innei.in> * update Signed-off-by: Innei <i@innei.in> * update Signed-off-by: Innei <i@innei.in> * feat: persist star data Signed-off-by: Innei <i@innei.in> * Update src/renderer/src/store/entry/hooks.ts --------- Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
52 changed files
with
800 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const LOCAL_DB_NAME = "FOLLOW_DB" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Dexie from "dexie" | ||
|
||
import { LOCAL_DB_NAME } from "./constants" | ||
import { | ||
dbSchemaV1, | ||
} from "./db_schema" | ||
import type { DB_Entry } from "./schemas/entry" | ||
import type { DBModel } from "./types" | ||
|
||
export interface LobeDBSchemaMap { | ||
|
||
entries: DB_Entry | ||
// TODO - Add more schemas here | ||
feeds: DB_Entry | ||
subscriptions: DB_Entry | ||
entryRelated: DB_Entry | ||
feedEntries: DB_Entry | ||
} | ||
|
||
// Define a local DB | ||
export class BrowserDB extends Dexie { | ||
public entries: BrowserDBTable<"entries"> | ||
public feeds: BrowserDBTable<"feeds"> | ||
public subscriptions: BrowserDBTable<"subscriptions"> | ||
public entryRelated: BrowserDBTable<"entryRelated"> | ||
public feedEntries: BrowserDBTable<"feedEntries"> | ||
|
||
constructor() { | ||
super(LOCAL_DB_NAME) | ||
this.version(1).stores(dbSchemaV1) | ||
|
||
this.entries = this.table("entries") | ||
this.feeds = this.table("feeds") | ||
this.subscriptions = this.table("subscriptions") | ||
this.entryRelated = this.table("entryRelated") | ||
this.feedEntries = this.table("feedEntries") | ||
} | ||
} | ||
|
||
export const browserDB = new BrowserDB() | ||
|
||
// ================================================ // | ||
// ================================================ // | ||
// ================================================ // | ||
// ================================================ // | ||
// ================================================ // | ||
|
||
// types helper | ||
export type BrowserDBSchema = { | ||
[t in keyof LobeDBSchemaMap]: { | ||
model: LobeDBSchemaMap[t] | ||
table: Dexie.Table<DBModel<LobeDBSchemaMap[t]>, string> | ||
}; | ||
} | ||
type BrowserDBTable<T extends keyof LobeDBSchemaMap> = BrowserDBSchema[T]["table"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const dbSchemaV1 = { | ||
entries: "&id", | ||
feeds: "&id", | ||
subscriptions: "&id", | ||
entryRelated: "&id", | ||
feedEntries: "&feedId", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createAtomHooks, jotaiStore } from "@renderer/lib/jotai" | ||
import { buildStorageNS } from "@renderer/lib/ns" | ||
import { atomWithStorage } from "jotai/utils" | ||
|
||
const SHOULD_USE_INDEXED_DB_KEY = buildStorageNS("shouldUseIndexedDB") | ||
|
||
export const [ | ||
__shouldUseIndexedDBAtom, | ||
useShouldUseIndexedDB, | ||
useShouldUseIndexedDBValue, | ||
useSetShouldUseIndexedDB, | ||
getShouldUseIndexedDB, | ||
setShouldUseIndexedDB, | ||
] = createAtomHooks(atomWithStorage(SHOULD_USE_INDEXED_DB_KEY, false)) | ||
|
||
export const subscribeShouldUseIndexedDB = (callback: (value: boolean) => void) => jotaiStore.sub(__shouldUseIndexedDBAtom, () => callback(getShouldUseIndexedDB())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./db" | ||
export * from "./hooks" | ||
export * from "./models" | ||
export * from "./schemas" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @see https://github.com/lobehub/lobe-chat/blob/adebf0a92167faad48581b0b0780cf8faeba362f/src/database/client/core/model.ts | ||
*/ | ||
|
||
import type Dexie from "dexie" | ||
import type { ZodObject } from "zod" | ||
|
||
import type { BrowserDB, BrowserDBSchema } from "./db" | ||
import { browserDB } from "./db" | ||
|
||
export class BaseModel< | ||
N extends keyof BrowserDBSchema = any, | ||
// T extends { id: string } = any, | ||
// T = BrowserDBSchema[N]["table"], | ||
> { | ||
protected readonly db: BrowserDB | ||
// used to data validation, but use now | ||
|
||
private readonly schema: ZodObject<any> | ||
private readonly _tableName: keyof BrowserDBSchema | ||
|
||
constructor(table: N, schema: ZodObject<any>, db = browserDB) { | ||
this.db = db | ||
this.schema = schema | ||
this._tableName = table | ||
} | ||
|
||
get table() { | ||
return this.db[this._tableName] as Dexie.Table | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseModel } from "../model" | ||
import { DB_EntrySchema } from "../schemas" | ||
|
||
class ModelStatic extends BaseModel<"entryRelated"> { | ||
constructor() { | ||
super("entryRelated", DB_EntrySchema) | ||
} | ||
} | ||
|
||
export const entryRelatedModel = new ModelStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseModel } from "../model" | ||
import { DB_EntrySchema } from "../schemas" | ||
|
||
class EntryModelStatic extends BaseModel<"entries"> { | ||
constructor() { | ||
super("entries", DB_EntrySchema) | ||
} | ||
} | ||
|
||
export const entryModel = new EntryModelStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseModel } from "../model" | ||
import { DB_EntrySchema } from "../schemas" | ||
|
||
class ModelStatic extends BaseModel<"feedEntries"> { | ||
constructor() { | ||
super("feedEntries", DB_EntrySchema) | ||
} | ||
} | ||
|
||
export const feedEntriesModel = new ModelStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseModel } from "../model" | ||
import { DB_EntrySchema } from "../schemas" | ||
|
||
class ModelStatic extends BaseModel<"feeds"> { | ||
constructor() { | ||
super("feeds", DB_EntrySchema) | ||
} | ||
} | ||
|
||
export const feedModel = new ModelStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./entry" | ||
export * from "./entry-related" | ||
export * from "./feed" | ||
export * from "./feed-entry" | ||
export * from "./subscription" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseModel } from "../model" | ||
import { DB_EntrySchema } from "../schemas" | ||
|
||
class ModelStatic extends BaseModel<"subscriptions"> { | ||
constructor() { | ||
super("subscriptions", DB_EntrySchema) | ||
} | ||
} | ||
|
||
export const subscriptionModel = new ModelStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from "zod" | ||
|
||
export const DB_EntrySchema = z.object({ | ||
id: z.string(), | ||
}) | ||
|
||
export type DB_Entry = z.infer<typeof DB_EntrySchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./entry" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type DBModel<T> = T & { | ||
createdAt: number | ||
id: string | ||
updatedAt: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { FEED_COLLECTION_LIST, levels } from "@renderer/lib/constants" | ||
import type { EntryModel } from "@renderer/models" | ||
import type { CombinedEntryModel } from "@renderer/models" | ||
|
||
import { useRouteParamsSelector } from "./useRouteParams" | ||
|
||
export function useAsRead(entry?: EntryModel) { | ||
return useRouteParamsSelector(({ feedId, level }) => { | ||
export function useAsRead(entry?: CombinedEntryModel) { | ||
return useRouteParamsSelector(() => { | ||
if (!entry) return false | ||
return entry.read && !(level === levels.folder && feedId === FEED_COLLECTION_LIST) | ||
return entry.read | ||
}, [entry?.read]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.