File size: 1,487 Bytes
5e93a53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Alert, Loader, Stack, Text } from "@mantine/core";
import { usePubSub } from "create-pubsub/react";
import { Suspense, lazy } from "react";
import { ErrorBoundary } from "react-error-boundary";
import { settingsPubSub } from "../../../modules/pubSub";

const TextSearchResults = lazy(() => import("./Textual/TextSearchResults"));
const ImageSearchResults = lazy(() => import("./Graphical/ImageSearchResults"));

const ErrorFallback = ({ error }: { error: Error }) => (
  <Alert color="red" title="Error loading search results">
    <Text size="sm">{error.message}</Text>
    <Text size="xs" c="dimmed">
      Please try refreshing the page.
    </Text>
  </Alert>
);

const LoadingFallback = () => (
  <Stack align="center" p="md">
    <Loader size="sm" />
    <Text size="sm" c="dimmed">
      Loading component, please wait...
    </Text>
  </Stack>
);

export default function SearchResultsSection() {
  const [settings] = usePubSub(settingsPubSub);

  const renderSearchResults = (
    Component: React.LazyExoticComponent<React.ComponentType>,
    enabled: boolean,
  ) =>
    enabled && (
      <ErrorBoundary FallbackComponent={ErrorFallback}>
        <Suspense fallback={<LoadingFallback />}>
          <Component />
        </Suspense>
      </ErrorBoundary>
    );

  return (
    <Stack gap="xl">
      {renderSearchResults(ImageSearchResults, settings.enableImageSearch)}
      {renderSearchResults(TextSearchResults, settings.enableTextSearch)}
    </Stack>
  );
}