<commit_before>import { Anchor, Box, Col, Icon, Row, Text } from '@tlon/indigo-react';
import { Association, GraphNode, Group, Post } from '@urbit/api';
import bigInt from 'big-integer';
import React from 'react';
import GlobalApi from '~/logic/api/global';
import { referenceToPermalink } from '~/logic/lib/permalinks';
import { getSnippet } from '~/logic/lib/publish';
import { useGroupForAssoc } from '~/logic/state/group';
import Author from '~/views/components/Author';
import { MentionText } from '~/views/components/MentionText';
import { GraphContent } from '~/views/landscape/components/Graph/GraphContent';
import ChatMessage from '../chat/components/ChatMessage';
import { NotePreviewContent } from '../publish/components/NotePreview';
import { PermalinkEmbed } from './embed';

function TranscludedLinkNode(props: {
  node: GraphNode;
  assoc: Association;
  transcluded: number;
  api: GlobalApi;
}) {
  const { node, api, assoc, transcluded } = props;
  const idx = node.post.index.slice(1).split('/');

  switch (idx.length) {
    case 1:
    const [{ text }, link] = node.post.contents;
      if('reference' in link) {
        const permalink = referenceToPermalink(link).link;
        return <PermalinkEmbed transcluded={transcluded + 1} api={api} link={permalink} association={assoc} />;
      }

      return (
        <Box borderRadius="2" p="2" bg="scales.black05">
          <Anchor underline={false} target="_blank" color="black" href={link.url}>
            <Icon verticalAlign="bottom" mr="2" icon="ArrowExternal" />
            {text}
          </Anchor>
        </Box>
      );
    case 2:
      return (
        <TranscludedComment
          api={api}
          transcluded={transcluded}
          node={node}
          assoc={assoc}
        />
      );
    default:
      return null;
  }
}

function TranscludedComment(props: {
  node: GraphNode;
  assoc: Association;
  api: GlobalApi;
  transcluded: number;
}) {
  const { assoc, node, api, transcluded } = props;
  const group = useGroupForAssoc(assoc)!;

  const comment = node.children?.peekLargest()![1]!;
  return (
    <Col>
      <Author
        p="2"
        showImage
        ship={comment.post.author}
        date={comment.post?.['time-sent']}
        group={group}
      />
      <Box p="2">
        <GraphContent
          api={api}
          transcluded={transcluded}
          contents={comment.post.contents}
          showOurContact={false}
        />
      </Box>
    </Col>
  );
}

function TranscludedPublishNode(props: {
  node: GraphNode;
  assoc: Association;
  api: GlobalApi;
  transcluded: number;
}) {
  const { node, assoc, transcluded, api } = props;
  const group = useGroupForAssoc(assoc)!;
  const idx = node.post.index.slice(1).split('/');
  switch (idx.length) {
    case 1:
      const post = node.children
        ?.get(bigInt.one)
        ?.children?.peekLargest()?.[1]!;
      return (
        <Col color="black" gapY="2"><commit_msg>permalinks: fix crashing slice error<commit_after>import { Anchor, Box, Col, Icon, Row, Text } from '@tlon/indigo-react';
import { Association, GraphNode, Group, Post } from '@urbit/api';
import bigInt from 'big-integer';
import React from 'react';
import GlobalApi from '~/logic/api/global';
import { referenceToPermalink } from '~/logic/lib/permalinks';
import { getSnippet } from '~/logic/lib/publish';
import { useGroupForAssoc } from '~/logic/state/group';
import Author from '~/views/components/Author';
import { MentionText } from '~/views/components/MentionText';
import { GraphContent } from '~/views/landscape/components/Graph/GraphContent';
import ChatMessage from '../chat/components/ChatMessage';
import { NotePreviewContent } from '../publish/components/NotePreview';
import { PermalinkEmbed } from './embed';

function TranscludedLinkNode(props: {
  node: GraphNode;
  assoc: Association;
  transcluded: number;
  api: GlobalApi;
}) {
  const { node, api, assoc, transcluded } = props;
  const idx = node?.post.index.slice(1).split('/');

  switch (idx.length) {
    case 1:
    const [{ text }, link] = node.post.contents;
      if('reference' in link) {
        const permalink = referenceToPermalink(link).link;
        return <PermalinkEmbed transcluded={transcluded + 1} api={api} link={permalink} association={assoc} />;
      }

      return (
        <Box borderRadius="2" p="2" bg="scales.black05">
          <Anchor underline={false} target="_blank" color="black" href={link.url}>
            <Icon verticalAlign="bottom" mr="2" icon="ArrowExternal" />
            {text}
          </Anchor>
        </Box>
      );
    case 2:
      return (
        <TranscludedComment
          api={api}
          transcluded={transcluded}
          node={node}
          assoc={assoc}
        />
      );
    default:
      return null;
  }
}

function TranscludedComment(props: {
  node: GraphNode;
  assoc: Association;
  api: GlobalApi;
  transcluded: number;
}) {
  const { assoc, node, api, transcluded } = props;
  const group = useGroupForAssoc(assoc)!;

  const comment = node.children?.peekLargest()![1]!;
  return (
    <Col>
      <Author
        p="2"
        showImage
        ship={comment.post.author}
        date={comment.post?.['time-sent']}
        group={group}
      />
      <Box p="2">
        <GraphContent
          api={api}
          transcluded={transcluded}
          contents={comment.post.contents}
          showOurContact={false}
        />
      </Box>
    </Col>
  );
}

function TranscludedPublishNode(props: {
  node: GraphNode;
  assoc: Association;
  api: GlobalApi;
  transcluded: number;
}) {
  const { node, assoc, transcluded, api } = props;
  const group = useGroupForAssoc(assoc)!;
  const idx = node?.post.index.slice(1).split('/');
  switch (idx.length) {
    case 1:
      const post = node.children
        ?.get(bigInt.one)
        ?.children?.peekLargest()?.[1]!;
      return (
        <Col color="black" gapY="2">
{'fraction_non_alphanumeric': 0.14748019551660205, 'fraction_numerical': 0.0053935614360357325, 'mean_word_length': 2.865798045602606, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4151615', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1842, 'n_words': 415}