Skip to content

Instantly share code, notes, and snippets.

View polRk's full-sized avatar
💯

Vladislav Polyakov polRk

💯
View GitHub Profile
@polRk
polRk / findBestPlacement.js
Last active October 28, 2018 09:42
Find the best placement of buttons in columns. Viber richMessage (see example.ts)
const findBestPlacement = (count) => {
const result = [4, 5, 6, 7].map(rows => ({
'Rows': rows,
'Columns': ((count / rows) ^ 0) === (count / rows) ? count / rows : (count / rows >> 0) + 1,
'Excess': count % rows,
'Free': count % rows === 0 ? 0 : rows - count % rows
}));
return [...result]
.filter((item) => {
@polRk
polRk / useAuth.ts
Created May 11, 2020 15:54
React Firebase useAuth
import * as firebase from 'firebase/app'
import { useEffect, useState, createContext } from 'react'
export type AuthState = {
initializing: boolean
user: firebase.User | null
}
export const userContext = createContext<AuthState>({
initializing: true,
@polRk
polRk / useWidthObserver.ts
Created May 11, 2020 16:45
Watch component width changes
import debounce from 'lodash/debounce'
import { RefObject, useEffect } from 'react'
export const useWidthObserver = <T extends HTMLElement>(
refObject: RefObject<T>,
setWidth: (width: number) => void
) => {
useEffect(() => {
function resizeListener() {
if (refObject.current) {
@polRk
polRk / useClient.ts
Last active May 11, 2020 16:47
React Apollo useClient with authorization
import {
ApolloClient,
ApolloLink,
HttpLink,
InMemoryCache,
} from '@apollo/client'
import { getMainDefinition } from '@apollo/client/utilities'
import { setContext } from '@apollo/link-context'
import { onError } from '@apollo/link-error'
import { RetryLink } from '@apollo/link-retry'
@mrkara
mrkara / read-key-press-cpp-linux.cpp
Last active March 18, 2023 18:59
[Read Key Press from Terminal under Linux with C++] #cpp #snippet
#include<iostream>
int main() {
char c;
// Set the terminal to raw mode
while(1) {
system("stty raw");
c = getchar();
// terminate when "." is pressed
system("stty cooked");
@polRk
polRk / calendar.ts
Created June 8, 2019 17:53
Generate month days calendar on TypeScript and date-fns
import { isSameMonth, startOfMonth } from 'date-fns'
export const generateCalendar = (
firstDateOfMonth: Date
): number[][] => {
const date = startOfMonth(firstDateOfMonth)
const getDay = (date: Date) => {
let day = date.getDay()
if (day === 0) day = 7
@smnbbrv
smnbbrv / promisified-grpc-client.ts
Last active November 4, 2023 21:22
Promisify @grpc-js service client with typescript
import { Client, ServiceError, Metadata, CallOptions, ClientUnaryCall } from '@grpc/grpc-js';
import { Message } from 'google-protobuf';
type OriginalCall<T, U> = (request: T, metadata: Metadata, options: Partial<CallOptions>, callback: (error: ServiceError, res: U) => void) => ClientUnaryCall;
type PromisifiedCall<T, U> = ((request: T, metadata?: Metadata, options?: Partial<CallOptions>) => Promise<U>);
export type Promisified<C> = { $: C; } & {
[prop in Exclude<keyof C, keyof Client>]: (C[prop] extends OriginalCall<infer T, infer U> ? PromisifiedCall<T, U> : never);
}
@niksudan
niksudan / mc-server-setup.md
Last active December 12, 2023 20:15
How to create a new Minecraft Server with DigitalOcean

Creating a new Minecraft Server

This is a short and simple guide on how to set up a multiplayer server running the latest version of Minecraft.

This guide has been tested on Ubuntu 16.04 and 18.04.

Setup

Create a new Ubuntu droplet on DigitalOcean. Make sure it has at least 2GB of RAM, and you provide it with your SSH key.

@polRk
polRk / App.tsx
Created May 11, 2020 16:02
React Apollo + Firebase authorization
import Dashboard from 'pages/Dashboard'
import Layout from 'components/Layout'
import Login from 'pages/Login'
import React from 'react'
import { useRoutes, Navigate, useLocation } from 'react-router-dom'
import { useAuth, userContext } from 'hooks/useAuth'
function App() {
const storedToken = localStorage.getItem('token')
const location = useLocation()
@konstantin24121
konstantin24121 / verifyTelegramWebAppData.tsx
Last active February 17, 2024 10:58
Telegram Bot 6.0 Validating data received via the Web App node implementation
const TELEGRAM_BOT_TOKEN = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw'; // https://core.telegram.org/bots#creating-a-new-bot
export const verifyTelegramWebAppData = async (telegramInitData: string): boolean => {
// The data is a query string, which is composed of a series of field-value pairs.
const encoded = decodeURIComponent(telegramInitData);
// HMAC-SHA-256 signature of the bot's token with the constant string WebAppData used as a key.
const secret = crypto