Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

3.9 KB
Raw Blame
import type { IServiceEvent, IServiceProfile } from '@openpanel/core';
import { TagIcon } from 'lucide-react';
import { parseAsStringEnum, useQueryState } from 'nuqs';
import { EmptyState } from '../empty-state';
import { WidgetButtons, WidgetHead } from '../overview/overview-widget';
import { KeyValueGrid } from '@/components/ui/key-value-grid';
import { Widget } from '@/components/widget';
import { cn } from '@/utils/cn';
import { formatDateTime } from '@/utils/date';

interface Props {
  profile: IServiceProfile;
}

export const ProfileProperties = ({ profile }: Props) => {
  const [tab, setTab] = useQueryState(
    'tab',
    parseAsStringEnum(['profile', 'properties']).withDefault('profile')
  );

  return (
    <Widget className="w-full">
      <WidgetHead>
        <div className="title">Profile</div>
        <WidgetButtons>
          {[
            {
              key: 'profile',
              btn: 'Profile',
            },
            {
              key: 'properties',
              btn: 'Properties',
            },
          ].map((w) => (
            <button
              className={cn(w.key === tab && 'active')}
              key={w.key}
              onClick={() => setTab(w.key as 'profile' | 'properties')}
              type="button"
            >
              {w.btn}
            </button>
          ))}
        </WidgetButtons>
      </WidgetHead>

      {tab === 'profile' && profile && (
        <KeyValueGrid
          className="border-0"
          columns={1}
          copyable
          data={[
            { name: 'id', value: profile.id },
            { name: 'firstName', value: profile.firstName },
            { name: 'lastName', value: profile.lastName },
            { name: 'email', value: profile.email },
            { name: 'isExternal', value: profile.isExternal ? 'Yes' : 'No' },
            {
              name: 'createdAt',
              value: formatDateTime(new Date(profile.createdAt)),
            },
            {
              name: 'lastSeenAt',
              value: formatDateTime(new Date(profile.lastSeenAt)),
            },
            ...(profile.properties.country
              ? [{ name: 'country', value: profile.properties.country }]
              : []),
            ...(profile.properties.city
              ? [{ name: 'city', value: profile.properties.city }]
              : []),
            ...(profile.properties.os
              ? [{ name: 'os', value: profile.properties.os }]
              : []),
            ...(profile.properties.browser
              ? [{ name: 'browser', value: profile.properties.browser }]
              : []),
            ...(profile.properties.device
              ? [{ name: 'device', value: profile.properties.device }]
              : []),
            ...(profile.properties.referrer_name
              ? [
                  {
                    name: 'referrerName',
                    value: profile.properties.referrer_name,
                  },
                ]
              : []),
          ].map((item) => ({
            ...item,
            event: {
              ...profile,
              ...profile.properties,
            } as unknown as IServiceEvent,
          }))}
        />
      )}

      {tab === 'properties' && profile && (
        <KeyValueGrid
          className="border-0"
          columns={1}
          copyable
          data={Object.entries(profile.properties)
            .filter(([, value]) => value !== undefined && value !== '')
            .map(([key, value]) => ({
              name: key,
              value,
              event: {
                ...profile,
                ...profile.properties,
              } as unknown as IServiceEvent,
            }))}
        />
      )}
      {!profile?.properties && (
        <EmptyState
          description="Properties you pass to identify show up here."
          icon={TagIcon}
          title="No properties yet"
          variant="inline"
        />
      )}
    </Widget>
  );
};