lodash-es#take TypeScript Examples

The following examples show how to use lodash-es#take. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: hottest-latest.tsx    From bext with MIT License 6 votes vote down vote up
HottestLatest: FC = () => {
  const { latestUpdateList, metaMap } = useMeta();

  const { data, error, loading } = useRequest(async () => {
    const result = await fetchHottest;
    if (result.code !== 200) {
      throw new Error(result?.message);
    }
    return result.data as string[];
  });

  const hottestMetas = useMemo(() => {
    if (!data?.length) {
      return [];
    }
    return take(data.map((id) => metaMap[id]).filter(Boolean), 10);
  }, [data, metaMap]);

  const renderHottest = () => {
    if (loading) {
      return <Spinner className="py-4" />;
    }
    if (error) {
      return <div className="py-4 text-center">获取失败...</div>;
    }
    return <MetaList list={hottestMetas} />;
  };

  return (
    <>
      <Title>最近热门</Title>
      {renderHottest()}
      <Title>最近更新</Title>
      <MetaList list={latestUpdateList} />
    </>
  );
}
Example #2
Source File: meta.tsx    From bext with MIT License 5 votes vote down vote up
MetaProvider: FC = ({ children }) => {
  const { data, loading, error } = useRequest(async () => {
    const response = await fetch(config.meta, {
      method: 'GET',
    });
    return await response.json();
  });

  const { previewId } = usePreview(true);

  const value: MetaValue | undefined = useMemo(() => {
    if (!data) {
      return undefined;
    }
    const { metas: metaList, latestUpdate } = data.meta;
    const metaMap = Object.fromEntries(
      metaList.map((meta: Meta) => [meta.id, meta]),
    );
    const latestUpdateList: any[] = take(
      latestUpdate.map((id: string) => metaMap[id]),
      3,
    );
    if (previewId) {
      latestUpdateList.unshift({
        id: 'preview',
        name: '预览脚本',
        synopsis: '当前正处于预览模式,请点击预览脚本',
      });
    }

    const { metaTag, hash } = data;
    const tagList = metaTag;
    const tagMap = Object.fromEntries(
      tagList.map((tag: Tag) => [tag.name, tag]),
    );

    return {
      metaList,
      metaMap,
      latestUpdateList,
      tagList,
      tagMap,
      prefix: `https://cdn.staticaly.com/gh/ikkz/bext-meta@${hash}/public/meta`,
      hash,
    };
  }, [data, previewId]);

  const { notify } = useNotifications();

  useEffect(() => {
    if (previewId) {
      notify({
        message:
          '当前处于预览模式,可前往首页最近更新进入预览,点击左上角图标/前往"开发"退出预览模式',
        status: 'info',
      });
    }
  }, [previewId]);

  if (loading) {
    return <PageLoading />;
  }

  if (error || !value) {
    return <div>出错了...</div>;
  }

  return <MetaContext.Provider value={value}>{children}</MetaContext.Provider>;
}