Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
debabin committed Jun 28, 2024
2 parents a9b9853 + b3f412f commit c40ed1c
Show file tree
Hide file tree
Showing 76 changed files with 172 additions and 19 deletions.
30 changes: 20 additions & 10 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { fileURLToPath } from 'node:url';
import type { DefaultTheme } from 'vitepress';
import { defineConfig } from 'vitepress';

import { getHookItems } from '../src/utils';

export default async () => {
const sidebarHookItems = await getHookItems();
const hookItems = await getHookItems();
const sidebarHookItems = hookItems.reduce<DefaultTheme.SidebarItem[]>((categoryItems, hookItem) => {
const category = categoryItems.find((group) => group.text === hookItem.category);

if (!category) {
categoryItems.push({ text: hookItem.category, items: [hookItem] });
} else {
category.items!.push(hookItem);
}

return categoryItems;
}, []);
const homePageFeatures = hookItems.map((item) => ({
title: item.text,
details: item.description,
link: item.link
}));

return defineConfig({
base: '/reactuse/',
Expand All @@ -19,11 +36,7 @@ export default async () => {
},
transformPageData: (pageData) => {
if (pageData.relativePath === 'index.md') {
pageData.frontmatter.features = sidebarHookItems.map((item) => ({
title: item.text,
details: item.description,
link: item.link
}))
pageData.frontmatter.features = homePageFeatures
}

if (pageData.relativePath.includes('hooks')) {
Expand Down Expand Up @@ -73,10 +86,7 @@ export default async () => {
text: 'Getting started',
link: '/getting-started'
},
{
text: 'Hooks',
items: [...sidebarHookItems]
}
...sidebarHookItems
]
}
}
Expand Down
3 changes: 3 additions & 0 deletions docs/functions/hooks/[name].md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<script setup>
import Meta from '../../src/components/meta.vue'
import Api from '../../src/components/api.vue'
import Demo from '../../src/components/demo.vue'
import Contributors from '../../src/components/contributors.vue'
</script>

# {{ $params.name }}

<Meta :last-modified="$params.lastModified" :category="$params.category" />

{{ $params.description }}

```typescript-vue
Expand Down
6 changes: 4 additions & 2 deletions docs/functions/hooks/[name].paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default {

const params = await Promise.all(
hooks.map(async (hook) => {
const file = await getHookFile(hook);
const jsdocMatch = matchJsdoc(file);
const { content, stats } = await getHookFile(hook);
const jsdocMatch = matchJsdoc(content);

if (!jsdocMatch) {
console.error(`No jsdoc comment found for ${hook}`);
Expand All @@ -26,6 +26,8 @@ export default {
id: hook,
name: hook,
description: jsdoc.description.description,
category: jsdoc.category?.name,
lastModified: stats.mtime.getTime(),
usage: jsdoc.usage.description,
apiParameters: jsdoc.apiParameters
}
Expand Down
62 changes: 62 additions & 0 deletions docs/src/components/meta.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<script setup>
const props = defineProps({
lastModified: {
type: Number
},
category: {
type: String
}
});
const timeAgo = (timestamp, locale = 'en') => {
let value;
const diff = Math.floor((new Date().getTime() - timestamp) / 1000);
const minutes = Math.floor(diff / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const months = Math.floor(days / 30);
const years = Math.floor(months / 12);
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
if (years > 0) {
value = rtf.format(0 - years, "year");
} else if (months > 0) {
value = rtf.format(0 - months, "month");
} else if (days > 0) {
value = rtf.format(0 - days, "day");
} else if (hours > 0) {
value = rtf.format(0 - hours, "hour");
} else if (minutes > 0) {
value = rtf.format(0 - minutes, "minute");
} else {
value = rtf.format(0 - diff, "second");
}
return value;
}
</script>

<template>
<div class="meta">
<template v-if="category">
<div>Category</div>
<div><code>{{ category }}</code></div>
</template>
<ClientOnly v-if="lastModified">
<div>Last Changed</div>
<div>{{ timeAgo(lastModified) }}</div>
</ClientOnly>
</div>
</template>

<style scoped>
.meta {
font-size: .875rem;
line-height: 1.25rem;
display: grid;
grid-template-columns: 100px auto;
gap: .5rem;
align-items: flex-start;
margin-top: 1rem;
margin-bottom: 2rem;
}
</style>
8 changes: 6 additions & 2 deletions docs/src/utils/hooks/getHookFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import fs from 'node:fs';

export const getHookFile = async (name: string) => {
try {
const fileContent = await fs.promises.readFile(`./src/hooks/${name}/${name}.ts`, 'utf-8');
return fileContent;
const filePath = `./src/hooks/${name}/${name}.ts`;

const stats = await fs.promises.stat(filePath);
const content = await fs.promises.readFile(filePath, 'utf-8');

return { stats, content };
} catch (error) {
console.error(`Error reading file: ${error}`);
throw error;
Expand Down
6 changes: 4 additions & 2 deletions docs/src/utils/hooks/getHookItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getHooks } from './getHooks';

interface HookItem {
text: string;
category: string;
description: string;
link: string;
}
Expand All @@ -14,10 +15,10 @@ export const getHookItems = async (): Promise<HookItem[]> => {

const sidebar = await Promise.all(
hooks.map(async (hook) => {
const file = await getHookFile(hook);
const { content } = await getHookFile(hook);

const jsdocCommentRegex = /\/\*\*\s*\n([^\\*]|(\*(?!\/)))*\*\//;
const match = file.match(jsdocCommentRegex);
const match = content.match(jsdocCommentRegex);

if (!match) {
console.error(`No jsdoc comment found for ${hook}`);
Expand All @@ -34,6 +35,7 @@ export const getHookItems = async (): Promise<HookItem[]> => {
return {
text: hook,
description: jsdoc.description.description,
category: jsdoc.category?.name,
link: `/functions/hooks/${hook}`
};
})
Expand Down
3 changes: 2 additions & 1 deletion docs/src/utils/parseHookJsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export const parseHookJsdoc = (file: string) => {
const description = jsdoc.tags.find(({ tag }) => tag === 'description');
const usage = jsdoc.tags.find(({ tag }) => tag === 'example');
const deprecated = jsdoc.tags.find(({ tag }) => tag === 'deprecated');
const category = jsdoc.tags.find(({ tag }) => tag === 'category');
const apiParameters = jsdoc.tags.filter(
({ tag }) => tag === 'param' || tag === 'overload' || tag === 'returns'
);

return { description, usage, apiParameters, deprecated };
return { description, usage, apiParameters, deprecated, category };
};
1 change: 1 addition & 0 deletions src/hooks/useBattery/useBattery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface UseBatteryStateReturn {
/**
* @name useBattery
* @description - Hook for getting information about battery status
* @category Browser
*
* @returns {UseBatteryStateReturn} Object containing battery information & Battery API support
*
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useBoolean/useBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type UseBooleanReturn = [
/**
* @name useBoolean
* @description - Hook provides a boolean state and a function to toggle the boolean value
* @category Utilities
*
* @param {boolean} [initialValue=false] The initial boolean value
* @returns {UseBooleanReturn} An object containing the boolean state value and utility functions to manipulate the state
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useBrowserLanguage/useBrowserLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const subscribe = (cb: () => void) => {
/**
* @name useBrowserLanguage
* @description - Hook that returns the current browser language
* @category Browser
*
* @returns {string} The current browser language
*
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useClickOutside/useClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type UseClickOutside = {
/**
* @name useClickOutside
* @description - Hook to handle click events outside the specified target element(s)
* @category Sensors
*
* @overload
* @template Target The target element(s)
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useCopyToClipboard/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface UseCopyToClipboardReturn {
/**
* @name useCopyToClipboard
* @description - Hook that manages a copy to clipboard
* @category Browser
*
* @returns {UseCopyToClipboardReturn} An object containing the boolean state value and utility functions to manipulate the state
*
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useCounter/useCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type UseCounter = {
/**
* @name useCounter
* @description - Hook that manages a counter with increment, decrement, reset, and set functionalities
* @category Utilities
*
* @overload
* @param {number} [initialValue=0] The initial number value
Expand All @@ -56,7 +57,7 @@ export type UseCounter = {
*
* @example
* const { count, dec, inc, reset, set } = useCounter(5);
*
*
* @example
* const { count, dec, inc, reset, set } = useCounter({ initialValue: 5, min: 0, max: 10 });
*/
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDebounceCallback/useDebounceCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { debounce } from '@/utils/helpers';
/**
* @name useDebounceCallback
* @description - Hook that creates a debounced callback and returns a stable reference of it
* @category Utilities
*
* @template Params The type of the params
* @template Return The type of the return
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDebounceValue/useDebounceValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useDebounceCallback } from '../useDebounceCallback/useDebounceCallback'
/**
* @name useDebounceValue
* @description - Hook that creates a debounced value and returns a stable reference of it
* @category Utilities
*
* @template Value The type of the value
* @param {Value} value The value to be debounced
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDefault/useDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState } from 'react';
/**
* @name useDefault
* @description - Hook that returns the default value
* @category Utilities
*
* @template Value The type of the value
* @param {Value} initialValue The initial value
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDidUpdate/useDidUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomo
/**
* @name useDidUpdate
* @description – Hook that behaves like useEffect, but skips the effect on the initial render
* @category Lifecycle
*
* @param {EffectCallback} effect The effect callback
* @param {DependencyList} [deps] The dependencies list for the effect
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDisclosure/useDisclosure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface UseDisclosureReturn {
/**
* @name useDisclosure
* @description - Hook that allows you to open and close a modal
* @category Utilities
*
* @param {boolean} initialValue The initial value of the component
* @param {() => void} [options.onOpen] The callback function to be invoked on open
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDocumentEvent/useDocumentEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEventListener } from '../useEventListener/useEventListener';
/**
* @name useDocumentEvent
* @description - Hook attaches an event listener to the document object for the specified event
* @category Browser
*
* @template Event Key of document event map.
* @param {Event} event The event to listen for.
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDocumentTitle/useDocumentTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type UseDocumentTitleReturn = [
/**
* @name useDocumentTitle
* @description - Hook that manages the document title and allows updating it
* @category Browser
*
* @param {string} [value] The initial title. If not provided, the current document title will be used
* @param {boolean} [options.restoreOnUnmount] Restore the previous title on unmount
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDocumentVisibility/useDocumentVisibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const subscribe = (callback: () => void) => {
/**
* @name useDocumentVisibility
* @description – Hook that provides the current visibility state of the document
* @category Browser
*
* @returns {DocumentVisibilityState} The current visibility state of the document, which can be 'visible' or 'hidden'
*
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useEvent/useEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomo
/**
* @name useEvent
* @description - Hook that creates an event and returns a stable reference of it
* @category Browser
*
* @template Params The type of the params
* @template Return The type of the return
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useEyeDropper/useEyeDropper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface UseEyeDropperReturn {
/**
* @name useEyeDropper
* @description - Hook that gives you access to the eye dropper
* @category Browser
*
* @param {string} [initialValue=undefined] The initial value for the eye dropper
* @returns {UseEyeDropperReturn} An object containing the supported status, the value and the open method
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useFavicon/useFavicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type UseFaviconReturn = [string, Dispatch<SetStateAction<string>>];
/**
* @name useFavicon
* @description - Hook that manages the favicon
* @category Browser
*
* @param {string} [initialFavicon] The initial favicon. If not provided, the current favicon will be used
* @returns {UseFaviconReturn} An array containing the current favicon and a function to update the favicon
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useField/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export interface UseFieldReturn<Value> {
}

/**
* @name UseField
* @name useField
* @description - Hook to manage a form field
* @category Utilities
*
* @template Value The input value
* @template Type The input value type
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useFps/useFps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
/**
* @name useFps
* @description - Hook that measures frames per second
* @category Sensors
*
* @returns {number} A number which determines frames per second
*
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useFullscreen/useFullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type UseFullScreen = {
/**
* @name useFullscreen
* @description - Hook to handle fullscreen events
* @category Browser
*
* @overload
* @template Target The target element for fullscreen
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useHash/useHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type UseHashReturn = [string, (value: string) => void];
/**
* @name useHash
* @description - Hook that manages the hash value
* @category Browser
*
* @returns {UseHashReturn} An array containing the hash value and a function to set the hash value
*
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useHotkeys/useHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type UseHotkeysKey = { key: string; code: string; alias: string };
/**
* @name useHotkeys
* @description - Hook that listens for hotkeys
* @category Sensors
*
* @param {UseHotkeysHotkeys} hotkeys The key or keys to listen for
* @param {(event: KeyboardEvent) => void} callback The callback function to be called when the hotkey is pressed
Expand Down
Loading

0 comments on commit c40ed1c

Please sign in to comment.