Skip to content

Commit

Permalink
refactor!: rename tp fansubId and publisherId
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 14, 2023
1 parent 0aea7b5 commit 475959a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 51 deletions.
8 changes: 4 additions & 4 deletions packages/animegarden/src/garden/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export interface FilterOptions {
/**
* Filter by the group id of fansub
*/
fansub?: number | string;
fansubId?: number | string;

/**
* Filter by the user id of publisher
*/
publisher?: number | string;
publisherId?: number | string;

/**
* Filter by the resource type
Expand Down Expand Up @@ -58,12 +58,12 @@ export interface ResolvedFilterOptions {
/**
* Filter by the group id of fansub
*/
fansub?: number;
fansubId?: number;

/**
* Filter by the user id of publisher
*/
publisher?: number;
publisherId?: number;

/**
* Filter by the resource type
Expand Down
12 changes: 6 additions & 6 deletions packages/animegarden/src/garden/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const parser = {
.number()
.default(100)
.transform((ps) => Math.round(Math.max(1, Math.min(1000, ps)))),
fansub: z.coerce.number().optional(),
publisher: z.coerce.number().optional(),
fansubId: z.coerce.number().optional(),
publisherId: z.coerce.number().optional(),
type: z.coerce.string().optional(),
before: dateLike,
after: dateLike,
Expand Down Expand Up @@ -133,11 +133,11 @@ export function stringifySearchURL(baseURL: string, options: FilterOptions): URL
if (options.pageSize) {
url.searchParams.set('pageSize', '' + options.pageSize);
}
if (options.fansub) {
url.searchParams.set('fansub', '' + options.fansub);
if (options.fansubId) {
url.searchParams.set('fansubId', '' + options.fansubId);
}
if (options.publisher) {
url.searchParams.set('publisher', '' + options.publisher);
if (options.publisherId) {
url.searchParams.set('publisherId', '' + options.publisherId);
}
if (options.type) {
url.searchParams.set('type', '' + options.type);
Expand Down
41 changes: 10 additions & 31 deletions packages/app/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ import { histories, loading } from '../state';
});
}

const DMHY_RE =
/(?:https:\/\/share.dmhy.org\/topics\/view\/)?(\d+_[a-zA-Z0-9_\-]+\.html)/;
const DMHY_RE = /(?:https:\/\/share.dmhy.org\/topics\/view\/)?(\d+_[a-zA-Z0-9_\-]+\.html)/;

const useActiveElement = () => {
const [listenersReady, setListenersReady] = useState(false);
const [activeElement, setActiveElement] = useState(document.activeElement);

useEffect(() => {
const onFocus = (event: FocusEvent) =>
setActiveElement(event.target as any);
const onFocus = (event: FocusEvent) => setActiveElement(event.target as any);
const onBlur = (event: FocusEvent) => setActiveElement(null);

window.addEventListener('focus', onFocus, true);
Expand Down Expand Up @@ -133,10 +131,7 @@ export default function Search() {
// Filter old history item which is the substring of the current input
const oldHistories = histories.get().filter((o) => !target.includes(o));
// Remove duplicate items
const newHistories = [...new Set([target, ...oldHistories])].slice(
0,
10
);
const newHistories = [...new Set([target, ...oldHistories])].slice(0, 10);
// Set histories
histories.set(newHistories);

Expand Down Expand Up @@ -201,9 +196,7 @@ export default function Search() {
onMouseDown={() => selectGoToSearch()}
onSelect={() => selectGoToSearch()}
>
{DMHY_RE.test(input)
? `前往 ${input}`
: `在本页列出 ${input} 的搜索结果...`}
{DMHY_RE.test(input) ? `前往 ${input}` : `在本页列出 ${input} 的搜索结果...`}
</Command.Item>
<Command.Item
onSelect={() => {
Expand Down Expand Up @@ -233,12 +226,8 @@ export default function Search() {
<Command.Item
key={r.href}
value={r.href}
onMouseDown={selectStatic(
`/resource/${r.href.split('/').at(-1)}`
)}
onSelect={selectStatic(
`/resource/${r.href.split('/').at(-1)}`
)}
onMouseDown={selectStatic(`/resource/${r.href.split('/').at(-1)}`)}
onSelect={selectStatic(`/resource/${r.href.split('/').at(-1)}`)}
>
{r.title}
</Command.Item>
Expand Down Expand Up @@ -285,9 +274,7 @@ export default function Search() {
{filteredFansub.map((fansub) => (
<Command.Item
key={fansub.id}
onMouseDown={selectStatic(
`/resources/1?fansub=${fansub.id}`
)}
onMouseDown={selectStatic(`/resources/1?fansub=${fansub.id}`)}
onSelect={selectStatic(`/resources/1?fansub=${fansub.id}`)}
>
{fansub.name}
Expand Down Expand Up @@ -333,9 +320,7 @@ function parseSearch(search: string) {
fansub.push(found.id);
} else {
word = word.replace('樱', '桜');
const found = fansubs.find((t) =>
tradToSimple(t.name).includes(word)
);
const found = fansubs.find((t) => tradToSimple(t.name).includes(word));
if (found) {
fansub.push(found.id);
}
Expand Down Expand Up @@ -390,20 +375,14 @@ function goToSearch(search: string) {
if (match) {
goTo(`/resource/${match[1]}`);
} else {
const {
search: keywords,
include,
exclude,
fansub,
after
} = parseSearch(search);
const { search: keywords, include, exclude, fansub, after } = parseSearch(search);
const query = [
`search=${JSON.stringify(keywords)}`,
`include=${JSON.stringify(include)}`,
`exclude=${JSON.stringify(exclude)}`
];
if (fansub !== undefined) {
query.push(`fansub=${fansub}`);
query.push(`fansubId=${fansub}`);
}
if (after !== undefined) {
query.push(`after=${after.toISOString()}`);
Expand Down
14 changes: 7 additions & 7 deletions packages/app/src/pages/resources/[page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const page = _page ? +_page : 1;
const url = Astro.url;
const params = parseSearchURL(Astro.url.searchParams);
const publisher = params.publisher;
const fansub = params.fansub;
const fansubItem = fansubs.find((f) => f.id === fansub);
const publisherId = params.publisherId;
const fansubId = params.fansubId;
const fansub = fansubs.find((f) => f.id === fansubId);
const type = params.type && params.type in QueryType ? QueryType[params.type] : params.type;
const after = params.after;
const before = params.before;
Expand Down Expand Up @@ -55,17 +55,17 @@ function removeQuote(words: string[]) {
{
ok ? (
<>
{(search || include || before || after || fansubItem) && (
{(search || include || before || after || fansub) && (
<>
<div class="mb4 p4 w-full bg-gray-100 rounded-md space-y-2">
{fansubItem && (
{fansub && (
<div class="space-x-2 select-none text-0">
<span class="text-4 text-base-800 font-bold inline-block mr2">字幕组</span>
<a
href={`/resources/1?fansub=${fansubItem.id}`}
href={`/resources/1?fansub=${fansub.id}`}
class="text-4 select-text text-link"
>
{fansubItem.name}
{fansub.name}
</a>
</div>
)}
Expand Down
11 changes: 8 additions & 3 deletions packages/worker/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ export const getSearchResources = memoAsync(
const timer = createTimer(`Search Resources`);
timer.start();

const { page, pageSize, fansub, publisher, type, before, after, search, include, exclude } =
const { page, pageSize, fansubId, publisherId, type, before, after, search, include, exclude } =
options;

const result = await prisma.resource.findMany({
where: {
AND: [
{
type,
fansubId: fansub,
publisherId: publisher,
createdAt: {
gte: after,
lte: before
Expand All @@ -60,6 +59,12 @@ export const getSearchResources = memoAsync(
search: search && search.length > 0 ? search.join(' ') : undefined
}
},
{
fansubId: fansubId
},
{
publisherId: publisherId
},
{
AND: include?.map((arr) => ({
OR: arr.map((t) => ({ titleAlt: { contains: normalizeTitle(t) } }))
Expand Down

0 comments on commit 475959a

Please sign in to comment.