Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watch): add button to show more content on truncated tl;dr #40

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/app/watch/watchResource.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { FC, ReactNode } from 'react'
import {
FC,
MouseEvent,
ReactNode,
useLayoutEffect,
useRef,
useState,
} from 'react'
import { tv } from 'tailwind-variants'

import Badge from '@/components/badge/Badge'
Expand Down Expand Up @@ -89,11 +96,16 @@ const Tldr: FC<{ parts: WatchResource['tldr']; query?: string }> = ({
parts,
query,
}) => {
function endsWithPunctuation(str: string): boolean {
const punctuationRegex = /[.!?]$/
return punctuationRegex.test(str)
}

return (
<span>
{parts?.map((part, index) => {
const isLast = index + 1 === parts.length
const needsDot = isLast && !part.plain_text.endsWith('.')
const needsDot = isLast && !endsWithPunctuation(part.plain_text)
return (
<span
key={`${part.plain_text}_${index}`}
Expand All @@ -115,8 +127,29 @@ const Tldr: FC<{ parts: WatchResource['tldr']; query?: string }> = ({
}

const WatchResource: FC<Props> = ({ resource, query, truncate = true }) => {
const [isTruncated, setIsTruncated] = useState(false)
const [showMore, setShowMore] = useState(false)

const { title, tldr, source, subSource, url, type, shiny } = resource

const ref = useRef<HTMLDivElement>(null)

useLayoutEffect(() => {
const { offsetHeight, scrollHeight } = ref.current || {}

if (offsetHeight && scrollHeight && offsetHeight < scrollHeight) {
setIsTruncated(true)
} else {
setIsTruncated(false)
}
}, [ref])

const onShowMoreClick = (e: MouseEvent<HTMLButtonElement>): void => {
e.preventDefault()
setShowMore(true)
setIsTruncated(false)
}

return (
<div className="group relative flex">
{shiny && (
Expand All @@ -129,16 +162,28 @@ const WatchResource: FC<Props> = ({ resource, query, truncate = true }) => {
className="relative z-10 flex w-full max-w-sm flex-col justify-between overflow-hidden rounded-lg border border-gray-200 bg-white p-4 shadow hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700"
>
<div>
<h5 className={resourceTitle({ truncate })}>
<h5 className={resourceTitle({ truncate })} title={title}>
{query ? highlightQuery(title, query) : title}
</h5>
<div className="grid grid-cols-6 gap-4 font-normal text-gray-700 dark:text-gray-400">
<div className={resourceTldr({ truncate })}>
<div
className={resourceTldr({ truncate: truncate && !showMore })}
ref={ref}
>
<Tldr parts={tldr} query={query} />
</div>
</div>
</div>

{isTruncated && (
<button
className="inline-block w-fit rounded pt-2.5 text-left text-xs font-medium leading-normal text-gray-400 hover:text-gray-600 focus:text-primary-600 focus:outline-none focus:ring-0 motion-reduce:transition-none dark:text-gray-500 dark:hover:text-gray-200"
onClick={onShowMoreClick}
>
Show more...
</button>
)}

<div className="mt-3 md:mt-6">
<Badge className="mr-2 inline group-hover:bg-white dark:group-hover:bg-white/10">
{source}
Expand Down
1 change: 0 additions & 1 deletion src/components/theme-switch/ThemeSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const ThemeSwitch: FC = () => {
}, [])

useEffect(() => {
console.log('📍 theme', theme)
// If the theme is `dark` or `system` (and system theme is `dark`)
if (theme === 'dark' || (theme === undefined && isSystemDark)) {
document.documentElement.classList.add('dark')
Expand Down