|
| 1 | +import { useContext } from 'react'; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogPanel, |
| 5 | + DialogTitle, |
| 6 | + Disclosure, |
| 7 | + DisclosureButton, |
| 8 | + DisclosurePanel, |
| 9 | +} from '@headlessui/react'; |
| 10 | +import { ChevronDownIcon } from '@heroicons/react/20/solid'; |
| 11 | +import { XMarkIcon } from '@heroicons/react/24/outline'; |
| 12 | +import { AppContext } from 'context/AppContext'; |
| 13 | +import { AppActions } from 'context/Reducer'; |
| 14 | +import { getHistory } from 'services/storage'; |
| 15 | + |
| 16 | +const History: React.FC = () => { |
| 17 | + const { state, dispatch } = useContext(AppContext); |
| 18 | + |
| 19 | + const history = getHistory(); |
| 20 | + |
| 21 | + const makeKey = (item: HistoryItem, index: number) => `${item.date}_${index}`; |
| 22 | + |
| 23 | + const handleRestore = (item: HistoryItem) => () => { |
| 24 | + const payload = { |
| 25 | + codeSample: item.code, |
| 26 | + codeSampleName: '', |
| 27 | + }; |
| 28 | + |
| 29 | + dispatch({ type: AppActions.LOAD_CODE_SAMPLE, payload }); |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <Dialog |
| 34 | + open={state.historyOpen} |
| 35 | + onClose={() => dispatch({ type: AppActions.HIDE_HISTORY })} |
| 36 | + className="relative z-10" |
| 37 | + > |
| 38 | + <div className="fixed inset-0" /> |
| 39 | + |
| 40 | + <div className="fixed inset-0 overflow-hidden"> |
| 41 | + <div className="absolute inset-0 overflow-hidden"> |
| 42 | + <div className="pointer-events-none fixed inset-y-0 right-0 flex max-w-full pl-10 sm:pl-16"> |
| 43 | + <DialogPanel |
| 44 | + transition |
| 45 | + className="pointer-events-auto w-screen max-w-2xl transform transition duration-500 ease-in-out data-closed:translate-x-full sm:duration-700" |
| 46 | + > |
| 47 | + <div className="flex h-full flex-col overflow-y-scroll bg-gray-900 text-white py-6 shadow-xl"> |
| 48 | + <div className="px-4 sm:px-6"> |
| 49 | + <div className="flex items-start justify-between"> |
| 50 | + <DialogTitle className="text-base font-semibold text-white"> |
| 51 | + Code History |
| 52 | + </DialogTitle> |
| 53 | + <div className="ml-3 flex h-7 items-center"> |
| 54 | + <button |
| 55 | + data-testid="history-close-btn" |
| 56 | + type="button" |
| 57 | + onClick={() => |
| 58 | + dispatch({ type: AppActions.HIDE_HISTORY }) |
| 59 | + } |
| 60 | + className="relative rounded-md bg-gray-900 text-white hover:text-gray-100 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-hidden cursor-pointer" |
| 61 | + > |
| 62 | + <span className="absolute -inset-2.5" /> |
| 63 | + <span className="sr-only">Close</span> |
| 64 | + <XMarkIcon aria-hidden="true" className="size-6" /> |
| 65 | + </button> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + <div className="relative mt-6 flex-1 px-4 sm:px-6"> |
| 70 | + <div className="h-screen w-full"> |
| 71 | + <div |
| 72 | + className="w-full divide-y divide-white/5 rounded-xl bg-white/5" |
| 73 | + data-testid="history-container" |
| 74 | + > |
| 75 | + {[...history].reverse().map((item, index) => { |
| 76 | + return ( |
| 77 | + <Disclosure |
| 78 | + as="div" |
| 79 | + key={makeKey(item, index)} |
| 80 | + className="p-6" |
| 81 | + > |
| 82 | + <DisclosureButton className="group flex w-full items-center justify-between cursor-pointer"> |
| 83 | + <span className="text-sm/6 font-medium text-white group-data-hover:text-white/80"> |
| 84 | + ({item.date} #({index + 1})){' - '} |
| 85 | + {item.code.slice(0, 50)}... |
| 86 | + </span> |
| 87 | + <ChevronDownIcon className="size-5 fill-white/60 group-data-hover:fill-white/50 group-data-open:rotate-180" /> |
| 88 | + </DisclosureButton> |
| 89 | + <DisclosurePanel className="mt-2 text-sm/5 text-white/50"> |
| 90 | + <pre |
| 91 | + className=" |
| 92 | + bg-gray-800 text-green-200 rounded p-4 font-mono text-xs whitespace-pre-wrap break-words overflow-x-auto border border-gray-700 shadow-inner max-h-64" |
| 93 | + > |
| 94 | + {item.code} |
| 95 | + </pre> |
| 96 | + <div className="flex justify-end mt-4"> |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + className="bg-gray-900 text-green-200 rounded px-4 py-1 border border-gray-700 shadow hover:bg-gray-700 hover:text-green-100 focus:outline-none focus:ring-2 focus:ring-green-400 transition cursor-pointer" |
| 100 | + onClick={handleRestore(item)} |
| 101 | + > |
| 102 | + Restore |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + </DisclosurePanel> |
| 106 | + </Disclosure> |
| 107 | + ); |
| 108 | + })} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </DialogPanel> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </Dialog> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +export default History; |
0 commit comments