-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Hits.tsx
156 lines (136 loc) · 4.26 KB
/
Hits.tsx
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { observer, useComputed } from '@legendapp/state/react';
import { Box, Group, SimpleGrid, Skeleton, Text } from '@mantine/core';
import { Link as NavLink } from '@remix-run/react';
import { useEffect, useRef } from 'react';
import { useInfiniteHits, useInstantSearch } from 'react-instantsearch';
import { useIsFontLoaded } from '@/hooks/useIsFontLoaded';
import { getPreviewText } from '@/utils/language/language';
import type { AlgoliaMetadata } from '@/utils/types';
import classes from './Hits.module.css';
import { type SearchState } from './observables';
import { Sort } from './Sort';
interface HitComponentProps {
state$: SearchState;
hit: AlgoliaMetadata;
}
interface InfiniteHitsProps {
state$: SearchState;
}
const HitComponent = observer(({ hit, state$ }: HitComponentProps) => {
const isFontLoaded = useIsFontLoaded(hit.family);
const display = state$.display.get();
const size = state$.size.get();
// Change preview text if hit.defSubset is not latin or if it's an ico
const isNotLatin =
hit.defSubset !== 'latin' ||
hit.category === 'icons' ||
hit.category === 'other';
// We want a unique preview text for each font if it's not latin
const currentPreview$ = useComputed(() => {
// If isNotLatin is true, update currentPreview to the correct preview text
if (state$.preview.inputView.get() === '' && isNotLatin) {
return getPreviewText(hit.defSubset, hit.objectID);
}
return state$.preview.value.get();
});
// Update the preview value to a language specific sentence
// if the language is changed and preset is not custom
state$.language.onChange((e) => {
if (state$.preview.label.get() !== 'Custom') {
state$.preview.value.set(getPreviewText(e.value, hit.objectID));
}
});
return (
<Box
renderRoot={({ ...others }) => (
<NavLink prefetch="intent" to={`/fonts/${hit.objectID}`} {...others} />
)}
className={classes.wrapper}
mih={{ base: '150px', sm: display === 'grid' ? '332px' : '150px' }}
>
<link
rel="stylesheet"
href={`https://cdn.jsdelivr.net/fontsource/css/${hit.objectID}@latest/index.css`}
/>
<Skeleton visible={!isFontLoaded}>
<Text
fz={size}
style={{ fontFamily: `"${hit.family}", "Fallback Outline"` }}
>
{currentPreview$.get()}
</Text>
</Skeleton>
<Group className={classes['text-group']}>
<Text fz={18} fw={700} component="span">
{hit.family}
</Text>
{hit.variable && (
<Text fz={15} fw={700} component="span">
Variable
</Text>
)}
</Group>
</Box>
);
});
interface HitsMapProps {
hits: AlgoliaMetadata[];
sentinelRef: React.MutableRefObject<HTMLDivElement | null>;
state$: SearchState;
}
const HitsMap = ({ hits, sentinelRef, state$ }: HitsMapProps) => {
return (
<>
{hits.map((hit) => (
<HitComponent key={hit.objectID} state$={state$} hit={hit} />
))}
<div ref={sentinelRef} aria-hidden="true" key="sentinel" />
</>
);
};
const InfiniteHits = observer(({ state$ }: InfiniteHitsProps) => {
const display = state$.display.get();
// Infinite Scrolling
const { results, indexUiState } = useInstantSearch();
const { hits, isLastPage, showMore } = useInfiniteHits<AlgoliaMetadata>();
const sentinelRef = useRef(null);
useEffect(() => {
if (sentinelRef.current !== null) {
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting && !isLastPage) {
showMore();
}
}
});
observer.observe(sentinelRef.current);
return () => {
observer.disconnect();
};
}
}, [isLastPage, showMore]);
// The `__isArtificial` flag makes sure to not display the No Results message
// when no hits have been returned yet.
if (!results.__isArtificial && results.nbHits === 0) {
return (
<Box>
<Text>No results found for "{indexUiState.query}"</Text>
</Box>
);
}
return (
<div id="hits">
<Sort state$={state$} count={results.nbHits} />
{display === 'grid' ? (
<SimpleGrid cols={{ base: 1, sm: 2, md: 3, xl: 4 }} spacing={16}>
<HitsMap state$={state$} hits={hits} sentinelRef={sentinelRef} />
</SimpleGrid>
) : (
<SimpleGrid cols={{ base: 1 }} spacing={16}>
<HitsMap state$={state$} hits={hits} sentinelRef={sentinelRef} />
</SimpleGrid>
)}
</div>
);
});
export { InfiniteHits };