Skip to content

Commit

Permalink
feat: update kavita integration to update specific libraries, when sp…
Browse files Browse the repository at this point in the history
…ecified (#96)

* feat: Update kavita integration to update specific libraries, when specified

* fix: prisma migration

* feat: add command to run app in windows

* fix: kavita libraries setting should be an array
  • Loading branch information
ShivamAmin authored May 6, 2023
1 parent d9e0447 commit 9ccd7c0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"scripts": {
"dev": "NODE_ENV=development ts-node --project tsconfig.server.json src/server/index.ts",
"dev_windows": "set NODE_ENV=development&&ts-node --project tsconfig.server.json src/server/index.ts",
"build": "pnpm build:next && pnpm build:server",
"build:server": "tsc --project tsconfig.server.json",
"start:server": "NODE_ENV=production node dist/server/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Settings" ADD COLUMN "kavitaLibraries" TEXT[] DEFAULT ARRAY[]::TEXT[];
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ model Settings {
kavitaHost String?
kavitaUser String?
kavitaPassword String?
kavitaLibraries String[] @default([])
}
36 changes: 34 additions & 2 deletions src/components/settings/integration.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Accordion, Box, Breadcrumbs, createStyles, Group, Image, Text } from '@mantine/core';
import { trpc } from '../../utils/trpc';
import { SwitchItem, TextItem } from './mangal';
import { ArrayItem, SwitchItem, TextItem } from './mangal';

const useStyles = createStyles((theme) => ({
item: {
Expand Down Expand Up @@ -35,7 +35,7 @@ export function IntegrationSettings() {
const update = trpc.settings.update.useMutation();
const settings = trpc.settings.query.useQuery();

const handleUpdate = async (key: string, value: boolean | string | number) => {
const handleUpdate = async (key: string, value: boolean | string | number | string[]) => {
await update.mutateAsync({
key,
value,
Expand Down Expand Up @@ -299,6 +299,38 @@ export function IntegrationSettings() {
initialValue={settings.data.appConfig.kavitaPassword}
/>
</Group>
<Group position="apart" className={classes.item} spacing="xl" noWrap>
<Box>
<Breadcrumbs
separator="/"
styles={{
separator: {
marginLeft: 4,
marginRight: 4,
},
breadcrumb: {
textTransform: 'capitalize',
fontSize: 13,
fontWeight: 500,
},
root: {
marginBottom: 5,
},
}}
>
Libraries
</Breadcrumbs>
<Text size="xs" color="dimmed">
Scan specific Kavita libraries
</Text>
</Box>
<ArrayItem
configKey="kavitaLibraries"
onUpdate={handleUpdate}
initialValue={settings.data.appConfig.kavitaLibraries}
itemName="Library"
/>
</Group>
</Accordion.Panel>
</Accordion.Item>
</Accordion>
Expand Down
4 changes: 3 additions & 1 deletion src/components/settings/mangal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ export function ArrayItem({
configKey,
initialValue,
onUpdate,
itemName,
}: {
configKey: string;
initialValue: string[];
onUpdate: (configKey: string, value: string[]) => void;
itemName: string;
}) {
const [value, setValue] = useState<{ [key: string]: string }>(
initialValue.reduce((acc, v) => ({ ...acc, [nanoid()]: v }), {}),
Expand Down Expand Up @@ -202,7 +204,7 @@ export function ArrayItem({
);
})}
<Button fullWidth onClick={() => setValue({ ...value, [nanoid()]: '' })}>
Add Url
Add {itemName}
</Button>
</Box>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/settings/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export function NotificationSettings() {
configKey="appriseUrls"
onUpdate={handleUpdate}
initialValue={settings.data.appConfig.appriseUrls}
itemName="URL"
/>
</Group>
</Accordion.Panel>
Expand Down
19 changes: 12 additions & 7 deletions src/server/utils/integration/kavita.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prisma } from '../../db/client';

interface Library {
name: string;
id: string;
}

Expand Down Expand Up @@ -57,14 +58,18 @@ export const scanLibrary = async () => {
})
).json();

const includedLibraries = settings.kavitaLibraries;

await Promise.all(
libraries.map(async (library) => {
const kavitaLibraryUrl = new URL(`/api/Library/scan?libraryId=${library.id}&force=false`, baseKavitaUrl).href;
await fetch(kavitaLibraryUrl, {
method: 'POST',
headers,
});
}),
libraries
.filter((library) => (includedLibraries.length > 0 ? includedLibraries.includes(library.name) : library.name))
.map(async (library) => {
const kavitaLibraryUrl = new URL(`/api/Library/scan?libraryId=${library.id}&force=false`, baseKavitaUrl).href;
await fetch(kavitaLibraryUrl, {
method: 'POST',
headers,
});
}),
);
}
};
Expand Down

0 comments on commit 9ccd7c0

Please sign in to comment.