Code Monkey home page Code Monkey logo

Comments (7)

silesky avatar silesky commented on July 20, 2024 2

This is done in #476! Thanks @yordis.

Now that we have a conventional monorepo -- if there are enhancements or specific packages you think are worth adding, please open up separate issues (or PRs)! FYI: now that we've laid the groundwork, we're currently working on breaking out node into a seperate package.

from analytics-next.

yordis avatar yordis commented on July 20, 2024 1

Do you need any help? By now I am in auto-pilot doing these grunt work 😄 happy to help

from analytics-next.

pooyaj avatar pooyaj commented on July 20, 2024

Hey @yordis, are you suggesting building a native React implementation ( e.g., a useSegmentAnalytics hook for example ) that abstracts away the library usage?

from analytics-next.

yordis avatar yordis commented on July 20, 2024

@pooyaj for the most part, my way to do things is "one function at the time, you make it declarative", that means, one function removed would allow me to be closer to bare-metal way to do things.

So yes, most of the time, they are more declarative abstractions for a given use-case, with given constraints that could tackle the 95% of the use cases, for the rest of 5%, just show people how to go one-level below.

Here is my working setup,

import * as React from 'react';
import { Analytics, AnalyticsBrowser, Context as TContext } from '@segment/analytics-next';
import { useQuery } from 'react-query';
import { AnalyticsSettings, InitOptions } from '@segment/analytics-next/src/analytics';
import { UseQueryOptions } from 'react-query/types/react/types';

type OmittedQueryOption =
  | 'queryKey'
  | 'select'
  | 'queryFn'
  | 'refetchOnMount'
  | 'refetchOnReconnect'
  | 'refetchOnWindowFocus';

type UseSegmentAnalyticsProps = {
  options?: Omit<
    UseQueryOptions<[Analytics, TContext], unknown, { analytics: Analytics; context: TContext }>,
    OmittedQueryOption
  >;
  settings: AnalyticsSettings;
  initOptions?: InitOptions;
};

function useSegmentAnalytics(props: UseSegmentAnalyticsProps) {
  const query = useQuery<[Analytics, TContext], unknown, { analytics: Analytics; context: TContext }>({
    ...(props.options ?? {}),
    queryKey: ['@segment/analytics-next/analytics-browser/load', props.settings.writeKey],
    refetchOnMount: false,
    refetchOnReconnect: false,
    refetchOnWindowFocus: false,
    select(data) {
      return {
        analytics: data[0],
        context: data[1],
      };
    },
    queryFn() {
      // @ts-ignore
      return AnalyticsBrowser.load(props.settings, props.initOptions ?? {});
    },
  });

  return {
    isReady: query.isSuccess,
    analytics: query.data?.analytics,
    context: query.data?.context,
  };
}

const Context = React.createContext<ReturnType<typeof useSegmentAnalytics>>(undefined);

export function useSegmentAnalyticsContext() {
  return React.useContext(Context);
}

type SegmentAnalyticsV2Props = React.PropsWithChildren<UseSegmentAnalyticsProps>;

export function SegmentAnalytics(props: SegmentAnalyticsV2Props) {
  const query = useSegmentAnalytics(props);
  return <Context.Provider value={query}>{props.children}</Context.Provider>;
}

Obviously ignore the React Query stuff, since we have it in our system, no point in dealing with isLoading, and isError and things like that, since React Query works with any Promise.

You would swap that with something basic, I can make it to work. Hopefully, you get the idea.

from analytics-next.

yordis avatar yordis commented on July 20, 2024

Also notice I had to do the following import:

import { AnalyticsSettings, InitOptions } from '@segment/analytics-next/src/analytics';

I was thinking eventually ones it is in prod, some ticket to expose that from @segment/analytics-next instead.

from analytics-next.

yordis avatar yordis commented on July 20, 2024

Alright, dont do what I did around React Query: #310 (comment)

If by any chance you need to reset the cache of React Query (for example when using logout), you will be asking for trouble.

from analytics-next.

silesky avatar silesky commented on July 20, 2024

@yordis This is on the roadmap for this quarter.

from analytics-next.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.