Skip to content

Commit

Permalink
feat: add local records in search panel
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 17, 2024
1 parent 66ac876 commit b96adb2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 44 deletions.
22 changes: 22 additions & 0 deletions src/renderer/src/components/common/ExPromise.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
useLayoutEffect,
useState,
} from "react"

const NOT_RESOLVED = Symbol("NOT_RESOLVED")
export const ExPromise = <T,>({
children,
promise,
}: {
promise: Promise<T>
children: (value: T) => JSX.Element
}) => {
// use() is a hook that returns the value of the promise, but in react 19

const [value, setValue] = useState<T | symbol>(NOT_RESOLVED)
useLayoutEffect(() => {
promise.then(setValue)
}, [promise])

return value === NOT_RESOLVED ? null : children(value as T)
}
45 changes: 27 additions & 18 deletions src/renderer/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2398,7 +2398,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
name: string | null;
id: string;
Expand All @@ -2408,6 +2407,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
handle: string | null;
createdAt: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2418,8 +2418,8 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
$post: {
input: {};
output: {
code: 0;
data: string;
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -2450,10 +2450,10 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
transactionHash: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2473,7 +2473,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
type: "tip" | "mint" | "burn" | "withdraw";
createdAt: string;
Expand Down Expand Up @@ -2518,6 +2517,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
ownerUserId: string | null;
} | null;
}[];
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2527,10 +2527,10 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
$post: {
input: {};
output: {
code: 0;
data: {
transactionHash: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2540,10 +2540,10 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
$get: {
input: {};
output: {
code: 0;
data: {
ttl: number;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2557,12 +2557,12 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
title: string | null;
id: string;
powerToken: string;
}[];
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2576,10 +2576,10 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
transactionHash: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -2594,7 +2594,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
createdAt: string;
userId: string;
Expand All @@ -2603,14 +2602,14 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
dailyPowerToken: string;
cashablePowerToken: string;
}[];
code: 0;
};
outputFormat: "json";
status: 200;
};
$post: {
input: {};
output: {
code: 0;
data: {
createdAt: string;
userId: string;
Expand All @@ -2619,6 +2618,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
dailyPowerToken: string;
cashablePowerToken: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -2761,10 +2761,10 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
[x: string]: number;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -2795,8 +2795,8 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: boolean;
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -2876,13 +2876,13 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
json: string;
xml: string;
description: string;
content: string;
json: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -2911,7 +2911,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
readCount: number;
feed: {
Expand Down Expand Up @@ -2940,6 +2939,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
isPrivate: boolean;
} | undefined;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -3056,12 +3056,12 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
$post: {
input: {};
output: {
code: 0;
data: {
userId: string;
sessionToken: string;
expires: string;
};
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down Expand Up @@ -3092,6 +3092,15 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
status: hono_utils_http_status.StatusCode;
};
};
} & {
[x: `/entries/read-histories/${string}`]: {
[x: `$${Lowercase<string>}`]: {
input: Partial<hono.ValidationTargets>;
output: any;
outputFormat: string;
status: hono_utils_http_status.StatusCode;
};
};
} & {
"/entries": {
$post: {
Expand Down Expand Up @@ -3247,7 +3256,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
description: string | null;
title: string | null;
Expand All @@ -3270,6 +3278,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
title?: string | undefined;
}[] | null | undefined;
}[];
code: 0;
};
outputFormat: "json";
status: 200;
Expand All @@ -3285,7 +3294,6 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
};
};
output: {
code: 0;
data: {
title: string | null;
userId: string;
Expand All @@ -3310,6 +3318,7 @@ declare const _routes: hono_hono_base.HonoBase<hono_types.BlankEnv, {
category: string | null;
isPrivate: boolean;
}[];
code: 0;
};
outputFormat: "json";
status: 200;
Expand Down
82 changes: 57 additions & 25 deletions src/renderer/src/modules/search/cmdk.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setAppSearchOpen, useAppSearchOpen } from "@renderer/atoms/app"
import { ExPromise } from "@renderer/components/common/ExPromise"
import { LoadMoreIndicator } from "@renderer/components/common/LoadMoreIndicator"
import { EmptyIcon } from "@renderer/components/icons/empty"
import { Logo } from "@renderer/components/icons/logo"
Expand Down Expand Up @@ -171,7 +172,11 @@ export const SearchCmdK: React.FC = () => {
return (
<SearchItem
key={entry.item.id}
view={feed?.id ? getSubscriptionByFeedId(feed.id)?.view : undefined}
view={
feed?.id ?
getSubscriptionByFeedId(feed.id)?.view :
undefined
}
title={entry.item.title!}
feedId={entry.feedId}
entryId={entry.item.id}
Expand Down Expand Up @@ -265,12 +270,8 @@ const SearchItem = memo(function Item({
}}
>
<div className="relative z-10 flex w-full items-center justify-between px-1 py-2">
{icon && (
<SiteIcon className="mr-2 size-5 shrink-0" url={icon} />
)}
<span className="block min-w-0 flex-1 shrink-0 truncate">
{title}
</span>
{icon && <SiteIcon className="mr-2 size-5 shrink-0" url={icon} />}
<span className="block min-w-0 flex-1 shrink-0 truncate">{title}</span>
<span className="block min-w-0 shrink-0 grow-0 text-xs font-medium text-zinc-800 opacity-60 dark:text-slate-200/80">
{subtitle}
</span>
Expand All @@ -292,26 +293,57 @@ const SearchGroupHeading: FC<{ icon: string, title: string }> = ({
const SearchResultCount: FC<{
count?: number
}> = ({ count }) => {
const searchInstance = React.useContext(SearchCmdKContext)
const hasKeyword = useSearchStore((s) => !!s.keyword)
if (!count) return null
const searchType = useSearchStore((s) => s.searchType)
const recordCountPromise = useMemo(async () => {
let count = 0
const counts = await searchInstance?.then((s) => s.counts)
if (!counts) return 0
if (searchType & SearchType.Entry) {
count += counts.entries || 0
}
if (searchType & SearchType.Feed) {
count += counts.feeds || 0
}
if (searchType & SearchType.Subscription) {
count += counts.subscriptions || 0
}
return count
}, [searchInstance, searchType])

return (
hasKeyword && (
<Tooltip>
<TooltipTrigger asChild>
<small className="center absolute bottom-3 right-3 shrink-0 gap-1 opacity-80">
{count}
{" "}
{pluralize("result", count)}
{" "}
(Local mode)
<i className="i-mingcute-question-line" />
</small>
</TooltipTrigger>
<TooltipContent>
This search run on local database, the result may not be up-to-date.
</TooltipContent>
</Tooltip>
)
<Tooltip>
<TooltipTrigger asChild>
<small className="center absolute bottom-3 right-3 shrink-0 gap-1 opacity-80">
{hasKeyword ? (
<span>
{count}
{" "}
{pluralize("result", count || 0)}
</span>
) : (
<ExPromise promise={recordCountPromise}>
{(count) => (
<>
{count}
{" "}
local
{" "}
{pluralize("record", count)}
</>
)}
</ExPromise>
)}
{" "}
(Local mode)
<i className="i-mingcute-question-line" />
</small>
</TooltipTrigger>
<TooltipContent>
This search run on local database, the result may not be up-to-date.
</TooltipContent>
</Tooltip>
)
}
const SearchOptions: Component = memo(({ children }) => {
Expand Down
Loading

0 comments on commit b96adb2

Please sign in to comment.