Skip to content

Commit

Permalink
feat: manage category added
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalkapoor committed Mar 8, 2023
1 parent 40b7044 commit c2131b4
Show file tree
Hide file tree
Showing 23 changed files with 590 additions and 50 deletions.
10 changes: 5 additions & 5 deletions source/Background/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import { browser } from 'webextension-polyfill-ts'
import { getDomainName } from '../utils/getDomain'
import { getIcon } from '../utils/geticon'

// create context menu

const createSubmenu = async () => {
const categoryList = await browser.storage.local.get('shortcutCategoryList')

await categoryList.shortcutCategoryList.forEach((category: string) => {
browser.contextMenus.create({
id: category,
Expand All @@ -23,8 +20,11 @@ browser.contextMenus.create({
title: 'Save to Kairy',
contexts: ['link'],
})

createSubmenu()
browser.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'local' && changes.shortcutCategoryList) {
createSubmenu()
}
})

// listen for context menu click
browser.contextMenus.onClicked.addListener((info: any, tab) => {
Expand Down
154 changes: 154 additions & 0 deletions source/Dashboard/components/Accordion/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import Tippy from '@tippyjs/react'
import React, { useContext, useEffect, useRef, useState } from 'react'
import { browser } from 'webextension-polyfill-ts'
import { ContextProvider } from '../../context/context'
import { Shortcut } from '../../reducers/types'
import AddCustomShortcut from '../AddCustomShortcut'
import styles from './styles.module.scss'

interface IProps {
title: string
content: string

index: number
}

const Accordion: React.FC<IProps> = (props) => {
const [active, setActive] = useState(props?.index == 0 ? true : false)
const { state, dispatch } = useContext(ContextProvider)
const content: any = useRef(null)
const [height, setHeight] = useState('0px')
const [isAddShortcutVisible, setIsAddShortcutVisible] = useState(false)
const [isEditShortcutVisible, setIsEditShortcutVisible] = useState(false)
const [selectedShortcut, setSelectedShortcut] = useState<Shortcut | null>(null)

useEffect(() => {
setHeight(active ? `${content.current?.scrollHeight}px` : '0px')
}, [])

function toggleAccordion() {
setActive(!active)
setHeight(active ? '0px' : `${content.current?.scrollHeight}px`)
}
const changeCategoryTitle = (oldName: string, newName: string) => {
let shortcutList = state.shortcutList.map((item) => {
if (item.category == oldName) {
item.category = newName
}
return item
})

let categoryList = state.shortcutCategoryList.map((item) => {
if (item == oldName) {
item = newName
}
return item
})

dispatch({ type: 'SET_SHORTCUT_LIST', payload: shortcutList })
dispatch({ type: 'SET_SHORTCUT_CATEGORY_LIST', payload: categoryList })
browser.storage.local.set({
shortcutCategoryList: categoryList,
shortcutList: shortcutList,
})
}
const handleEditShortcut = (shortcut: Shortcut) => {
console.log(shortcut, 'clicked shortcut')
setSelectedShortcut(shortcut)
setIsEditShortcutVisible(true)
}
return (
<>
{isAddShortcutVisible && (
<AddCustomShortcut setIsAddShortcutVisible={setIsAddShortcutVisible} />
)}
{selectedShortcut !== null && isEditShortcutVisible && (
<AddCustomShortcut
setIsAddShortcutVisible={setIsEditShortcutVisible}
shortcutData={selectedShortcut}
/>
)}

<div className={styles.accordionSection}>
<div className={`${styles.accordion} ${active ? styles.active : ''}`}>
<div className={styles.icon} onClick={toggleAccordion}>
<span className={styles.accordionIcon}>{active ? '-' : '+'}</span>
</div>

<div className={styles.titleHeader}>
<Tippy
content={
<span className={styles.tooltip}>
Double click to Edit {props.title}
</span>
}
placement='bottom-start'
arrow={false}
>
<input
type='text'
className={styles.accordionTitle}
defaultValue={props.title}
readOnly
onDoubleClick={(e) => {
e.currentTarget.readOnly = false
e.currentTarget.classList.add(styles.selectedInput)
}}
onBlur={(e) => {
e.currentTarget.readOnly = true
e.currentTarget.classList.remove(styles.selectedInput)
changeCategoryTitle(props.title, e.target.value)
}}
/>
</Tippy>
</div>
</div>
<div
ref={content}
style={{ maxHeight: `${height}` }}
className={styles.accordionContent}
>
<div className={styles.accordionText}>
{state.shortcutList.map((shortcut, index) => {
if (shortcut.category == props.title) {
return (
<div key={index}>
<Tippy
content={
<span className={styles.tooltip}>Edit {shortcut.title}</span>
}
placement='right-end'
arrow={false}
>
<div
className={styles.shortcutItem}
onClick={() => {
handleEditShortcut(shortcut)
}}
>
<img src={shortcut.icon} />
<p>{shortcut.title}</p>
</div>
</Tippy>
</div>
)
}
return null
})}

<div
className={styles.shortcutItem}
onClick={() => {
setIsAddShortcutVisible(true)
}}
>
<p>+</p>
</div>
</div>
</div>
</div>
</>
)
}

export default Accordion
141 changes: 141 additions & 0 deletions source/Dashboard/components/Accordion/styles.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
@import '../../../styles/fonts';
@import '../../../styles/variables';
@import '../../../styles/reset';

.accordionSection {
display: flex;
flex-direction: column;
width: 100%;
margin: 1rem 0;

.accordion {
display: grid;
grid-template-columns: 1fr 25fr;
align-items: flex-start;
border: none;
outline: none;
transition: background-color 0.3s ease-in-out;
cursor: pointer;

.icon {
display: flex;
justify-content: center;
align-items: center;
width: 24px;
height: 24px;
background-color: #133551;
color: #fff;
border-radius: 50%;
margin-top: 6px;

.accordionIcon {
font-size: 1.5rem;
font-weight: 500;
}
}
.titleHeader {
display: flex;
justify-content: space-between;
align-items: center;
.accordionTitle {
font-family: 'Poppins';
font-style: normal;
font-weight: 400;
font-size: 1.2rem;
line-height: 30px;
margin-left: 0.5rem;
padding-left: 0;
text-align: left;
background-color: transparent;
color: $primary-heading;
width: 80%;
padding-inline: 0.5rem;
border-radius: 8px;
margin-bottom: 0.5rem;
user-select: none;
&:hover {
background-color: rgba(128, 128, 128, 0.181);
}
}
.selectedInput {
border: 1px solid $primary-border;
}
}
}

.accordionContent {
width: 100%;
height: auto;
overflow: hidden;
transition: max-height 0.3s ease-in-out;
padding-inline: 2rem;

.accordionText {
max-width: 1000px;
font-family: 'Poppins';
font-style: normal;
font-weight: 300;
font-size: 1.2rem;
line-height: 32px;
margin-left: 5px;
color: $primary-text;
display: flex;
flex-wrap: wrap;
.shortcutItem {
display: flex;
align-items: center;
justify-content: center;
width: fit-content;
padding: 0.2rem 0.5rem;
border: 0.5px solid $primary-border;
border-radius: 8px;
margin-inline: 0.4rem;
margin-block: 0.4rem;

&:hover {
cursor: pointer;
background-color: rgba(128, 128, 128, 0.263);
}
p {
margin: 0 0.4rem;
}
}
}
span {
color: #fff;
font-size: 20px;
font-weight: 800;
}
}
}

@media only screen and (max-width: 600px) {
.accordionSection {
padding-inline: 2rem;
margin: 1rem 0;

.accordion {
width: 100%;
gap: 1rem;

.icon {
width: 20px;
height: 20px;
.accordionIcon {
padding: 4px;
}
}
.accordionTitle {
font-size: 1rem;
}
}
.accordionContent {
padding-inline: 0rem;
.accordionText {
font-size: 1rem;
text-align: start;
line-height: 25px;
}
}
}
}
12 changes: 10 additions & 2 deletions source/Dashboard/components/AddCustomShortcut/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Shortcut } from '../../reducers/types'
import ShortcutCard from '../ShortcutCard'
import styles from './styles.module.scss'
import { motion } from 'framer-motion'
import { browser } from 'webextension-polyfill-ts'

const variants = {
enter: {
Expand Down Expand Up @@ -41,14 +42,18 @@ const AddCustomShortcut: React.FC<IAddCustomShortcutProps> = ({
const [category, setCategory] = useState('all')
const [id, setId] = useState(nanoid())
const [icon, setIcon] = useState('')
const [isPinned, setIsPinned] = useState(false)
const mainRef = useRef() as React.MutableRefObject<HTMLInputElement>
useEffect(() => {
console.log('shortcutData', shortcutData)

if (shortcutData) {
setTitle(shortcutData.title)
setUrl(shortcutData.url)
setCategory(shortcutData.category)
setId(shortcutData.id)
setIcon(shortcutData.icon)
setIsPinned(shortcutData.isPinned)
}

function handleClickOutside(event: any) {
Expand All @@ -75,7 +80,7 @@ const AddCustomShortcut: React.FC<IAddCustomShortcutProps> = ({
category: category,
icon: icon || `https://www.google.com/s2/favicons?domain=${url}`,
modifiedOn: new Date().getTime().toString(),
isPinned: false,
isPinned: isPinned,
}
if (shortcutData) {
const newShortcutList = state.shortcutList.map((shortcut) => {
Expand All @@ -88,13 +93,16 @@ const AddCustomShortcut: React.FC<IAddCustomShortcutProps> = ({
type: 'SET_SHORTCUT_LIST',
payload: newShortcutList,
})
browser.storage.local.set({ shortcutList: newShortcutList })
} else {
dispatch({
type: 'SET_SHORTCUT_LIST',
payload: [...state.shortcutList, updatedShortcut],
})
browser.storage.local.set({
shortcutList: [...state.shortcutList, updatedShortcut],
})
}
console.log('saved')
setIsAddShortcutVisible(false)
}
}
Expand Down
Loading

0 comments on commit c2131b4

Please sign in to comment.