aboutsummaryrefslogtreecommitdiff
path: root/web/src/Doc.tsx
blob: 44d120e7f180181e39ed470352d1df5cb7ee1928 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import z from 'zod';
import './Doc.css'

export interface ResultDoc {
    /** The document ID */
    id: number,
    /** The Score returned by pylucene */
    score: number,
    /** The domain of the result */
    domain: string,
    /** The full URL of the result */
    url: string,
    /** The title of the webpage */
    title: string,
    /** All the image URLs in the site */
    images: string[],
    /** The parsed content */
    content: string,
}

const resultDocSchema = z.object({
    id: z.number().int(),
    score: z.number(),
    domain: z.string(),
    url: z.string(),
    title: z.string(),
    images: z.string().array(),
    content: z.string(),
})

export function isResultDoc(doc: unknown): doc is ResultDoc {
    const result = resultDocSchema.safeParse(doc);
    if (!result.success) {
        console.error(result.error);
        return false;
    } else {
        return true;
    }
}

export interface DocParams {
    document: ResultDoc,
}

function Doc({document}: DocParams) {
    return (
        <div className="card">
            <div className="card-body">
                <h2 className='doc-title'>
                    <a href={document.url} target="_blank" rel="noreferrer noopener">
                        {document.title}
                    </a>
                </h2>
                <div className='doc-domain-score'>
                    <p className='doc-domain'>{document.domain}</p>
                    <p className='doc-domain'>Score: {(document.score).toFixed(2)}</p>
                </div>
            </div>
        </div>
    )
}

export default Doc;